ini*_*fus 3 c# kestrel .net-core-3.0
我在启动 Web api 时遇到问题。我正在使用 .net core 3.0 Web Api。我在本地使用iis express进行调试和测试,没有出现问题。但是当我尝试将 Web api 部署到我的 Linux 服务器时,我收到如下错误消息:
crit: Microsoft.AspNetCore.Server.Kestrel[0] 无法启动 Kestrel。System.InvalidOperationException:只能使用 IApplicationBuilder.UsePathBase() 配置路径基。
并在我的本地计算机上以调试模式运行它会执行相同的操作。所以我创建了一个新的配置文件来在 VS 中调试应用程序时启动可执行文件。
抛出异常:System.Private.CoreLib.dll 中的“System.InvalidOperationException” System.Private.CoreLib.dll 中发生类型为“System.InvalidOperationException”的未处理异常 路径基只能使用 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>();
}
Run Code Online (Sandbox Code Playgroud)
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();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的启动设置
{
"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"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试摆弄
app.UsePathBase("/");
Run Code Online (Sandbox Code Playgroud)
或者
app.UsePathBase("http://localhost:5050")
Run Code Online (Sandbox Code Playgroud)
但在后一种情况下,错误消息是该值需要以 / 开头
我以前见过其他人抱怨这个问题,但他们的解决方案对我不起作用。为什么我会得到这个?
该UseUrls(...)方法期望 URL 用分号;而不是逗号分隔,。
尝试将行更改program.cs为
.UseUrls("http://*:5050;https://*:5051")
Run Code Online (Sandbox Code Playgroud)
文件说(强调我的):
使用这些方法提供的值可以是一个或多个 HTTP 和 HTTPS 端点(如果默认证书可用,则为 HTTPS)。将值配置为分号分隔的列表(例如,“Urls”:“ http://localhost:8000;http://localhost:8001 ”)。
您可以在此处查看完整的文档
| 归档时间: |
|
| 查看次数: |
5535 次 |
| 最近记录: |