I am a beginner in .NET Core and I am trying to configure a custom domain to be able to use HTTPS for my ASP.NET Core 7.0 Web API.
I was following this guide: Custom Local Domain using HTTPS, Kestrel & ASP.NET Core, but as you can see, he uses .NET Core 3.1 and the structure of the files in the project has changed. In .NET Core 7.0 the program.cs
and startup.cs
file have been merged.
Here is my HostConfig.cs
file:
public class HostConfig
{
public static string CertPath { get; set; }
public static string CertPassword { get; set; }
}
Here is my program.cs
file:
using Dot7.API.CRUD.Data;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
builder.Host.ConfigureServices((context, services) =>
{
HostConfig.CertPath = builder.Configuration["CertPath"];
HostConfig.CertPassword = builder.Configuration["CertPassword"];
});
builder.Host.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.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();
When I build and run the application, I get this error message:
C:\Desktop\dotnet7-vue3-crud - Developpment - HTTPS\Dot7.API.CRUD\Program.cs(19,24): warning CS8604: Possible null reference argument for parameter 'fileName' in 'ListenOptions ListenOptionsHttpsExtensions.UseHttps(ListenOptions listenOptions, string fileName, string? password)'. [C:\Desktop\dotnet7-vue3-crud - Developpment - HTTPS\Dot7.API.CRUD\Dot7.API.CRUD.csproj]
Unhandled exception. System.NotSupportedException: ConfigureWebHost() is not supported by WebApplicationBuilder.Host. Use the WebApplication returned by WebApplicationBuilder.Build() instead.
at Microsoft.AspNetCore.Builder.ConfigureHostBuilder.Microsoft.AspNetCore.Hosting.Infrastructure.ISupportsConfigureWebHost.ConfigureWebHost(Action
1 configure, Action
1 configureOptions)
at Program.$(String[] args)
I don't really understand what this error message means and how to debug it. Can you somebody help me out?
PS: if you require additional files to inspect the issue, don't hesitate to ask.
I tried looking for Youtube videos, but most of the reference material uses an older version of .NET Core and as a beginner it is not really helpful. I also tried using ChatGPT, but obviously, the answer were outdated and not helpful.