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

Dav*_*WEB 1 iis asp.net-core

我们的托管部门不愿意允许运行Kestrel的ASP.NET核心托管,甚至不安装ASP.NET核心服务器托管套件(AspNetCoreModule).

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

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

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

Oli*_*ppi 6

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

这是可能的,因为ASP.NET Core 可以托管在OWIN服务器上,IIS 可以成为OWIN服务器.

看看下面的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();
    }
}
Run Code Online (Sandbox Code Playgroud)