33

I'm just trying to get my head around relationship between webapi, webhost (iis) and owin. I'll write down my current understanding, and I ask you to tell me if it is correct or not.

  • Webapi, unlike MVC was written in host-independent manner. This was in pre-Owin days, but apparently they anticipated that Owin would happen sooner or later. Host independency mainly means that System.Web is not used anywhere in Webapi code. It is System.Web that relies solely on IIS and would not work without it. This way Webapi could be theoretically hosted anywhere - once other hosts become available.
  • Webhost (Microsoft.Owin.Host.SystemWeb, Microsoft.AspNet.WebApi.WebHost) is a layer to between a higher level API (such as Webapi) and the IIS. Since Webapi initially was host independent, an intermediate layer required to make it run on a particular host, such as IIS. Webhost for Webapi (Microsoft.AspNet.WebApi.WebHost) provided this layer. Later on there will also be Webhost layer for Owin (Microsoft.Owin.Host.SystemWeb), that would allow hosting anything Owin compatible on IIS.
  • Owin came around the last. It basically provided an abstraction that theoretically would allow hosting any Owin compatible application on any host as long as there is a layer between owin and that host. Owin came with Webhost (Microsoft.Owin.Host.SystemWeb) (similar to how Webapi came with Webhost) that allowed Owin apps to be hosted on IIS. It also came with self-host (Microsoft.Owin.SelfHost) that allowed Owin apps to be hosted inside any executable. As far as Webapi concerned, Owin also came with Owin host for Webapi (Microsoft.AspNet.WebApi.Owin) which allowed running WebApi on Owin stack.

All the above means that one has two different ways of hosting Webapi on IIS. It can be done without Owin, using Webapi WebHost, or it can be done with Owin Host for Webapi and with Webhost for Owin.

Nuget references:

Is this understanding correct?

Andrew Savinykh
  • 25,351
  • 17
  • 103
  • 158

1 Answers1

20

Your understanding is generally correct, but the role of OWIN seems misunderstood. A more complete timeline would be:

  1. OWIN Standard developed to describe generic .NET web interface, a la WSGI/Rake/Connect (first commit in 2010).
  2. ASP.NET WebAPI is developed host-independent, but released with https://www.nuget.org/packages/Microsoft.AspNet.WebApi.WebHost/.
  3. Katana Project implements several OWIN hosts:
    1. https://www.nuget.org/packages/Microsoft.Owin.SelfHost/
    2. https://www.nuget.org/packages/Microsoft.Owin.Host.HttpListener/
    3. https://www.nuget.org/packages/Microsoft.Owin.Host.IIS/
    4. https://www.nuget.org/packages/Microsoft.Owin.Host.SystemWeb/
  4. ASP.NET WebAPI adapter for OWIN is released: https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Owin.

Your summary:

All the above means that one has two different ways of hosting Webapi on IIS. It can be done without Owin, using Webapi WebHost, or it can be done with Owin Host for Webapi and with Webhost for Owin.

I would restate that as:

All the above means that one has two different ways of hosting WebAPI. It can be done without Owin, using WebAPI WebHost, or it can be done with the OWIN adapter for WebAPI and any OWIN-compatible host. Hosting options on IIS are Microsoft.Owin.Host.IIS and Microsoft.Owin.Host.SystemWeb. Microsoft.AspNet.WebApi.OwinSelfHost is also provided.

dahlbyk
  • 75,175
  • 8
  • 100
  • 122
  • 2
    The only caveat I would add is that it is *wrong* to trying and host in IIS using Microsoft.AspNet.WebApi.Owin even though most samples use it - this is just for self-host. See http://stackoverflow.com/questions/33402654/web-api-with-owin-throws-objectdisposedexception-for-httpmessageinvoker and the underlying answer on the Katana project http://aspnetwebstack.codeplex.com/workitem/2091 – Paul Hatcher Feb 09 '17 at 16:21
  • 1
    @PaulHatcher Since you posted your comment, another post was made in that discussion on CodePlex. It points to a different discussion where a Katana team member instructs to use Microsoft.AspNet.WebApi.Owin, so either that ASP.NET team member is misinformed and it *does* work, or he is right but it works well enough. *(Also, I'm not an expert on OWIN but it doesn't make sense to me to say "this OWIN package is only meant to be used with this specific OWIN host".)* – user247702 Aug 10 '17 at 11:49
  • 1
    @Stijn Well I had lots of stability problems at scale until I removed WebApi.Owin, also the discussion is regarding modifying the headers, but this conversation http://katanaproject.codeplex.com/discussions/540202 which is the source mentions other issues with going this route such as not supporting WebAPI attribute routing - it's all a bit confused with no canoncial advice – Paul Hatcher Aug 10 '17 at 16:17