Introduction To OWIN Startup Class in Visual Studio 2013 RC

Introduction

This article introduces the configuration of the OWIN Startup Class. I provided an overview of OWIN and Katana in my previous article. In an OWIN application there is a Startup class that determines the components for the pipeline of the application. I am defining the OWIN startup class.

In that context, you can connect with your OWIN startup class at runtime by various ways. It depends on your hosting model like IIS, OwinHost, IIS-Express. The following are ways to connect the Startup class with the hosting runtime:

  • Naming Convention

    Katana looks for a Startup class in the namespace that matches the assembly name.
     
  • OwinStartup Attribute

    This is  the most general class that is used by the developers to determine the Startup class as shown in the following example:

    [assembly:OwinStartup(typeof(OwinDemo.Startup))]
     
  • Configure appSettings

    The appSettings element overrides both which are mentioned above. You can select your Startup class with the hosting runtime and configure it here. You can do it as follows:
     

    [assembly:OwinStartup("Brand",typeof(OwinDemo.BrandStartup))]

ASP.NET Application with OWIN

Let's start to create an ASP.NET Web Application in which we use the OWIN Startup using following the procedure.

Step 1: Create a new project.

NewApp-in-RC2013.jpg

Step 2: Create an application with an empty project template.

EmptyTemplate-in-RC2013.jpg

Step 3: Open the NuGet Package Manager.

NuGet-in-RC2013.jpg

Step 4: Install the OwinHost.

NuGet2-in-RC2013.jpg

Step 5: Add a OWIN Startup Class.

OwinStartupClass-in-RC2013.jpg

Step 6: Replace the code with the following code:

using System;

using Microsoft.Owin;

using Owin;

 

[assembly: OwinStartup(typeof(OwinDemo.Startup))]

 

namespace OwinDemo

{

    public class Startup

    {

        public void Configuration(IAppBuilder app)

        {

            string time=DateTime.Now.Millisecond.ToString();

 

            app.Run(async context =>

                {

                    await context.Response.WriteAsync(time + " " + "Welcome to OWIN Application");

                });

        }

    }

}

 

Step 7: Debug your application.

Debug.jpg

OWIN Startup Class

As I said earlier, you can add multiple OWIN Startup classes. So add multiple classes like Brand and Model.

Step 1: Add another OWIN Startup class with the name BrandStartup.cs.

Step 2: Change the code in the OwinDemo namespace with the following code:

namespace OwinDemo

{

    public class BrandStartup

    {

        public void Configuration(IAppBuilder app)

        {

            string time = DateTime.Now.Millisecond.ToString();

 

            app.Run(context =>

            {

                return context.Response.WriteAsync(time + " " + "Welcome to Brand OWIN Application");

            });

        }

    }

}

 

Step 3: Debug the application.

Debug2.jpg

Step 4: Add another OWIN Startup class with the name ModelStartup.cs.

Step 5: Change the code in the OwinDemo namespace with the following code:

namespace OwinDemo

{

    public class ModelStartup

    {

        public void Configuration(IAppBuilder app)

        {

            string time = DateTime.Now.Millisecond.ToString();

 

            app.Run(context =>

            {

                return context.Response.WriteAsync(time + " " + "Welcome to Model OWIN Application");

            });

        }

    }

}

Step 6: Debug the application.

Debug3.jpg

Note: As you can see, the application is by default running the Startup class. If you want to run another startup class then it is as follows.

Step 1: Open the Web.Config file and write the following code in it:

<configuration>

  <appSettings>

    <add key="owin:appStartup" value="Brand"/>

  </appSettings>

  <system.web>

    <compilation debug="true" targetFramework="4.5.1" />

    <httpRuntime targetFramework="4.5.1" />

  </system.web>

</configuration>

Step 2: Change the assembly code as follows:

[assembly: OwinStartup("Brand", typeof(OwinDemo.BrandStartup))]

Note: I am changing it in the BrandStartup.cs file code.

Step 3: Debug your application.

Debug4.jpg

Summary

So this article will help you to configure the OWIN Startup class and multiple Startup classes. This is done in the latest Visual Studio 2013 RC release. So just use this feature with the RC 2013. Thanks for reading.


Similar Articles