How to Use OWIN to Self-Host ASP.NET Web API?

 In a Service Oriented architecture where there are multiple services serving the request from different clients it becomes imperative to add additional capabilities to these services and allow the Services to describe itself.

What is meant by Self-Hosting?

Self Hosting is a term used commonly to host a web Application or a Backend Service in a custom exe like console application instead of hosting it in IIS or some other exe. It is normally done to reduce the bulkiness and additional overhead which comes with external hosting platforms.

What is OWIN?

OWIN stands for Open Web Interface for .NET which basically acts as a abstraction between the Web Application and the Web Server so this means that Web Application is no more tightly bound to the Web Server which is hosting the application. OWIN  provides some standard API through which the Web Server can communicate with the Web Application. It also provides some middleware functionality through which it is possible to add pipelines through which the request flows and reaches the Web application end point.

These pipelines can hold different logic like authenticating the request which is coming in and validating the request etc.

Why we need to go with OWIN?

Still if we have any doubts about why we need to go ahead and use OWIN, then we can look at the below example which demonstrates using OWIN to host a Web API.

I have used a Console Application to host the Web API. Once the Console Application is created its time to add few Nuget packages which is needed to use the functionality of OWIN.

Below Two Packages are mainly needed 

  • Microsoft.AspNet.WebApi.OwinSelfHost
  • Swashbucke
Along with these there are multiple dependent bundles which also gets downloaded. Once these packages are installed then we can add a OWIN Startup file which specifies the configuration to be used by the Web Application.

Sample Startup File is given below:

Using OWIN To Selfhost ASP.NET Web API | Startup File

As shown in the Startup file we are specifying some HttpConfigurations which must be used.

Once the Startup file is created its time to Host the Application using the OWIN WebApp as shown below:

OWIN to Self Host the Web Application | Hosting

Project Structure is as shown below:

Web API Project structure


Now Run the Console Application and navigate to the URL : http://localhost:4500/swagger. We can see Swagger Web Page which gets loaded and it describes the API's exposed by the Web Application which its hosting as shown below:

API Description in OWIN Web Page

We have Defined a HomeController class which exposes few API as shown below:

ApiController exposing few API


These API's are described in the Swagger page which is quite helpful if we want to Test out few API's or simply if we want to know all the API's exposed by the Web Application.

Note: Please make the Controller class public otherwise Swagger/Clients will not be able to discover the API's exposed by the Controller.

Comments

Popular posts from this blog

Creating RESTful Minimal WebAPI in .Net 6 in an Easy Manner! | FastEndpoints

Step By Step Guide to Detect Heap Corruption in Windows Easily

How to dynamically add Properties in C# .NET?