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
OWIN
custom host. So
let's start with it.
Add a new Console application and let's call it
WebAPIOWINHosting.
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.
Add a new
webapi
controller
class. We remove all the methods and add a simple GET
method to get sum
of two random numbers.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.
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.
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.
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...!!!
No comments:
Post a Comment