19

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?

Nate Barbettini
  • 51,256
  • 26
  • 134
  • 147
Troskyvs
  • 7,537
  • 7
  • 47
  • 115
  • 1
    Yes, OWIN is still a primary supported standard. .Net core supplies the bare essentials to build your web app. Pull in the OWIN packages from Nuget. https://docs.asp.net/en/latest/fundamentals/owin.html – Murray Foxcroft Aug 02 '16 at 06:26
  • 1
    @MurrayFoxcroft: Not exactly correct. .NET Core and ASP.NET Core are **not** based on OWIN. But they support plugging in of OWIN Middleware. – Tseng Aug 02 '16 at 07:21
  • @Tseng, indeed, that's why I said supported. – Murray Foxcroft Aug 02 '16 at 09:27

1 Answers1

34

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.

Nate Barbettini
  • 51,256
  • 26
  • 134
  • 147
  • 1
    Is it possible to access the OWIN environment dictionary from ASP.NET Core? – Fred Sep 10 '16 at 18:26
  • @Fred Yes, through the `HttpContext.Items` property. – Nate Barbettini Sep 11 '16 at 20:55
  • 5
    but `HttpContext.Items` is a `Dictionary` while the OWIN environment dictionary is a `Dictionary`. Also `HttpContext.Items` is empty, so while it may be used to share data between middleware it does not contain the original OWIN environment. – Fred Sep 12 '16 at 11:12