For better test job with Microsoft.Owin.Testing.TestServer
, I found that Global.asax is not loaded with Owin TestServer.
So, I try to move my Global.asax configurations to Startup.cs as below,
public partial class Startup { public void Configuration(IAppBuilder app) { // pasted Global.asax things start. GlobalConfiguration.Configuration.Formatters.Clear(); var jsonSerializerSettings = new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects, ReferenceLoopHandling = ReferenceLoopHandling.Ignore, }; GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter() { SerializerSettings = jsonSerializerSettings }); GlobalConfiguration.Configuration.Formatters.Add(new FormMultipartEncodedMediaTypeFormatter()); AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configure(WebApiConfig.Register); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); // pasted Global.asax things end. ConfigureAuth(app); } }
But TestServer
failed to initialize in every point of configuration such as AreaRegistration.RegisterAllAreas
, FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters)
, so on...
Minimum viable migration(successful test with TestServer) for me is as below.
public partial class Startup { public void Configuration(IAppBuilder app) { var config = new HttpConfiguration(); config.Formatters.Clear(); var jsonSerializerSettings = new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects, ReferenceLoopHandling = ReferenceLoopHandling.Ignore, }; config.Formatters.Add(new JsonMediaTypeFormatter() { SerializerSettings = jsonSerializerSettings }); config.Formatters.Add(new FormMultipartEncodedMediaTypeFormatter()); WebApiConfig.Register(config); // moved from GlobalConfiguration.Configure(WebApiConfig.Register) app.UseWebApi(config); ConfigureAuth(app); } }
Is there anyway to move all configurations to Startup.cs?
Global. asax file contains mostly application level pre-defined events where Startup. cs file is more about registering services and injection of modules in HTTP pipeline.
Global. ASP.NET Core introduced a new mechanism for bootstrapping an app. The entry point for ASP.NET applications is the Global. asax file. Tasks such as route configuration and filter and area registrations are handled in the Global.
How to add global. asax file: Select Website >>Add New Item (or Project >> Add New Item if you're using the Visual Studio web project model) and choose the Global Application Class template. After you have added the global.
The Application_Start and Application_End methods are special methods that do not represent HttpApplication events. ASP.NET calls them once for the lifetime of the application domain, not for each HttpApplication instance.
As you are already aware, OwinContext
consumed by Startup.Configuration()
is different from the traditional ASP.NET HttpContext
consumed by MvcApplication.Application_Start()
. Both are using different context pipelines. More specifically, ASP.NET MVC still relies on System.Web.dll
while ASP.NET Web API doesn't.
Therefore, based on your code, some methods usually laid in MvcApplication.Application_Start()
can't be run within Startup.Configuration()
:
AreaRegistration.RegisterAllAreas();
: This method relies on System.Web.dll
.RouteConfig.RegisterRoutes(RouteTable.Routes);
: RouteCollection
is a part of System.Web.dll
.GlobalConfiguration.Configure(WebApiConfig.Register)
: Again, RouteCollection
within WebApiConfig.Register()
is a part of System.Web.dll
.For URL routing within OWIN context, AttributeRouting
is recommended. So, instead of this, try config.MapHttpAttributeRoutes();
That will give you much freedom.
If you still want to run AreaRegistration.RegisterAllAreas();
within OWIN context, Startup.Configuration()
, I'd better recommend to import Katana library. This integrates OWIN with System.Web.dll
so that you probably archieve your goal.
HTH
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With