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).
OWIN allows web apps to be decoupled from web servers. It defines a standard way for middleware to be used in a pipeline to handle requests and associated responses. ASP.NET Core applications and middleware can interoperate with OWIN-based applications, servers, and middleware.
ASP.NET Core's OWIN support is deployed as part of the Microsoft. AspNetCore. Owin package. You can import OWIN support into your project by installing this package.
OWIN application has different working parts like (Host, Server, Middleware Pipeline, Application).
Kestrel comes from ASP.NET Core. It's a OWIN compatible web server.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With