WebApplicationBuilder: Was löst launchUrl aus, um den Browser tatsächlich zu starten?C#

Ein Treffpunkt für C#-Programmierer
Anonymous
 WebApplicationBuilder: Was löst launchUrl aus, um den Browser tatsächlich zu starten?

Post by Anonymous »

Nach dem Upgrade auf .NET 10 funktionierten launchUrl und launchBrowser in appsettings.json unter Kestrel nicht mehr. Das Upgrade auf .NET 10 ist eine große Änderung, da IStartup ersetzt werden muss und ich ziemlich sicher bin, dass bei der Konvertierung etwas die launchUrl kaputt gemacht hat.
Um es klar zu sagen:
Das funktioniert:

Code: Select all

    "IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
Dies gilt nicht:

Code: Select all

    "http": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
Der Fehlermodus: Die Anwendung wird ausgeführt und lauscht, aber der Browser wird nie geöffnet. Wenn der Browser manuell geöffnet und auf die Webanwendung gezeigt wird, funktioniert alles.
Der Startcode:

Code: Select all

        System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
//Snip irrelevant
var config = CreateSettings(out var sources);
//Snip irrelevant
var builder = WebApplication.CreateEmptyBuilder(new WebApplicationOptions { ApplicationName = AppName, Args = args });
builder.Configuration.SetBasePath(Directory.GetCurrentDirectory());
builder.Configuration.Sources.AddRange(sources);
builder.Configuration.AddEnvironmentVariables();

builder.WebHost
.UseKestrel(options =>
{
// Snip loading production port config
// Looks essentially like this:
// if (port > 0)
//      options.Listen(System.Net.IPAddress.Loopback, port);
// but port is 0 in developer environment
options.AllowSynchronousIO = true;
})
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureLogging((logging) =>
{
logging.SetMinimumLevel(config.LogLevel);
logging.ClearProviders();
logging.AddConsole();
#if DEBUG
logging.AddDebug();
#endif
})
.UseIISIntegration()
.UseIIS() // Enables use of IIS in the case of IIS launch; does not force IIS.
.ConfigureServices((services) =>
{
Startup.ConfigureServices(services, config, builder.Environment);
});

builder.WebHost
.UseSetting(WebHostDefaults.ApplicationKey, AppName);

var app = builder.Build();
Startup.Configure(app, app.Environment, config);
app.Run();
Der Inhalt von Startup.Configure ist derselbe wie der .NET 9-Code mit IStartup.
Praktisch identischer funktionierender Startcode:

Code: Select all

        var builder = WebApplication.CreateEmptyBuilder(new WebApplicationOptions { ApplicationName = AppName, Args = args });
builder.Configuration.SetBasePath(Directory.GetCurrentDirectory());
builder.Configuration.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
builder.Configuration.AddJsonFile("devsettings.json", optional: true, reloadOnChange: true);
builder.Configuration.AddEnvironmentVariables();
builder.WebHost
.UseKestrel(options =>
{
// Trim rading config file.
System.Net.IPAddress bindto;
Action https =
listenOptions => {
if (!string.IsNullOrEmpty(certificate))
listenOptions.UseHttps(certificate, certificatepw);
// force http 1 to avoid the http2 cipher suite black list
// https://github.com/aspnet/AspNetCore/issues/14350
// https://tools.ietf.org/html/rfc7540#appendix-A
if (forcehttp1)
listenOptions.Protocols = Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols.Http1;
};
options.Listen(bindto, httpsport, https);
})
.ConfigureLogging((logging) =>
{
logging.ClearProviders();
logging.AddConsole();
#if DEBUG
logging.AddDebug();
#endif
})
.UseIISIntegration()
.UseIIS() // Enables use of IIS in the case of IIS launch; does not force IIS.
.ConfigureServices((services) =>
{
// It looks like you can do this but you can't.
// services.AddCors(options => options.AddPolicy("AllowAll", p => p.AllowAnyOrigin().AllowCredentials()));
// The CORS protocol does not allow specifying a wildcard (any) origin and credentials at the same time.  Configure the CORS policy by listing individual origins if credentials needs to be supported.
// But you can say it the long way.
services.AddCors((options) =>
options.AddPolicy("Enable", (builder) =>
builder.SetIsOriginAllowed(_ => true)
.WithMethods("GET")
.AllowAnyHeader()
.AllowCredentials())
);
services.AddRouting();
})
.UseSetting(WebHostDefaults.ApplicationKey, AppName);

var app = builder.Build();
app.UseCors();
// Trim MapGet()
app.Run();
Hypothesen:
  • das explizite Aufrufen von Listen ist wichtig. Ergebnis: false
  • Der Aufruf von AddJsonFile ist wichtig. Ergebnis: false
  • logging.SetMinimumLevel zählt. Ergebnis: falsch
Weitere Informationen: Die eigentliche auslösende Änderung war nicht die WebHostBuilder-Änderung und auch nicht das .NET 10-Upgrade. Ich musste zurück zum .NET 9-Zweig wechseln, der immer noch den alten Builder verwendet, und das Problem trat bei diesem Projekt erneut auf. Das heißt, die Visual Studio-Version spielt eine Rolle. Wenn ich nicht die fast identische funktionierende Code-Demo hätte, würde ich sagen, dass sie in Visual Studio einfach kaputt ist.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post