Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / All-Topics

WebAPI Self Hosting Using OWIN

0.00/5 (No votes)
22 Mar 2015CPOL1 min read 11K  
WebAPI Self Hosting Using OWIN

In one of my previous articles, we discussed this. Continuing on the same path, we will now discuss how we can self host the web API, using OWIN custom host. So let's start with it.

Add a new Console application and let's call it WebAPIOWINHosting.

Image 1

Next, in order to create a webapi and use OWIN custom host, we add references to Microsoft.AspNet.WebApi.OwinSelfHost, using nuget package manager. This will not only add the references for the webapi but also the OWIN components, along with the other required dependencies.

Image 2

Add a new webapi controller class. We remove all the methods and add a simple GET method to get the sum of two random numbers.

Image 3

Add a new class named Startup.cs. This is as per the OWIN specifications. Use the HttpConfiguration class to create the webapi routing template and add it to the request pipeline using the appBuilder.UseWebApi method, where appBuilder is of type IAppBuilder.

Image 4

Open the Program.cs file and start the server using the WebApp.Start method, specifying StartUp class as the entry point for the settings required. This is the OWIN specification of starting the server in the custom host.

Image 5

Image 6

To test the webapi, we will use the Chrome browser Postman extension. So enter the url of the webapi, which we specified in the Program.cs and Send the request. See the results are received.

Image 7

Easy to host, isn't it. If you learnt it, then do share it. The source code of the example is also attached here. Happy coding...!!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)