Search code examples
asp.net-web-api2httphandler

How to resolve handler mapping issues for WebAPI + OData + Owin + Ninject


I posted this yesterday ...

https://stackoverflow.com/questions/32615769/owin-webapi-odata-v4-aspnet-identity-initialisation

If I change the first line of the startup configuration method to ...

var config = GlobalConfiguration.Configuration ?? new HttpConfiguration();

Then I get this error where navigating to the root ...

The 'DelegatingHandler' list is invalid because the property 'InnerHandler' of 'CorsMessageHandler' is not null. Parameter name: handlers

I found some information dotted about that suggested that the base implementation of "HttpServer" had a bug in it related to batching and that microsoft had said they implemented some new batching framework so wern't going to fix it, the blogs offered up this code sample to fix the bug ...

public class BatchServer : HttpServer
{
    private readonly HttpConfiguration _config;

    public BatchServer(HttpConfiguration configuration)
        : base(configuration)
    {
        _config = configuration;
    }

    protected override void Initialize()
    {
        var firstInPipeline = _config.MessageHandlers.FirstOrDefault();
        if (firstInPipeline != null && firstInPipeline.InnerHandler != null)
        {
            InnerHandler = firstInPipeline;
        }
        else
        {
            base.Initialize();
        }
    }
}

Apparently this doesn't seem to be solving my problem.

So, i'm stuck between these two errors with no idea how to get out.


Solution

  • I just ran into the same error on a similar configuration. Basically, you're on the right path, but having read this great answer, I realized I had to use a different overload of the .UseWebApi() that accepted the HttpServer.

    So, the Startup.cs should look something like this:

    HttpConfiguration config = GlobalConfiguration.Configuration ?? new HttpConfiguration();
    config.EnableCors();
    //the next two lines I had to change - instead of 'config' pass in the HttpServer
    appBuilder.UseWebApi(GlobalConfiguration.DefaultServer);
    appBuilder.UseNinjectWebApi(GlobalConfiguration.DefaultServer);