这让我很困惑。我有这个 OWIN/Katana 中间件:
class M1 : OwinMiddleware
{
public M1(OwinMiddleware next) : base(next) { }
public override Task Invoke(IOwinContext context)
{
return Task.Run(() =>
{
Thread.Sleep(200); ; // do something
string reqUrl = context.Request.Uri.ToString(); //<- throws exception
})
.ContinueWith(t => this.Next.Invoke(context));
}
}
然后是一个启动类:
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.Use((context, next) =>
{
return Task.Run(() =>
{
}).ContinueWith(t => next());
});
app.Use<M1>();
}
}
运行此程序会在 M1 中引发 ObjectDisposedException:
无法访问已处置的对象。对象名称:“System.Net.HttpListenerRequest”。
堆栈跟踪:
在 System.Net.HttpListenerRequest.CheckDisposed() 在 System.Net.HttpListenerRequest.GetKnownHeader(HttpRequestHeader 标头) 在 System.Net.HttpListenerRequest.get_UserHostName() 在 Microsoft.Owin.Host.HttpListener.RequestProcessing.RequestHeadersDictionary.TryGetValue(String key, String[]& value) 在 Microsoft.Owin.HeaderDictionary.TryGetValue(String key, String[]& value) 在 Microsoft.Owin.Infrastructure.OwinHelpers.GetHeaderUnmodified(IDictionary
2 headers, String key) at Microsoft.Owin.Infrastructure.OwinHelpers.GetHeader(IDictionary
2 headers, String key) 在 Microsoft.Owin.Infrastructure.OwinHelpers .GetHost(IOwinRequest 请求) 在 Microsoft.Owin.OwinRequest.get_Host()
如果我在app.Use();之前删除该匿名中间件;那么不会抛出异常。
我做错了吗?