Are Microsoft.Owin types like OwinMiddleware and IOwinContext incompatible with other Owin servers?

假如想象 提交于 2020-01-01 04:11:07

问题


If I build an OWIN middleware using Microsoft.Owin types like OwinMiddleware and IOwinContext, would my middleware work with non-Microsoft Owin hosts/servers? I'm looking at the middleware classes for Nancy and SignalR and they seem very different from the OwinMiddleware base class that middlewares like the Cookie authentication middleware and WebApi is based on. I'm reading the spec but I'm still not clear if a non-Microsoft Owin server could work with the OwinMiddleware and IOwinContext type without having a dependency on Microsoft.Owin (which I guess would defeat the purpose of Owin).


回答1:


If you build middleware with the OwinMiddleware base type, by default it will not work with non Microsoft Owin servers. But it can be made to work with non Microsoft servers (SignalR works fine with Nowin as an example).

The default implementation of IAppBuilder (https://github.com/owin/owin-hosting/blob/master/src/main/Owin.Builder/AppBuilder.cs) has a signature conversion feature built in. This allows anyone to register a conversion from T -> AppFunc and AppFunc -> T. This means that you can mix and match middleware with different signatures in the same pipeline. (see https://katanaproject.codeplex.com/SourceControl/latest#src/Microsoft.Owin/Infrastructure/SignatureConversions.cs for an example of how OwinMiddleware works). As long as this conversion exists, you can make "raw" middleware (like nancy) work with OwinMiddleware seamlessly. To see how this works look here:

https://github.com/owin/owin-hosting/blob/master/src/main/Owin.Builder/AppBuilder.cs#L182 (Now that your brain as exploded... read on)

In the case of SignalR, we automatically add the conversion on your behalf (https://github.com/SignalR/SignalR/blob/dev/src/Microsoft.AspNet.SignalR.Core/Owin/OwinExtensions.cs#L168), but it can be done in any code that depends on OwinMiddleware to make sure the transition works.

If you use any of the Microsoft.Owin.Hosting to bootstrap your application but you use a non Microsoft web server, you'll get the conversion for free as well (see the nowin readme for an example https://github.com/Bobris/Nowin/blob/master/README.md).

Hope this helps.



来源:https://stackoverflow.com/questions/19613246/are-microsoft-owin-types-like-owinmiddleware-and-iowincontext-incompatible-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!