Self-Hosting Web API 2 Using OWIN


By: Amit Dabli | February 24, 2016

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 middle ware 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:

clip_image001

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 favorite browser to make sure it works.

 

This post has been viewed 9,245 times

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>