无法启动 Kestrel,system.InvalidOperationException

Unable to start Kestrel, system.InvalidOperationException

我在启动网络时遇到问题 api。我正在使用 .net core 3.0 Web Api。 我在本地使用 iis express 调试和测试它没有问题。但是当我尝试将 Web api 部署到我的 linux 服务器时,我收到如下所示的错误消息:

crit: Microsoft.AspNetCore.Server.Kestrel[0] Unable to start Kestrel. System.InvalidOperationException: A path base can only be configured using IApplicationBuilder.UsePathBase().

和 运行 它在我本地机器上的调试模式下做同样的事情。所以当我在 VS 中调试应用程序时,我创建了一个新的配置文件来启动可执行文件。

Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll An unhandled exception of type 'System.InvalidOperationException' occurred in System.Private.CoreLib.dll A path base can only be configured using IApplicationBuilder.UsePathBase().

这是 Program.cs 和 Startup.cs

中的代码
public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .ConfigureKestrel((a, b) => { })
                .UseUrls("http://*:5050,https://*:5051")
                .UseKestrel()                
                .UseStartup<Startup>();
}
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.AddMvc(options => options.EnableEndpointRouting = false).SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

        connectionObject.SetConfiguration(Configuration);

        // configure strongly typed settings objects
        var appSettingsSection = Configuration.GetSection("AppSettings");
        services.Configure<AppSettings>(appSettingsSection);

        // configure jwt authentication
        var appSettings = appSettingsSection.Get<AppSettings>();
        var key = Encoding.ASCII.GetBytes(appSettings.Secret);
        services.AddAuthentication(x =>
        {
            x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        })
        .AddJwtBearer(x =>
        {
            x.Events = new JwtBearerEvents
            {
                OnTokenValidated = context =>
                {
                    var userService = context.HttpContext.RequestServices.GetRequiredService<IUserData>();
                    var userId = int.Parse(context.Principal.Identity.Name);
                    var user = userService.GetById(userId);
                    if (user == null)
                    {
                        // return unauthorized if user no longer exists
                        context.Fail("Unauthorized");
                    }
                    return Task.CompletedTask;
                }
            };
            x.RequireHttpsMetadata = false;
            x.SaveToken = true;
            x.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuerSigningKey = true,
                IssuerSigningKey = new SymmetricSecurityKey(key),
                ValidateIssuer = false,
                ValidateAudience = false
            };
        });

        // configure DI for application services
        services.AddScoped<IUserData, UD>();
    }

    // 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();
        }
        else
        {
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseAuthentication();
        app.UseMvc();
    }
}

这是我的启动设置

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:60850",
      "sslPort": 44372
    }
  },
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "IIS Express": {
      "commandName": "Executable",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Kestrel": {
      "commandName": "Executable",
      "executablePath": ".\WebApi.exe",
      "applicationUrl": "http://localhost:5050"
    }
  }
}

我试着摆弄

app.UsePathBase("/"); 

app.UsePathBase("http://localhost:5050") 

但在后一种情况下,错误消息是值需要以 /

开头

我以前见过其他人抱怨这个问题,但他们的解决方案对我不起作用。为什么我会得到这个?

UseUrls(...) 方法要求 URL 由分号 ; 分隔,而不是逗号 ,.

尝试将 program.cs 中的行更改为

.UseUrls("http://*:5050;https://*:5051")

文档说(强调我的):

The value provided using these approaches can be one or more HTTP and HTTPS endpoints (HTTPS if a default cert is available). Configure the value as a semicolon-separated list (for example, "Urls": "http://localhost:8000;http://localhost:8001").

可以看到完整的文档here