Web API with OWIN throws ObjectDisposedException for HttpMessageInvoker

I'm having some difficulties with OWIN in setting up ASP.NET WebApi2. The first request after restarting throws an exception:

  [ObjectDisposedException: Cannot access a disposed object.
     Object name: 'System.Net.Http.HttpMessageInvoker'.]
        System.Net.Http.HttpMessageInvoker.CheckDisposed () +327456
        System.Net.Http.HttpMessageInvoker.SendAsync (HttpRequestMessage request, CancellationToken cancellationToken) +24
        System.Web.Http.Owin. <InvokeCore> d__0.MoveNext () +501
        System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (Task task) +99
        System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Task task) +58
        Microsoft.Owin.Host.SystemWeb.IntegratedPipeline. <RunApp> d__5.MoveNext () +187
        System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (Task task) +99
        System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Task task) +58
        Microsoft.Owin.Host.SystemWeb.IntegratedPipeline. <DoFinalWork> d__2.MoveNext () +185
        Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End (IAsyncResult ar) +69
        Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork (IAsyncResult ar) +64
        System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute () +483
        System.Web.HttpApplication.ExecuteStep (IExecutionStep step, Boolean & completedSynchronously) +157
    
I believe the problem was caused by a long-term task when it first hit the EF DbContext. In general, the first request is about 4-6000 ms. There are no more exceptions after this first request.

I reproduced the problem in a simplified form using the webapi project and the following run OWIN (and not global.asax):

public class Startup { public void Configuration(IAppBuilder app) { var config = new HttpConfiguration(); config.MapHttpAttributeRoutes(); // ---- simulate long running initialization code ------- Thread.Sleep(3000); // ------------------------------------------------------ app.UseWebApi(config); } } 

I add a controller:

 [Route("api/test/number")] public class TestController : ApiController { public object Get() { return 42; } } 

When I request this, the first request throws an exception.

+6
source share
1 answer

This is an old question, but just spent hours trying to defeat ObjectDisposedException, and just found a solution that this AspNetWebStack problem on codeplex helped me with:

When your owin web api needs to run in self-service environments, you bind the web api configuration in the owin startup class

 public void Configuration(IAppBuilder app) { ConfigureOAuth(app); HttpConfiguration config = new HttpConfiguration(); WebApiConfig.Register(config); app.UseWebApi(config); } 

If you are going to use IIS as the host for your owin web api, then bind the web api configuration in the asax global class

 protected void Application_Start(object sender, EventArgs e) { GlobalConfiguration.Configure(WebApiConfig.Register); } 

This fixed an ObjectDisposedException and my web api works like a charm

+5
source

All Articles