Working With OWIN Hosting and Self Hosting in ASP.Net

Introduction

Today we'll learn how to host OWIN in IIS and self-host OWIN in a console application in Visual Studio 2013. OWIN stands for Open Web Interface for .NET; it elaborates an abstraction between the web servers and web applications. It creates a decoupled architecture between them. It makes it easier to create middleware for the .NET web development.

OWIN is a community-owned specification; it is not the implementation. The Katana project is a set of open-source OWIN components developed by Microsoft.

So, let's start to develop the application in which we host the OWIN application in IIS. Use the following procedure to do that

Hosting of OWIN in IIS

In this section, we'll create the ASP.NET Web Application in Visual Studio 2013 and host the OWIN in IIS. It provides the adaptability of an OWIN pipeline together with IIS. Use the following procedure.

Step 1: Open the Visual Studio and create the "New Project".

Step 2: Create the new ASP.NET Web Application and enter the name for the application.

Creating Web Application in VS 2013

Step 3: Select the Empty Project Template to create the application.

Creating Empty Project in VS 2013

Step 4: Now enter the following command to install the NuGet Package for the appplication.

Install-Pacakage Microsoft.Owin.Host.SystemWeb -Pre

It'll install the required package for the application as in the following:

Pacakage Manager Console

Adding OWIN Startup Class

In this we'll add the OWIN Startup Class and work with this class. So, use the following procedure.

Step 1: Just right-click on the application and click on the Add New Item and select OWIN Startup Class.

Adding Startup Class

Step 2: Add the following code:

namespace OwinSample

{

    public class OwinSampleStartup

    {

        public void Configuration(IAppBuilder sampleapp)

        {

            sampleapp.Run(sample =>

                {

                    sample.Response.ContentType = "text/plain";

                    return sample.Response.WriteAsync("Hello from OWIN");

                });

        }

    }

}

Step 3: Now run the application.

Generating Output from Owin

Working with Self-Host OWIN

In this section we'll see how to convert the application from IIS hosting to the self-hosting in a custom way. When the hosting is done by IIS then IIS acts as both the HTTP Server and as the process that hosts the server. The Self-hosting creates the process and uses the HttpListener class for the HTTP Server.

Step 1: In Visual Studio, create a new Console application.

Step 2: Install the following command in the Package Manager Console:

Install-Package Microsoft.Owin.SelfHost -Pre

Step 3: Now add the existing Startup class (created in the previous section).

Step 4: Modify the Program class with the following cdode:

namespace ConsoleApplication3

{

    class Program

    {

        static void Main(string[] args)

        {

            using (Microsoft.Owin.Hosting.WebApp.Start
<
OwinSampleStartup>("http://localhost:2259"))

            {

                Console.WriteLine("Open the address in the browser or");

                Console.WriteLine("Press any key to continue..");

                Console.ReadLine();

            }

        }

    }

}

In the code above, the localhost path is the same path that is copied from the browser of the previous application execution time.

Step 5: Now run the application.

Execution of Console Application

Step 6: Do not stop the debugging and click on the path to execute to the browser as in the following:

Opening Localhost in Visual Studio

Step 7: You will see the localhost running in the Visual Studio

Localhost Running in Visual Studio

That's it.

Summary

This article described OWIN hosting in IIS and the self-hosting process in a Console application in Visual Studio. You can also add the OWIN diagnostics to the application to see the exception. Thanks for reading.

Up Next
    Ebook Download
    View all
    Learn
    View all