4

I've got app in .net core 5. And this is the code in Startup.cs

'''''

public static IHostBuilder CreateHostBuilder(string[] args) =>
        //Host.CreateDefaultBuilder(args)
        //    .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
    
        Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder => {
        webBuilder
            .UseStartup<Startup>()
            .UseKestrel(o =>
            {
                o.Listen(IPAddress.Any, 443, opt =>
                {
                    opt.UseHttps("pathfto.pfx", "passwordtocert");
                });
            });
    });

I would like to take upgrade it to .net core 6

I thought that it would be like this

var builder = WebApplication.CreateBuilder(args);

builder.Host
.ConfigureWebHostDefaults(webBuilder =>
{
    webBuilder
        .UseKestrel(o =>
        {
            o.Listen(IPAddress.Any, 443, opt => { opt.UseHttps("pathto.pfx", "passwordtocert"); });
        });
});

But it doesn't work when I try compile it.

Thank you in advance for any solutions.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
Dami Bore
  • 49
  • 1
  • 2
  • Does this answer your question? [Configuring Kestrel Server Options in .NET 6 Startup](https://stackoverflow.com/questions/69904260/configuring-kestrel-server-options-in-net-6-startup) – Guru Stron Dec 18 '21 at 23:35

2 Answers2

5

Try to use builder.WebHost

builder.WebHost.ConfigureKestrel(options =>
{
    options.Listen(IPAddress.Any, int.Parse(builder.Configuration.GetSection("SSL")["port"]), listenOptions =>
    {
        listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
        if (builder.Configuration.GetSection("SSL")["sertificateName"].Trim() != "")
            listenOptions.UseHttps(Path.Combine(AppContext.BaseDirectory, "cfg", builder.Configuration.GetSection("SSL")["sertificateName"]), builder.Configuration.GetSection("SSL")["password"]);

    });
});

More details you find on https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis?view=aspnetcore-6.0

Mihal By
  • 162
  • 2
  • 12
0

Your problem is your trying builder.Host instead of builder.WebHost. I think this would be the equivalent.

Program.cs

builder.WebHost.ConfigureKestrel(opt => {
    opt.ListenAnyIP(443, listOpt =>
     {
         listOpt.UseHttps(@"pathto.pfx", "passwordtocert");
     });
});

var app = builder.Build();
clamchoda
  • 4,411
  • 2
  • 36
  • 74