Click here to Skip to main content
15,885,309 members
Articles / All Topics

WebAPI Self Hosting Using OWIN

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
22 Mar 2015CPOL1 min read 10.7K   4   1
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)


Written By
Software Developer (Senior)
India India
.Net developer and blogger by profession, keen to learn new technologies, love nature, music and cricket.

Blogger@http://dotnetfreakblog.wordpress.com

Comments and Discussions

 
Suggestionscreenshots missed Pin
He Lingyong13-Jan-21 15:14
He Lingyong13-Jan-21 15:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.