"
 
 
 
ASP.NET (snapshot 2017) Microsoft documentation and samples

OWIN Startup Class Detection

by Praburaj Thiagarajan, Rick Anderson

This tutorial shows how to configure which OWIN startup class is loaded. For more information on OWIN, see An Overview of Project Katana. This tutorial was written by Rick Anderson ( [@RickAndMSFT](https://twitter.com/#!/RickAndMSFT) ), Praburaj Thiagarajan, and Howard Dierking ( [@howard\_dierking](https://twitter.com/howard_dierking) ).

Prerequisites

Visual Studio 2013

OWIN Startup Class Detection

Every OWIN Application has a startup class where you specify components for the application pipeline. There are different ways you can connect your startup class with the runtime, depending on the hosting model you choose (OwinHost, IIS, and IIS-Express). The startup class shown in this tutorial can be used in every hosting application. You connect the startup class with the hosting runtime using one of the these approaches:

  1. Naming Convention: Katana looks for a class named Startup in namespace matching the assembly name or the global namespace.
  2. OwinStartup Attribute: This is the approach most developers will take to specify the startup class. The following attribute will set the startup class to the TestStartup class in the StartupDemo namespace.

    [!code-csharpMain]

       1:  [assembly: OwinStartup(typeof(StartupDemo.TestStartup))]

The OwinStartup attribute overrides the naming convention. You can also specify a friendly name with this attribute, however, using a friendly name requires you to also use the appSetting element in the configuration file. 3. The appSetting element in the Configuration file: The appSetting element overrides the OwinStartup attribute and naming convention. You can have multiple startup classes (each using an OwinStartup attribute) and configure which startup class will be loaded in a configuration file using markup similar to the following:

[!code-xmlMain]
   1:  <appSettings>  
   2:    <add key="owin:appStartup" value="StartupDemo.ProductionStartup" />
   3:  </appSettings>

The following key, which explicitly specifies the startup class and assembly can also be used:

[!code-xmlMain]
   1:  <add key="owin:appStartup" value="StartupDemo.ProductionStartup, StartupDemo" />

The following XML in the configuration file specifies a friendly startup class name of ProductionConfiguration.

[!code-xmlMain]
   1:  <appSettings>  
   2:    <add key="owin:appStartup" value="ProductionConfiguration" />       
   3:  </appSettings>

The above markup must be used with the following OwinStartup attribute which specifies a friendly name and causes the ProductionStartup2 class to run.

[!code-csharpMain]
   1:  [assembly: OwinStartup("ProductionConfiguration", typeof(StartupDemo.ProductionStartup2))]
   2:   
   3:  namespace StartupDemo
   4:  {
   5:      public class ProductionStartup
   6:      {
   7:          public void Configuration(IAppBuilder app)
   8:          {
   9:              app.Run(context =>
  10:              {
  11:                  string t = DateTime.Now.Millisecond.ToString();
  12:                  return context.Response.WriteAsync(t + " Production OWIN App");
  13:              });
  14:          }
  15:      }
  16:      public class ProductionStartup2
  17:      {
  18:          public void Configuration(IAppBuilder app)
  19:          {
  20:              app.Run(context =>
  21:              {
  22:                  string t = DateTime.Now.Millisecond.ToString();
  23:                  return context.Response.WriteAsync(t + " 2nd Production OWIN App");
  24:              });
  25:          }
  26:      }
  27:  }
  1. To disable OWIN startup discovery add the appSetting owin:AutomaticAppStartup with a value of "false" in the web.config file.

    [!code-xmlMain]

       1:  <add key="owin:AutomaticAppStartup" value="false" />

Create an ASP.NET Web App using OWIN Startup

  1. Create an empty Asp.Net web application and name it StartupDemo. - Install Microsoft.Owin.Host.SystemWeb using the NuGet package manager. From the Tools menu, select Library Package Manager, and then Package Manager Console. Enter the following command:

    [!code-powershellMain]
       1:  Install-Package Microsoft.Owin.Host.SystemWeb

The next time you want to add an Owin Startup class, it will be in available from the Add menu.

![](owin-startup-class-detection/_static/image2.png)  

Alternatively, you can right click the project and select Add, then select New Item, and then select the Owin Startup class.

![](owin-startup-class-detection/_static/image3.png)  

The app.Use lambda expression is used to register the specified middleware component to the OWIN pipeline. In this case we are setting up logging of incoming requests before responding to the incoming request. The next parameter is the delegate ( Func < Task > ) to the next component in the pipeline. The app.Run lambda expression hooks up the pipeline to incoming requests and provides the response mechanism. > [!NOTE] > In the code above we have commented out the OwinStartup attribute and we???re relying on the convention of running the class named Startup .- Press F5 to run the application. Hit refresh a few times.

![](owin-startup-class-detection/_static/image4.png)  

Note: The number shown in the images in this tutorial will not match the number you see. The millisecond string is used to show a new response when you refresh the page.
You can see the trace information in the Output window.

![](owin-startup-class-detection/_static/image5.png)

Add More Startup Classes

In this section we???ll add another Startup class. You can add multiple OWIN startup class to your application. For example, you might want to create startup classes for development, testing and production.

  1. Create a new OWIN Startup class and name it ProductionStartup.
  2. Replace the generated code with the following:

    [!code-csharpMain]
       1:  using System;
       2:  using System.Threading.Tasks;
       3:  using Microsoft.Owin;
       4:  using Owin;
       5:   
       6:  [assembly: OwinStartup(typeof(StartupDemo.ProductionStartup))]
       7:   
       8:  namespace StartupDemo
       9:  {
      10:      public class ProductionStartup
      11:      {
      12:          public void Configuration(IAppBuilder app)
      13:          {
      14:              app.Run(context =>
      15:              {
      16:                  string t = DateTime.Now.Millisecond.ToString();
      17:                  return context.Response.WriteAsync(t + " Production OWIN App");
      18:              });
      19:          }
      20:      }
      21:  }
  3. Press Control F5 to run the app. The OwinStartup attribute specifies the production startup class is run.

  4. Create another OWIN Startup class and name it TestStartup.
  5. Replace the generated code with the following:

    [!code-csharpMain]

       1:  using System;
       2:  using System.Threading.Tasks;
       3:  using Microsoft.Owin;
       4:  using Owin;
       5:   
       6:  [assembly: OwinStartup("TestingConfiguration", typeof(StartupDemo.TestStartup))]
       7:   
       8:  namespace StartupDemo
       9:  {
      10:      public class TestStartup
      11:      {
      12:          public void Configuration(IAppBuilder app)
      13:          {
      14:              app.Run(context =>
      15:              {
      16:                  string t = DateTime.Now.Millisecond.ToString();
      17:                  return context.Response.WriteAsync(t + " Test OWIN App");
      18:              });
      19:          }
      20:      }
      21:  }

The OwinStartup attribute overload above specifies TestingConfiguration as the friendly name of the Startup class. 6. Open the web.config file and add the OWIN App startup key which specifies the friendly name of the Startup class:

[!code-xmlMain]
   1:  <?xml version="1.0" encoding="utf-8"?>
   2:  <configuration>
   3:    <appSettings>
   4:      <add key="owin:appStartup" value="TestingConfiguration" />
   5:    </appSettings>
   6:    <system.web>
   7:      <compilation debug="true" targetFramework="4.5" />
   8:      <httpRuntime targetFramework="4.5" />
   9:    </system.web>
  10:  </configuration>
  1. Press Control F5 to run the app. The app settings element takes precedent, and the test configuration is run.

  2. Remove the friendly name from the OwinStartup attribute in the TestStartup class.

    [!code-csharpMain]
       1:  [assembly: OwinStartup(typeof(StartupDemo.TestStartup))]
  3. Replace the OWIN App startup key in the web.config file with the following:

    [!code-xmlMain]
       1:  <add key="owin:appStartup" value="StartupDemo.TestStartup" />
  4. Revert the OwinStartup attribute in each class to the default attribute code generated by Visual Studio:

    [!code-csharpMain]

       1:  [assembly: OwinStartup(typeof(StartupDemo.Startup))]
       2:  [assembly: OwinStartup(typeof(StartupDemo.ProductionStartup))]
       3:  [assembly: OwinStartup(typeof(StartupDemo.TestStartup))]

Each of the OWIN App startup keys below will cause the production class to run.

[!code-xmlMain]
   1:  <add key="owin:appStartup" value="StartupDemo.ProductionStartup" />
   2:  <add key="owin:appStartup" value="StartupDemo.ProductionStartup, StartupDemo" />
   3:  <add key="owin:appStartup" value="StartupDemo.ProductionStartup.Configuration, StartupDemo" />

The last startup key specifies the startup configuration method. The following OWIN App startup key allows you to change the name of the configuration class to MyConfiguration .

[!code-xmlMain]
   1:  <add key="owin:appStartup" value="StartupDemo.ProductionStartup2.MyConfiguration" />

Using Owinhost.exe

  1. Replace the Web.config file with the following markup:

    [!code-xmlMain]

       1:  <?xml version="1.0" encoding="utf-8"?>
       2:  <configuration>
       3:     <appSettings>
       4:        <add key="owin:appStartup" value="StartupDemo.Startup" />
       5:        <add key="owin:appStartup" value="StartupDemo.TestStartup" />
       6:     </appSettings>
       7:    <system.web>
       8:      <compilation debug="true" targetFramework="4.5" />
       9:      <httpRuntime targetFramework="4.5" />
      10:    </system.web>
      11:  </configuration>

The last key wins, so in this case TestStartup is specified. 2. Install Owinhost from the PMC:

[!code-consoleMain]
   1:  Install-Package OwinHost
  1. Navigate to the application folder (the folder containing the Web.config file) and in a command prompt and type:

    [!code-consoleMain]

       1:  ..\packages\Owinhost<Version>\tools\Owinhost.exe

The command window will show:

[!code-consoleMain]
   1:  C:\StartupDemo\StartupDemo>..\packages\OwinHost.2.0.0\tools\Owin
   2:  Host.exe
   3:  Starting with the default port: 5000 (http://localhost:5000/)
   4:  Started successfully
   5:  Press Enter to exit
  1. Launch a browser with the URL http://localhost:5000/.

OwinHost honored the startup conventions listed above. 5. In the command window, press Enter to exit OwinHost. 6. In the ProductionStartup class, add the following OwinStartup attribute which specifies a friendly name of ProductionConfiguration.

[!code-csharpMain]
   1:  [assembly: OwinStartup("ProductionConfiguration", 
   2:             typeof(StartupDemo.ProductionStartup))]
  1. In the command prompt and type:

    [!code-consoleMain]

       1:  ..\packages\OwinHost.2.0.0\tools\OwinHost.exe ProductionConfiguration

The Production startup class is loaded.

Our application has multiple startup classes, and in this example we have deferred which startup class to load until runtime. 8. Test the following runtime startup options:

[!code-consoleMain]
   1:  ..\packages\OwinHost.2.0.0-rc1\tools\OwinHost.exe StartupDemo.TestStartup
   2:  ..\packages\OwinHost.2.0.0-rc1\tools\OwinHost.exe "StartupDemo.TestStartup,StartupDemo"
   3:  ..\packages\OwinHost.2.0.0-rc1\tools\OwinHost.exe StartupDemo.TestStartup.Configuration
   4:  ..\packages\OwinHost.2.0.0-rc1\tools\OwinHost.exe "StartupDemo.TestStartup.Configuration,StartupDemo"




Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/aspnet/overview/owin-and-katana/owin-startup-class-detection.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>