OWIN Startup Class Detection

 

啟動 OWIN Startup Class 有下列方法,以 Katana Web Application example 為例

一、Naming Convention

using Owin;

namespace WebApplication1
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.Run(context =>
            {
                context.Response.ContentType = "text/plain";
                return context.Response.WriteAsync("Hello World!");
            });
        }
    }
}

 

二、OwinStartup Attribute

using Owin;
using Microsoft.Owin;

[assembly: OwinStartup(typeof(WebApplication1.Startup2))]
namespace WebApplication1
{
    public class Startup2
    {
        public void Configuration(IAppBuilder app)
        {
            app.Run(context =>
            {
                context.Response.ContentType = "text/plain";
                return context.Response.WriteAsync("Hello World!");
            });
        }
    }
}

 

三、The appSetting element in the Web.config file

有以下要點

1、典型範例

using Owin;
//using Microsoft.Owin;

//[assembly: OwinStartup(typeof(WebApplication1.Startup2))]
namespace WebApplication1
{
    public class Startup2
    {
        public void Configuration(IAppBuilder app)
        {
            app.Run(context =>
            {
                context.Response.ContentType = "text/plain";
                return context.Response.WriteAsync("Hello World!");
            });
        }
    }
}

於 Web.config 加入

<configuration>
  <appSettings>
    <add key="owin:appStartup" value="WebApplication1.Startup2" />
  </appSettings>
</configuration>

 

2、Web.config 甚至可以設定到方法層級

using Owin;

namespace WebApplication1
{
    public class Startup2
    {
        public void MyConfiguration(IAppBuilder app)
        {
            app.Run(context =>
            {
                context.Response.ContentType = "text/plain";
                return context.Response.WriteAsync("Hello World!");
            });
        }
    }
}
<configuration>
  <appSettings>
    <add key="owin:appStartup" value="WebApplication1.Startup2.MyConfiguration" />
  </appSettings>
</configuration>

 

3、或是特別指定 namespace

<configuration>
  <appSettings>
    <add key="owin:appStartup" value="WebApplication1.Startup2,WebApplication1" />
  </appSettings>
</configuration>

 

4、也可以使用指定名稱

using Owin;
using Microsoft.Owin;

[assembly: OwinStartup("friendlyName", typeof(WebApplication1.Startup2))]
namespace WebApplication1
{
    public class Startup2
    {
        public void Configuration(IAppBuilder app)
        {
            app.Run(context =>
            {
                context.Response.ContentType = "text/plain";
                return context.Response.WriteAsync("Hello World!");
            });
        }
    }
}
<configuration>
  <appSettings>
    <add key="owin:appStartup" value="friendlyName" />
  </appSettings>
</configuration>

 

5、關掉 OWIN startup 進入點。 (跟上面範例無關)

<configuration>
  <appSettings>
    <add key="owin:appStartup" value="friendlyName" />
    <add key="owin:AutomaticAppStartup" value="false" />
  </appSettings>
</configuration>

 

四、利用 Owinhost.exe

不解釋,請參考 Using Owinhost.exe

 

參考資料:

OWIN Startup Class Detection