From 4cc0e99149e6be7256a39e4a47970733c92d4867 Mon Sep 17 00:00:00 2001 From: phoenix Date: Mon, 3 Feb 2025 21:50:00 +0800 Subject: [PATCH] Refactor Kestrel configuration to support dynamic port assignment and update appsettings for HTTP endpoint --- Worker.cs | 10 ++++++++-- appsettings.Development.json | 8 ++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Worker.cs b/Worker.cs index 86109cb..43034cf 100644 --- a/Worker.cs +++ b/Worker.cs @@ -22,10 +22,16 @@ namespace ResourceMonitorService protected override async Task ExecuteAsync(CancellationToken stoppingToken) { var builder = WebApplication.CreateBuilder(); - builder.WebHost.ConfigureKestrel(options => + /* builder.WebHost.ConfigureKestrel(options => { options.ListenAnyIP(5000); // Replace 5000 with your desired port - }); + }); */ + /* builder.WebHost.ConfigureKestrel(options => + { + var url = builder.Configuration.GetValue("Kestrel:Endpoints:Http:Url"); + var port = url.Split(':').Last(); + options.ListenAnyIP(int.Parse(port)); + }); */ builder.Services.AddCors(options => { options.AddPolicy("AllowAllOrigins", diff --git a/appsettings.Development.json b/appsettings.Development.json index b2dcdb6..6c87146 100644 --- a/appsettings.Development.json +++ b/appsettings.Development.json @@ -4,5 +4,13 @@ "Default": "Information", "Microsoft.Hosting.Lifetime": "Information" } + }, + "RunAsWindowsService": false, + "Kestrel": { + "Endpoints": { + "Http": { + "Url": "http://*:5000" + } + } } }