Web API Hosting From OWIN With Windows Azure

Introduction

 
In this article, we will learn about the hosting of the Web API. We will do it from the Azure Worker Role using OWIN. As you know, OWIN is used for decoupled architectures between a Web Server and a Web Application.
 
In that context, OWIN is the ideal web interface for self-hosting the Web API. We'll use the HttpListener of OWIN to self-host the application based on OWIN.
 
Prerequisites
 
The following are the prerequisites:
  • Visual Studio 2012 or later
  • Windows Azure SDK for Visual Studio 2012
So, let's start the application development with the following sections:
  • Windows Azure Project
  • OWIN Package Installation
  • Role Endpoint
  • API Configuration
  • API Controller
  • OWIN Hosting
  • Azure Emulator
Windows Azure Project
 
In this section, we'll create an Azure Project. Proceed with the following.
 
Step 1: Open the Visual Studio with an Administrator Privilege
 
Visual Studio Start Page
 
Step 2: Choose the Azure Cloud Service and enter the service name 
 
New Azure Cloud Service
 
Step 3: In the next wizard, double-click the Worker Role and enter the worker role name.
 
Azure Worker Role
 
Visual Studio automatically creates the Windows Azure Cloud Service with the Worker Role.
 

OWIN Package Installation

 
In this section, we will add the OWIN package and the Web API. To add it you need to open the Package Manager Console and write the following command:
 
Install-Package Microsoft.AspNet.WebApi.OwinSelfHost -Pre
 
Package Manager Console
 
Role Endpoint
 
In this section, we'll add the endpoint and protocol modification.
 
Step 1: Select your worker role and open the properties.
 
Properties
 
Step 3: Click on "Add Endpoint".
 
WorkerRole Endpoints
 
Step 2: Enter the Endpoint name (you can also leave it as-is). Edit the public and private port.
 
Endpoint Port
 

API Configuration

 
Add a new class named Startup by right-clicking on the worker role.
 
Startup Class
 
Replace the code with the following code:
  1. using Owin;  
  2. using System.Web.Http;  
  3.    
  4. namespace ApiWorkerRole  
  5. {  
  6.     class Startup  
  7.     {  
  8.         public void Configuration(IAppBuilder MyOwinApp)  
  9.         {  
  10.             HttpConfiguration OwinConfig = new HttpConfiguration();  
  11.    
  12.             OwinConfig.Routes.MapHttpRoute("Default""{Controller}/{defaultid}"new { defaultid = RouteParameter.Optional });  
  13.             MyOwinApp.UseWebApi(OwinConfig);  
  14.         }  
  15.          
  16.     }  

API Controller
 
Now, add a class on the worker role and enter the name. Replace the code with the following code:
  1. using System;  
  2. using System.Net.Http;  
  3. using System.Web.Http;  
  4.    
  5. namespace ApiWorkerRole  
  6. {  
  7.     class CheckController : ApiController  
  8.     {  
  9.         public HttpResponseMessage GetText()  
  10.         {  
  11.             return new HttpResponseMessage()  
  12.             {  
  13.                 Content = new StringContent("Hello This is Owin from Azure")  
  14.             };  
  15.         }  
  16.    
  17.         public HttpResponseMessage GetText(int appid)  
  18.         {  
  19.             string Message = String.Format("Hello This is Owin on (appid= {0})", appid);  
  20.             return new HttpResponseMessage()  
  21.             {  
  22.                 Content = new StringContent(Message)  
  23.             };  
  24.         }  
  25.     }  

OWIN Hosting

 
Open WorkerRole.cs from the Solution Explorer and add the following code to the Run() method:
  1. private IDisposable MyApp = null
Now, modify your code with the following code:
  1. using System;  
  2. using System.Diagnostics;  
  3. using System.Net;  
  4. using System.Threading;  
  5. using Microsoft.WindowsAzure.ServiceRuntime;  
  6. using Microsoft.Owin.Hosting;  
  7.   
  8. namespace ApiWorkerRole {  
  9.     public class WorkerRole: RoleEntryPoint {  
  10.         private IDisposable MyApp = null;  
  11.         public override void Run() {  
  12.             // This is a sample worker implementation. Replace with your logic.  
  13.             Trace.TraceInformation("ApiWorkerRole entry point called""Information");  
  14.   
  15.             while (true) {  
  16.                 Thread.Sleep(10000);  
  17.                 Trace.TraceInformation("Working""Information");  
  18.             }  
  19.         }  
  20.   
  21.         public override bool OnStart() {  
  22.             ServicePointManager.DefaultConnectionLimit = 50;  
  23.   
  24.             var AppPoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["OwinEndpoint"];  
  25.             string identifier = string.Format("{0}://{1}",  
  26.                 AppPoint.Protocol, AppPoint.IPEndpoint);  
  27.   
  28.             Trace.TraceInformation(String.Format("OWIN URL is {0}", identifier),  
  29.                 "Information");  
  30.   
  31.             MyApp = WebApp.Start < Startup > (new StartOptions(url: identifier));  
  32.             return base.OnStart();  
  33.         }  
  34.   
  35.         public override void OnStop() {  
  36.             if (MyApp != null) {  
  37.                 MyApp.Dispose();  
  38.             }  
  39.             base.OnStop();  
  40.         }  
  41.     }  

Azure Emulator

 
Press F5 to start the execution. This will open the Windows Azure Emulator. You must run Visual Studio with Administrator Privileges to show the Emulator.
 
To open the Emulator:
 
Show Emulator
 
The Azure Emulator designates an IP address to your endpoint. You can also open the web browser with your address and controller name.
 
Windows Azure Emulator

Summary

 
This article introduced you to the Azure Cloud Service and the Azure Worker Role. You can add the OWIN self-hosting package to your API and End Points. Thanks for reading.