Web API OWIN hosting


In one of my previous articles, we discussed self-hosting web api using windows application. Continuing on the same paths, 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.

Web API self hosting using OWIN

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.

Web API self hosting using OWIN

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

Web API self hosting using OWIN

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.

Web API self hosting using OWIN

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

Web API self hosting using OWIN

Now simply run the application and the server is started.

Web API self hosting using OWIN

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.

Web API self hosting using OWIN

Easy to host, isn’t it. If you learnt it than do share it. Happy coding…!!!

Advertisements

About Jasminder

.Net developer and blogger by profession, keen to learn new technologies, love nature, music and cricket.
This entry was posted in ASP.Net web API and tagged . Bookmark the permalink.

I have a suggestion..

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s