Question

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).

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top