ASP.NET Core middleware or OWIN middleware?

2019-03-23 08:10发布

问题:

As I understand it, ASP.NET Core has support for OWIN middleware (via app.UseOwin()) in addition to its own native middleware.

What is the difference between ASP.NET Core middleware and OWIN middleware?

When designing a new middleware, how do I know if I should design it as a ASP.NET Core middleware or a OWIN middleware?

回答1:

Your question made me curious and I would like to share, what I have learned so far.

Katana is the implementation of the OWIN spec. After version 3.0 of Katana this technology has been fully integration in the web stack we know as ASP.NET Core today.

While this transition much has stayed similar to the OWIN specifications. Although some changes have been made. In order to use existing OWIN middleware in ASP.NET Core the supports OWIN by an optional feature ("app.UseOwin()").

If you want to target with your middleware ASP.NET apps and ASP.NET core apps, then I would use OWIN middleware. If you want to give ASP.NET Core developers a first class citizen experience, then a ASP.NET Core middleware would be recognized as more "fitting".

Some information about the relationship between ASP.NET Core middleware and OWIN middleware can be found here:

  • Katana, ASP.NET 5, and bridging the gap
  • Katana Project
  • https://docs.asp.net/en/latest/fundamentals/owin.html


回答2:

I have come to understand it as this; ASP.NET Core middleware is on a higher level than OWIN middleware which is on a lower level.

ASP.NET Core middleware has the advantage that it is much easier to develop a middleware in as you get passed in the HttpContext which you can use. The disadvantage is that the middleware you develop depends on ASP.NET Core.

OWIN is on a lower level and you get a OWIN environment which is a IDictionary<string, object>. The advantage is that is it not tied to ASP.NET hence can run on any OWIN server (such as Nowin). The disadvantage is that it takes more effort to code since you have to build your own context from the OWIN environment or use the OWIN environment dictionary directly and keep track of all OWIN keys and objects.

Edit: You don't have to keep track of OWIN keys yourself, you can use the OwinEnvironment class to get a strongly typed environment.

var environment = new OwinEnvironment(HttpContext);
var features = new OwinFeatureCollection(environment);