Search code examples
c#asp.net-web-apiowinself-host-webapi

Increase request header limit in Web Api OWIN self host


I have a Web Api 2 application hosted in Service Fabric which is using Microsoft.AspNet.WebApi.OwinSelfHost to host the service. My Startup class is pretty typical:

public static class Startup
{
    public static void ConfigureApp(IAppBuilder app)
    {
        var config = new HttpConfiguration();

        config.MapHttpAttributeRoutes();
        // ...

        app.UseWebApi(config);
    }
}

The service farbric service is starting the web listener in its CreateServiceInstanceListeners() method:

protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
    return new[]
    {
        new ServiceInstanceListener(serviceContext => new OwinCommunicationListener(Startup.ConfigureApp, serviceContext, "WebApiEndpoint"))
    };
}

OwinCommunicationListener is a service fabric listener which will eventually start the web server:

public Task<string> OpenAsync(CancellationToken cancellationToken)
{
    // ...
    WebApp.Start(this.listeningAddress, appBuilder => this.startup(appBuilder)));

    return Task.FromResult(this.publishAddress);
}

I am running into an issue with some requests where I am trying to pass a large amount of data over the request headers. I seem to be hitting the limit because my requests are getting rejected with the following:

Bad Request - Request Too Long HTTP Error 400. The size of the request headers is too long.

Is there a way to configure the self hosted web api to increase the limit of the request headers?


Solution

  • Surely there must be ways to increase the self hosted web API header length.

    But do you really want to do it? Most likely you will end up fighting with a lot more configurations (other web servers, firewalls, load balancers, API clients, etc.)

    A proper architecture that doesn't involve sending large amounts of data in request headers will make your life a lot easier in the future.