简体   繁体   English

如何在OWIN中删除中间件?

[英]How to remove middleware in OWIN?

I have registered an OWIN middleware in Configuration method. 我已经在Configuration方法中注册了OWIN中间件。 How can I then change remove the middleware. 然后如何更改删除中间件。

Particularly, I want to modify (remove/add) StaticFiles middleware after some files are changed (which I would like detect by FileSystemWatcher). 特别是,我想在某些文件更改后修改(删除/添加)StaticFiles中间件(我想由FileSystemWatcher检测)。

NOTE: There is a similar question regarding Connect of Node.js (which I consider where idea of OWIN come from). 注意:关于Node.js的连接有一个类似的问题 (我认为OWIN的思想来自何处)。

if you're using the microsoft implementation of AppBuilder ( Microsoft.Owin.Builder.AppBuilder ), you can't. 如果您使用的是AppBuilder的Microsoft实现( Microsoft.Owin.Builder.AppBuilder ),则不能。

In this implementation, The middlewares are stored in a private IList<T> , and there is no public access provided for this instance. 在此实现中,中间件存储在私有IList<T> ,并且没有为此实例提供公共访问。

You could create your own AppBuilder class, which had to implement the Owin.IAppBuilder interface. 您可以创建自己的AppBuilder类,该类必须实现Owin.IAppBuilder接口。

I did not think too much about it, but allowing the removal of middlewares feel like quite the ordeal: 我对此没有考虑太多,但是允许删除中间件感觉很麻烦:

Middlewares can be linked one to another. 中间件可以相互链接。 So if you delete one, you have to manage the relations of the other middlewares referring to it. 因此,如果删除一个,则必须管理引用它的其他中间件的关系。

So you can't simply remove a middleware. 因此,您不能简单地删除中间件。 But you could wrap the execution of the middleware with a condition. 但是您可以使用条件包装中间件的执行。 There may be a better solution. 可能有更好的解决方案。

IAppBuilder doesn't do it but you can add your own stage in the pipeline that lets you. IAppBuilder不会这样做,但是您可以在允许您的管道中添加自己的阶段。

A simple stage for this would use existing List<Action<OwinContext>> befores; 一个简单的步骤是使用现有的List<Action<OwinContext>> befores; and List<Action<OwinContext>> afters; List<Action<OwinContext>> afters; lists. 列表。 The stage gets defined and added like: 该阶段的定义和添加如下:

appBuilder.Use(
    new Func<Func<IDictionary<string, object>, Task>, Func<IDictionary<string, object>, Task>>(
        next => async environment =>
            {
                var ctx = new OwinContext(environment);
                lock(befores)
                {
                    foreach(before in befores)
                    {
                        before(ctx);
                    }
                }
                await next.Invoke(environment);
                lock(afters)
                {
                    foreach(after in afters)
                    {
                        after(ctx);
                    }
                }
        }));

Then, you manage your befores and afters lists any way you wish (with appropriate locking, of course). 然后,您管理beforesafters列出你希望的任何方式(用适当的锁定,当然)。 Caveat: I got this by stripping down code I use but I haven't even tried to compile it . 警告:我通过精简使用的代码来做到这一点,但是我什至没有尝试编译它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM