Self-Hosting Web API 2 Using OWIN
OWIN stands for Open Web Interface for .NET . OWIN is an abstraction between .NET web servers and web applications. It decouples the application from the server, making it ideal for self-hosting. OWIN can serve as host for webapi, nancy or even as ftp server.
- Host application in your own process, independent of IIS e.g. in a windows service.
- Easily port application between hosts and potentially entire platforms/operating systems.
- Reduces middleware components, works as pipeline transparently.
- Simple workflow due to pipelines, and improved efficiency due to reduce pipeline.
- OWIN provides the following general definitions for OWIN-based application:
OWIN is a community-owned specification, not an implementation. The Katana project is Microsoft’s implementation of OWIN. This tutorial shows how to host ASP.NET Web API in a console application, using OWIN to self-host the Web API framework.
- Step 1: Create a project and install Nugets Create a project of type Console Application, to host our webapi using own. Install nugets
> Microsoft.AspNet.WebApi.OwinSelfHost> Topshelf We are using topshelf to host owin inside a windows service.
- Step 2: Setup Webapi Create HelloWorldApiController.cs
Also register the controller with route provider, using
Step 3: Register the web application with Owin To register webapi with owin, create a class OwinConfiguration.cs
Step 4: Host owin inside windows service Topshelf hosts OWIN as console application while debugging using visual studio. To register OWIN with topself create below class
Also change the main method as below
Step 5: Test Visit the URL http://localhost:8099/api/HelloWorldApi in your favourite browser to make sure it works.