在IIS上启动.NET Core Web API时出错:“应用程序在IIS进程内运行,但未配置为使用IIS服务器”

时间:2019-04-30 14:10:17

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

我已经为.NET Core v.10发布了IIS 2.2 API,由于某种原因,它可以响应500!

我尝试了

  • stdoutLogEnabled =“ true”(未生成日志文件)
  • app.UseDeveloperExceptionPage(什么都没有)
  • dotnet.exe x.dll,其中x是项目名称(表示:未配置任何侦听端点。绑定到http://localhost:5000),然后它可以在5000而不是IIS上的指定端口上完美运行

感谢您的建议。谢谢

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

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();

launchSettings.json文件:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:5694",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "x": {
      "commandName": "Project",
      "applicationUrl": "http://localhost:5694",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

启动:

public Startup(IConfiguration configuration, ILoggerFactory loggerFactory)
{
    NLog.LogManager.LoadConfiguration(String.Concat(Directory.GetCurrentDirectory(), "/nlog.config"));
    Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseCors("CorsPolicy");
    app.UseMvc();
}

我已经重新安装了.NET Core捆绑包,而我是从stdout那里得到的:

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.Hosting.Internal.WebHost.BuildApplication()
crit: Microsoft.AspNetCore.Hosting.Internal.WebHost[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.Hosting.Internal.WebHost.BuildApplication()
dbug: Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer[2]
      Failed to locate the development https certificate at '(null)'.
dbug: Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer[1]
      Unable to locate an appropriate development https certificate.
dbug: Microsoft.AspNetCore.Server.Kestrel[0]
      No listening endpoints were configured. Binding to http://localhost:5000 by default.
Hosting environment: Production
Content root path: C:\inetpub\wwwroot\VetWorkAdmin
dbug: Microsoft.AspNetCore.Hosting.Internal.WebHost[4]
      Hosting started
dbugNow listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
: Microsoft.AspNetCore.Hosting.Internal.WebHost[0]
      Loaded hosting startup assembly VetWork.Admin
dbug: Microsoft.AspNetCore.Hosting.Internal.WebHost[0]
      Loaded hosting startup assembly Microsoft.AspNetCore.Server.IISIntegration

2 个答案:

答案 0 :(得分:1)

尝试一下:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>();

对于InProcess,它使用IISHttpServer。检查Host your ASP.NET Core 2.2 Web App with IIS (in-process and out-of-process hosting model) and deploy to Docker Windows Containers解释详细信息。

答案 1 :(得分:1)

如果在将应用程序托管为Azure应用程序服务时遇到这种情况

添加此内容:

<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>

<TargetFramework>netcoreapp3.1</TargetFramework>

位于主机*.csproj文件中。