geInvoker的ObjectDisposedExceptio

geInvoker的ObjectDisposedExceptio

本文介绍了带有OWIN的Web API抛出HttpMessageInvoker的ObjectDisposedException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET WebApi2设置中使用OWIN时遇到一些困难.重新启动后的第一个请求导致异常:

I am having some difficulties with OWIN in a ASP.NET WebApi2 setting. The first request after a restart results in the 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
    

我认为该问题是由长时间运行的任务引起的,该任务是首次击中EF DbContext的.总的来说,第一个请求大约是4-6000ms.在发出第一个请求后,不再有例外.

I believe the problem is caused by a long running task when its hitting the EF DbContext for the first time. All in all the first request is around 4-6000ms. After this first request there are no more exceptions.

我已通过webapi项目和以下OWIN启动(并且没有global.asax)以简化的形式复制了该问题:

I have reproduced the issue in a simplified form with a webapi project and the following OWIN startup (and no 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);
    }
}

我添加了一个控制器:

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

当我请求此请求时,第一个请求导致异常.

When I request this the first request results in the exception.

推荐答案

这是一个老问题,但是花了几个小时试图击败ObjectDisposedException并找到了一个 codeplex上的AspNetWebStack问题帮助我:

This is an old question but a just spent hours trying to beat the ObjectDisposedException and just found a solution which this AspNetWebStack issue on codeplex helped me with:

当owin Web api应该在自托管环境中运行时,请在owin启动类中绑定Web api配置

When your owin web api should run in self-hosting environments you bind the web api config in the owin startup class

public void Configuration(IAppBuilder app)
{
    ConfigureOAuth(app);

    HttpConfiguration config = new HttpConfiguration();
    WebApiConfig.Register(config);
    app.UseWebApi(config);
}

如果您打算将IIS用作owin Web api的主机,则将Web api配置绑定到全局asax类中

If you intend to use IIS as host for your owin web api then bind the web api config in the global asax class

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

这消除了ObjectDisposedException,并且我的Web api像一个魅力一样运行

This eliminated the ObjectDisposedException and my web api runs like a charm

这篇关于带有OWIN的Web API抛出HttpMessageInvoker的ObjectDisposedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 03:34