From c63a48af6ef16a656fe28fa02f066bea1fc06dc6 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Sun, 29 Jan 2023 04:32:11 +0200 Subject: [PATCH] NetworkResurrector.Agent fix --- src/agent/NetworkResurrector.Agent/Program.cs | 25 +++++++++++++++---- .../PublishProfiles/FolderProfile.pubxml | 6 ++--- .../NetworkResurrector.Agent/appsettings.json | 3 --- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/agent/NetworkResurrector.Agent/Program.cs b/src/agent/NetworkResurrector.Agent/Program.cs index 4b0f210..b18aa5d 100644 --- a/src/agent/NetworkResurrector.Agent/Program.cs +++ b/src/agent/NetworkResurrector.Agent/Program.cs @@ -6,6 +6,8 @@ using NetworkResurrector.Agent.Extensions; using NetworkResurrector.Agent.Extensions.Serilog; using Serilog; using System; +using System.Diagnostics; +using System.Linq; namespace NetworkResurrector.Agent { @@ -13,7 +15,24 @@ namespace NetworkResurrector.Agent { public static void Main(string[] args) { - var builder = WebApplication.CreateBuilder(args); + var isConsole = Debugger.IsAttached || args.Contains("--console"); + + WebApplicationBuilder builder; + if (isConsole) + { + builder = WebApplication.CreateBuilder(args); + } + else + { + var webApplicationOptions = new WebApplicationOptions() + { + ContentRootPath = AppContext.BaseDirectory, + Args = args, + ApplicationName = Process.GetCurrentProcess().ProcessName + }; + builder = WebApplication.CreateBuilder(webApplicationOptions); + builder.Host.UseWindowsService(); + } builder.Host.UseSerilog((_, lc) => { @@ -27,10 +46,6 @@ namespace NetworkResurrector.Agent builder.Services.ConfigureServices(builder.Configuration); - var useWindowsService = builder.Configuration.GetValue("Host:UseWindowsService"); - if (useWindowsService) - builder.Host.UseWindowsService(); - var app = builder.Build(); app.Configure(builder.Configuration); diff --git a/src/agent/NetworkResurrector.Agent/Properties/PublishProfiles/FolderProfile.pubxml b/src/agent/NetworkResurrector.Agent/Properties/PublishProfiles/FolderProfile.pubxml index 3985073..89918b7 100644 --- a/src/agent/NetworkResurrector.Agent/Properties/PublishProfiles/FolderProfile.pubxml +++ b/src/agent/NetworkResurrector.Agent/Properties/PublishProfiles/FolderProfile.pubxml @@ -1,8 +1,8 @@  - + false false @@ -10,7 +10,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121. Release Any CPU FileSystem - bin\Release\net5.0\publish\ + bin\Release\net6.0\publish\ FileSystem net6.0 diff --git a/src/agent/NetworkResurrector.Agent/appsettings.json b/src/agent/NetworkResurrector.Agent/appsettings.json index daac0b0..6aef3d7 100644 --- a/src/agent/NetworkResurrector.Agent/appsettings.json +++ b/src/agent/NetworkResurrector.Agent/appsettings.json @@ -3,9 +3,6 @@ "ConnectionStrings": { "DatabaseConnection": "Server=#########;Database=#########;User Id=#########;Password=#########;MultipleActiveResultSets=true" }, - "Host": { - "UseWindowsService": true - }, "Serilog": { "MinimumLevel": { "Default": "Information",