在 IIS (10.0.17763.1) 上托管 .net5.0 核心 Web api 时出现问题

时间:2021-02-07 14:34:20

标签: c# iis asp.net-core-webapi core .net-5

我在 IIS 上托管我的 .net core 5.0 web api 时遇到问题,所以我决定创建一个基本的开箱即用版本 (WeatherForecast)。同样,这也不适用于 IIS(所以我不认为它不是我的代码)。我已经安装了 .Net Core Hosting Bundle 5.0 (V5.0.2) 并重新启动了 IIS(net stop was /ynet start w3svc)。

我的网站托管在 https://authdev.XXXXXXXX.net/api/weatherforecast(其中 XXXXXXXX.net 在我的内部网络上)。服务器是 Microsoft Hyper-V Server 2019。我有一个单独的应用程序池,.Net CLR 版本在集成管道模式下设置为“无托管代码”。我的网站设置了适当的绑定并正确配置。我已经通过创建一个从该服务器远程工作的 .Net Core 5.0 MVC 网站(开箱即用)确认了这一点。我在进程外模式下得到 404(并且相同的 url 在本地使用明显的服务器 url 更改)。我在某一阶段添加了日志信息,并注意到 Startup.cs 中的 public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 方法没有被命中。

Error message 500.30 - ASP.NET Core app failed to start

我认为此消息表明我的托管环境配置正确,但我的(开箱即用)web api 中存在错误。

程序.cs

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder
                    .UseContentRoot(Directory.GetCurrentDirectory())
                    .UseKestrel()
                    .UseIISIntegration()
                    .UseStartup<Startup>();
            });
}

web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath=".\WebApplication4.exe" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
    </system.webServer>
  </location>
</configuration>
<!--ProjectGuid: 9f838b88-2902-4f1d-96d5-468aa0449fb0-->

Startup.cs

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }

我尝试在进程内和进程外模式下运行它

进程内日志

crit: Microsoft.AspNetCore.Hosting.Diagnostics[6]
      Application startup exception
      System.InvalidOperationException: Application is running inside IIS process but is not configured to use IIS server.
         at Microsoft.AspNetCore.Server.IIS.Core.IISServerSetupFilter.<>c__DisplayClass2_0.<Configure>b__0(IApplicationBuilder app)
         at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
         at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
Unhandled exception. System.InvalidOperationException: Application is running inside IIS process but is not configured to use IIS server.
   at Microsoft.AspNetCore.Server.IIS.Core.IISServerSetupFilter.<>c__DisplayClass2_0.<Configure>b__0(IApplicationBuilder app)
   at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
   at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
   at WebApplication4.Program.Main(String[] args) in C:\Working\dev\sandbox\WebApplication4\WebApplication4\Program.cs:line 17

进程外日志

info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://127.0.0.1:43593
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: e:\websites\auth-api

1 个答案:

答案 0 :(得分:0)

您确定要发布为自包含包吗?因为如果没有,您的配置应该如下所示:

      <aspNetCore processPath="dotnet"
                  arguments=".\WebApplication16.dll"
                  stdoutLogEnabled="false"
                  stdoutLogFile=".\logs\stdout"
                  hostingModel="inprocess" />