Web API with OWIN. 1 HttpConfiguration object vs 2 HttpConfiguration objects
Asked Answered
S

0

10

I can't really think of a better title but I will explain the scenario in detail.

I have a .Net Web API that is being hosted by IIS.

    protected void Application_Start()
    {
        SwaggerConfig.Register();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        GlobalConfiguration.Configure(WebApiConfig.Register);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    }

I also have OWIN in the mix that doesn't do much other than initializing Hangfire, CORS and the web api.

    public static void Configuration(IAppBuilder app)
        {
            app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
            OAuthConfig.Config(app);
            Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage("SSDeployment");
            app.UseHangfireServer();

            app.UseWebApi(System.Web.Http.GlobalConfiguration.Configuration);
            app.Use(typeof(OwinLogging));

            Log.Debug("Starting SSDeployment.IntakeService - OWIN Configuration set for WebApi ...");
        }

Here is where it gets interesting. Initially, inside the Configuration(IAppBuilder app) method, the first line was

HttpConfiguration config = new HttpConfiguration();

In another unrelated situation, my DelegatingHandlers classes weren't working. I thought that maybe two intances of the HttpConfiguration were being created, therefore the problem.

I then decided to use the same HttpConfiguration that is globally declared in the GlobalConfiguration, you can see in the method I pasted here...

GlobalConfiguration.Configuration

That change, somehow, seemed to have fixed the DelegatingHandlers issue. However, soon enough some of my actions were broken.

After a POST where I create a new item, I always return a 201 Created response which adds the Location header. For this I use Url.Link("ActionName", new {field = value}).

Thing is that I get an 'Not implemented' exception.

When i go back and use a var config = new HttpConfiguration() in the OWIN then it works. Also my DelegatingHandlers work.

Can anyone explain what's going on here? I haven't been able to find much documentation about it. Why is the GlobalConfiguration.Configuration (an HttpConfiguration object) global, yet I can't use it in my OWIN's startup class.

It bothers me to 'fix' something without knowing why it is fixed. Shooting blanks is not my thing :)

thanks!

Smothers answered 26/7, 2016 at 14:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.