Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 345
enamrik Avatar asked Oct 27 '13 00:10

enamrik


People also ask

What is the use of Microsoft 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.

Is OWIN used in .NET core?

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.

Which are parts of OWIN application?

OWIN application has different working parts like (Host, Server, Middleware Pipeline, Application).

Is Kestrel a OWIN?

Kestrel comes from ASP.NET Core. It's a OWIN compatible web server.


1 Answers

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.

like image 198
davidfowl Avatar answered Oct 13 '22 09:10

davidfowl