In one of my previous articles, we discussed `. Continuing on the same paths, we will now discuss how we can self host the web api, using
OWINcustom host. So let's start with it.
Add a new and let's call it
Next, in order to create a
OWINcustom host, we add references to
Microsoft.AspNet.WebApi.OwinSelfHost, using nuget package manager. This will not only add the references for the
webapibut also the
OWINcomponents, along with the other required dependencies.
Add a new
webapicontroller class. We remove all the methods and add a simple
GETmethod to get sum of two random numbers.
Add a new class named
Startup.cs. This is as per the
OWINspecifications. Use the
HttpConfigurationclass to create the
webapirouting template and add it to the request pipeline using the
appBuilderis of type
Program.csfile and start the server using the
StartUpclass as the entry point for the settings required. This is the
OWINspecification of starting the server in the custom host.
To test the
webapi,we will use the browser extension. So enter the url of the
webapi,which we specified in the
Program.csand the request. See the results are received.
Easy to host, isn't it. If you learnt it than do share it. Source code of the example is also attached here. Happy coding...!!!