Over the past several months, I’ve talked to a few people that were completely unaware that you could self host ASP.NET Web Api application without the need for IIS. Anyone who has worked with IIS knows that sometimes it can feel a bit heavy. Especially if you not using any other features other than serving static content or executing your ASP.NET Web Api. There are a couple important aspects to cover first which are what makes self hosting possible. If you have any questions, please follow me on Twitter.
Sponsor: Do you build complex software systems? See how NServiceBus makes it easier to design, build, and manage software systems that use message queues to achieve loose coupling. Get started for free.
OWIN defines a standard interface between .NET web servers and web applications. The goal of the OWIN interface is to decouple server and application, encourage the development of simple modules for .NET web development, and, by being an open standard, stimulate the open source ecosystem of .NET web development tools.To clarify and reword slightly, OWIN is a open source specification that is not defined by microsoft and is not an actual implementation.
KatanaKatana is an open source project by the Microsoft Open Technologies. It’s a set of components for building and hosting OWIN-based web applications that follows the OWIN specification. I’m not entirely sure what the status is of OWIN/Katana within ASP.NET 5 (vNext), however I would like to think the separation between server, host, middleware, and application will continue.
Video TutorialIf you prefer to follow along with a video tutorial, take a look at the video below.
All of the source code for this demo is available on Github.The first thing we are going to do for this demo is create a Console Application and add the Owin SelfHost and WebAPI Owin Selfhost nuget packages
PM> Install-Package Microsoft.Owin.SelfHost PM> Install-Package Microsoft.AspNet.WebApi.OwinSelfHostNext, we will create a Startup class that will contain the HttpConfiguration similar to how you configure Web Api currently. The startup class is the implementation of the OWIN specification that our OWIN Self Host implementation will use. The Startup class must provide a public Configuration() method with the signature void. Next, we will create a simple ApiController. Last step is to add the web server implementation to out Program.cs Main() method. This is done by simply calling out Katana implementation of WebApp.Start and passing our Startup class name as a type argument along with the host/port binding details as the first parameter argument. That’s it! Now when I make a call from Postman to http://localhost:8080/api/demo, here are the results:
21 thoughts to “How to Self Host ASP.NET Web Api”
This package alone was enough to run this demo: Microsoft.AspNet.WebApi.OwinSelfHost.
Is there a reason for also including Microsoft.Owin.SelfHost?
Interesting. I’ll have to take a look. Feel free to adjust and send a PR.
I might be a little late here, but Microsoft.Owin.SelfHost is a dependency for Microsoft.AspNet.WebApi.OwinSelfHost.
Therefore you don’t need to include it explicitly.
I have downloaded your code from git. I am running it in Visual Studio (running as Admin). No matter what URL I use against your server I get: 503 Service Unavailable
It is clearly from the server, headers:
Content-Type: text/html; charset=us-ascii
Date: Fri, 11 Dec 2015 11:21:49 GMT
Do you have any inkling as to what I am doing wrong?
Hmmm. I would expect you to have the issue if not running as admin. You could try adding a an ACL. Take a look at this thread:
It’s possible to access this API from another computer, on the same network? I’m trying to access with the IP address, from my phone, but I’m getting Bad Request – Invalid Hostname
So right now in my example I have the webserver listening on localhost (127.0.0.1) however you could bind to all IP addresses by changing:
Hi! Thanks for your answer.
When I try to change the ip address to your code I get an exeption: System.Reflection.TargetInvocationException
I tried to put my IP address instead of localhost and I’m getting this exception too 🙁
Finally I did it! Thanks a lot @dcomartin:disqus
I need to run the project with administration mode
Great. That was going to be my next suggestion.
Any idea’s on how to get this to work with views? I’m starting my web host from another project, so my executing code base is not that same as my project that contains my views.
I’m assuming you are referring to views in ASP.NET MVC. Are you receiving a specific error about the view not existing?
Simple easy to understand example, Thanks!
So I spend the last hour or so trying to figure out why I keep getting a 404 not found when calling the demo controller (own code, written from scratch). Then I download the github project, runs fine. Pulling out the little hair I have, I eventually see my DemoController wasn’t a public class. Added “public” in front, and now it works. lol
you saved my 1 hour. Thanks 😉
If I want to add an EF Core db context to it, any ideas? I can find examples where startup has a configure method, and it is added to services collection, but not really sure how to handle it here…
Can please you do an example using AutoFac for dependency injection? Once I added Autofac, the dependencies are being injected but I get a 404 not found when I hit the API.