31

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?

Youngjae
  • 21,562
  • 14
  • 100
  • 182

1 Answers1

46

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

justinyoo
  • 1,993
  • 1
  • 19
  • 23
  • 1
    So do we just install the package and job done or anything else have to be done ? – Gurpreet Jul 10 '15 at 15:55
  • 4
    @Gurpreet Indeed. If you install at least [Microsoft.Owin.Host.SystemWeb](https://www.nuget.org/packages/Microsoft.Owin.Host.SystemWeb/) and move bits and pieces from `Application_Start` in `Global.asax.cs` to `Startup.cs`, it should be fine. – justinyoo Aug 20 '15 at 05:42