Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core middleware or OWIN middleware?

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?

like image 853
Fred Avatar asked Sep 10 '16 18:09

Fred


People also ask

Is OWIN needed for NET Core?

NET Core and ASP.NET Core are not based on OWIN. But they support plugging in of OWIN Middleware.

What is ASP.NET Core middleware?

Middleware in ASP.NET Core controls how our application responds to HTTP requests. It can also control how our application looks when there is an error, and it is a key piece in how we authenticate and authorize a user to perform specific actions.

What is difference between middleware and filters in .NET Core?

Middleware only has access to the HttpContext and anything added by preceding middleware. In contrast, filters have access to the wider MVC context, so can access routing data and model binding information for example.


2 Answers

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
like image 67
Ralf Bönning Avatar answered Sep 21 '22 13:09

Ralf Bönning


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); 
like image 39
Fred Avatar answered Sep 22 '22 13:09

Fred