Does ASP.NET Core still use OWIN?

2019-02-06 15:42发布

问题:

I noticed when using C# to build an older MVC project, that there is a lot of references to an OWIN library. But when creating a .NET Core web application in VS2015 SP3, seems there's no reference to it.

Is this library obsoleted?

回答1:

OWIN is not a library, it's a specification. From the docs,

OWIN defines a standard interface between .NET web servers and web applications.

The "big idea" of OWIN is that you can decouple applications from the servers they run on, if everyone can agree on a common interface to use. In practice, an OWIN-compatible server can run any OWIN-compatible application. This is an improvement over the tight coupling we've had in the past (applications can only run on IIS, and that's it).

A lot of the OWIN spec is dedicated to describing the names of various properties. An OWIN-compatible server transforms incoming requests into a Dictionary<string, object> that contains a bunch of keys like owin.RequestBody and owin.ResponseBody. If the application or middleware that the request is passed to knows how to read those keys, it can process the request. That's it!

There are OWIN libraries (like OWIN and Microsoft.Owin), which is a common source of confusion. These libraries are just type-safe implementations of the OWIN interface, or helpers that make it easier to interact with components that are OWIN-compatible.

As @Murray and @Tseng pointed out, ASP.NET Core is not built on OWIN, but it is OWIN-compatible. Or, rather, the servers that ASP.NET Core run on (Kestrel, WebListener, IIS) are OWIN-compatible. ASP.NET Core is a further abstraction over low-level requests (the "OWIN level") and makes it easy to build things like controllers and server-rendered pages.

Since ASP.NET Core is OWIN-compatible, it's easy to plug in any OWIN-compatible middleware. The Microsoft.AspNetCore.Owin package exposes a UseOwin method:

app.UseOwin(pipeline =>
{
    pipeline(next => OwinHello);
    // where  OwinHello is a compatible middleware function
});

See the documentation for more examples of adding OWIN middleware to the ASP.NET Core pipeline.