自托管和IIS托管的OWIN项目

时间:2017-09-08 12:37:12

标签: iis-6 windows-7-x64 owin-middleware

我想修改我的解决方案以在自托管的OWIN实例下运行,但我还需要它在必要时在http://localhost下运行。

我应该如何构建我的Startup类以便被两者识别?

目前,我将Web API项目设置为控制台应用程序,项目URL为http://localhost:2746,这是我的启动类:

[assembly: OwinStartup(typeof(Startup))]
namespace Books
{
    public class Startup
    {
        public static void Main(string[] args)
        {
            const int port = 2746;
            var url = $"http://localhost:{port}/";

            using (WebApp.Start<Startup>(new StartOptions(url) { ServerFactory = "Microsoft.Owin.Host.HttpListener" }))
            {
                var client = new HttpClient { BaseAddress = new Uri(url) };
                Console.ReadLine();
            }
        }

        public void Configuration(IAppBuilder app)
        {
            HttpConfiguration httpConfiguration = new HttpConfiguration();
            WebApiConfig.Register(httpConfiguration);
            app.Use<CustomExceptionMiddleware>().UseWebApi(httpConfiguration);
            app.UseFileServer(StaticFileConfig.Build());
        }
    }
}

我在web.config中也有这个:

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

1 个答案:

答案 0 :(得分:0)

使用两个项目:

  1. 为api控制器创建一个Web项目。这个项目可以在iis中托管
  2. 创建另一个控制台项目作为自托管,此项目需要参考Web项目,现在您可以将WebApiConfig / Controllers分享到此项目
  3. ps:OwinStartup属性表示iis的入口点,但我使用传统的global.asax来初始化api配置。这样Startup类也可以从WebApiConfig

    共享配置