gpt4 book ai didi

asp.net - 使用 OWIN 的 Web API 会针对 HttpMessageInvoker 引发 ObjectDisposeException

转载 作者:行者123 更新时间:2023-12-02 04:41:31 26 4
gpt4 key购买 nike

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

    [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 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.

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);
}
}

我添加一个 Controller :

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

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

最佳答案

这是一个老问题,但只是花了几个小时试图击败 ObjectDisposeException,并且刚刚找到了一个解决方案,此 AspNetWebStack issue on codeplex帮助我:

当您的 owin Web api 应在自托管环境中运行时,您可以在 owin 启动类中绑定(bind) Web api 配置

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

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

如果您打算使用 IIS 作为 owin Web api 的主机,则在全局 asax 类中绑定(bind) Web api 配置

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

这消除了 ObjectDisposeException,我的 Web api 运行得非常顺利

关于asp.net - 使用 OWIN 的 Web API 会针对 HttpMessageInvoker 引发 ObjectDisposeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33402654/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com