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


clip_image002

Also register the controller with route provider, using


clip_image003

Step 3: Register the web application with Owin To register webapi with owin, create a class OwinConfiguration.cs


clip_image004

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


clip_image005

Also change the main method as below


clip_image006

Step 5: Test Visit the URL http://localhost:8099/api/HelloWorldApi in your favourite browser to make sure it works.

Write a comment
Cancel Reply
  • ERIC E STIMPSON July 30, 2020, 11:49 am
    Nice! Got me over the hump
    reply
  • Sanjay Bathre July 4, 2018, 2:06 am
    simple and nice article, thanks for sharing
    reply