首页 文章

在没有Kestrel的情况下在IIS中托管ASP.NET Core

提问于
浏览
1

我们的托管部门不愿意允许运行Kestrel的ASP.NET核心托管,甚至不安装ASP.NET Core Server Hosting Bundle(AspNetCoreModule) .

在这种情况下是否有允许ASP.NET核心的替代方案?

环境:带有最新IIS和.NET 4.6.2的Windows Server 2012 R2 .

它是一个共享托管环境,应用程序必须在IIS中运行 .

2 回答

  • 5

    是的,您可以使用WebListener Web服务器而不是Kestrel . WebListener仅适用于Windows平台,但由于这是您运行的地方,因此它是您的选择 .

    但是,WebListener不依赖IIS作为反向代理,实际上WebListener不能与IIS或IIS Express一起使用,因为它与ASP.NET核心模块不兼容 . 但它确实为您提供了一个非Kestrel选项,用于在Windows上托管ASP.NET Core .

    您可以在此处了解更多信息:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/weblistener

    If you must host in IIS and you don't want to use Kestrel and you are running on windows, then there are no options. 在Windows上,您可以使用不带IIS的WebListener托管,也可以使用IIS作为反向代理托管Kestrel . 这是目前Windows上唯一的两个选项 .

  • 0

    实际上,您可以使用OWIN在工作进程中的IIS中运行ASP.NET Core(因此不使用ASP.NET核心模块) .

    这是可能的,因为ASP.NET Core can be hosted on an OWIN server和IIS can be made an OWIN Server .

    看看下面的OWIN中间件,它展示了如何在IIS上运行ASP.NET Core . 有关更完整的示例,请参阅此要点:https://gist.github.com/oliverhanappi/3720641004576c90407eb3803490d1ce .

    public class AspNetCoreOwinMiddleware<TAspNetCoreStartup> : OwinMiddleware, IServer
        where TAspNetCoreStartup : class
    {
        private readonly IWebHost _webHost;
        private Func<IOwinContext, Task> _appFunc;
    
        IFeatureCollection IServer.Features { get; } = new FeatureCollection();
    
        public AspNetCoreOwinMiddleware(OwinMiddleware next, IAppBuilder app)
            : base(next)
        {
            var appProperties = new AppProperties(app.Properties);
            if (appProperties.OnAppDisposing != default(CancellationToken))
                appProperties.OnAppDisposing.Register(Dispose);
    
            _webHost = new WebHostBuilder()
                .ConfigureServices(s => s.AddSingleton<IServer>(this))
                .UseStartup<TAspNetCoreStartup>()
                .Build();
    
            _webHost.Start();
        }
    
        void IServer.Start<TContext>(IHttpApplication<TContext> application)
        {
            _appFunc = async owinContext =>
            {
                var features = new FeatureCollection(new OwinFeatureCollection(owinContext.Environment));
    
                var context = application.CreateContext(features);
                try
                {
                    await application.ProcessRequestAsync(context);
                    application.DisposeContext(context, null);
                }
                catch (Exception ex)
                {
                    application.DisposeContext(context, ex);
                    throw;
                }
            };
        }
    
        public override Task Invoke(IOwinContext context)
        {
            if (_appFunc == null)
                throw new InvalidOperationException("ASP.NET Core Web Host not started.");
    
            return _appFunc(context);
        }
    
        public void Dispose()
        {
            _webHost.Dispose();
        }
    }
    

相关问题