.NET 7.0配置Kestrel自定义域名HTTPS报错求助
ASP.NET Core 7.0 Web API 配置HTTPS自定义域名问题排查
问题说明
作为.NET Core新手,在为ASP.NET Core 7.0 Web API配置自定义域名HTTPS时,参考.NET Core 3.1教程导致两个问题:
- CS8604空引用警告
- 运行时抛出
System.NotSupportedException,提示WebApplicationBuilder.Host不支持ConfigureWebHost()
错误1:System.NotSupportedException 修复
.NET 6及以后版本(包括7.0)的WebApplicationBuilder已调整配置逻辑,不能通过builder.Host.ConfigureWebHostDefaults配置Web主机,直接使用builder.WebHost或依赖注入配置Kestrel即可。
替换原代码中builder.Host.ConfigureWebHostDefaults的部分,改为直接配置:
builder.WebHost.ConfigureKestrel(opt => { opt.ListenAnyIP(5000); opt.ListenAnyIP(5001, listOpt => { listOpt.UseHttps(HostConfig.CertPath, HostConfig.CertPassword); }); });
或者更推荐的依赖注入方式:
builder.Services.Configure<KestrelServerOptions>(opt => { opt.ListenAnyIP(5000); opt.ListenAnyIP(5001, listOpt => { listOpt.UseHttps(HostConfig.CertPath, HostConfig.CertPassword); }); });
错误2:CS8604 空引用警告修复
警告原因是从配置读取的CertPath和CertPassword可能为null,而UseHttps的fileName参数要求非null值,可通过以下方式解决:
方式1:强制校验配置存在
读取配置时使用GetRequiredSection,确保配置项存在,不存在则直接抛出异常:
HostConfig.CertPath = builder.Configuration.GetRequiredSection("CertPath").Value!; HostConfig.CertPassword = builder.Configuration.GetRequiredSection("CertPassword").Value!;
方式2:添加空值判断
修改HostConfig属性为可空类型,同时在配置Kestrel时添加空检查:
// HostConfig.cs public class HostConfig { public static string? CertPath { get; set; } public static string? CertPassword { get; set; } }
// Program.cs中配置Kestrel部分 builder.WebHost.ConfigureKestrel(opt => { opt.ListenAnyIP(5000); if (!string.IsNullOrEmpty(HostConfig.CertPath)) { opt.ListenAnyIP(5001, listOpt => { listOpt.UseHttps(HostConfig.CertPath, HostConfig.CertPassword); }); } else { throw new InvalidOperationException("CertPath配置项未设置"); } });
修改后的完整代码
HostConfig.cs
public class HostConfig { public static string CertPath { get; set; } = string.Empty; public static string CertPassword { get; set; } = string.Empty; }
Program.cs
using Dot7.API.CRUD.Data; using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); // 读取配置并确保非空 HostConfig.CertPath = builder.Configuration.GetRequiredSection("CertPath").Value!; HostConfig.CertPassword = builder.Configuration.GetRequiredSection("CertPassword").Value!; // 配置Kestrel builder.WebHost.ConfigureKestrel(opt => { opt.ListenAnyIP(5000); opt.ListenAnyIP(5001, listOpt => { listOpt.UseHttps(HostConfig.CertPath, HostConfig.CertPassword); }); }); builder.Services.AddCors(options => { options.AddPolicy("Cors", p => { p.AllowAnyOrigin() .AllowAnyHeader() .AllowAnyMethod(); }); }); builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddDbContext<databaseContext>(options => { options.UseSqlServer(builder.Configuration.GetConnectionString("databaseConnection")); }); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseCors("Cors"); app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run();
内容的提问来源于stack exchange,提问作者Oddy
相关产品推荐
相关产品推荐

