1

这让我很困惑。我有这个 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(IDictionary2 headers, String key) 在 Microsoft.Owin.Infrastructure.OwinHelpers .GetHost(IOwinRequest 请求) 在 Microsoft.Owin.OwinRequest.get_Host()

如果我在app.Use();之前删除该匿名中间件;那么不会抛出异常。

我做错了吗?

4

1 回答 1

1

您应该使用 await 而不是 ContinueWith 以避免在执行中间件之前将控制权返回给 owin 管道。像这样的东西:

class M1 : OwinMiddleware
{
    public M1(OwinMiddleware next) : base(next) { }
    public override async Task Invoke(IOwinContext context)
    {
        await Task.Run(() =>
        {
            Thread.Sleep(200); ; // do something
            string reqUrl = context.Request.Uri.ToString(); //<- throws exception
        });

        await this.Next.Invoke(context);    
     }
}

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.Use(async (context, next) =>
        {
            await Task.Run(() => { });
                    await next();
            });

            app.Use<M1>();
        }
    }
}
于 2014-03-16T22:27:18.800 回答