From 2114650b089ff5acc2e91f17976fafd47c237f88 Mon Sep 17 00:00:00 2001 From: Sean McLellan Date: Sat, 12 Sep 2020 11:21:57 -0400 Subject: [PATCH] Fix issue with single-file-publish not being able to find appsettings.json --- .../CreateFSMosquitoClientRelease.yml | 2 +- FSMosquitoClient.csproj | 38 +++++++++++++------ Program.cs | 15 +++++++- appsettings.dev.json | 3 ++ appsettings.json | 2 +- 5 files changed, 44 insertions(+), 16 deletions(-) create mode 100644 appsettings.dev.json diff --git a/.github/workflows/CreateFSMosquitoClientRelease.yml b/.github/workflows/CreateFSMosquitoClientRelease.yml index 7c6da69..0e38746 100644 --- a/.github/workflows/CreateFSMosquitoClientRelease.yml +++ b/.github/workflows/CreateFSMosquitoClientRelease.yml @@ -35,7 +35,7 @@ jobs: # Build FSMosquitoClient - name: Build the application - run: dotnet publish $env:Project_Path --configuration Release --runtime win10-x64 /p:Platform=x64 /p:DeployOnBuild=true /p:PublishReadyToRun=true /p:PublishSingleFile=true /p:PublishTrimmed=true /p:PublishProfile="Create FSMosquitoClient Release" + run: dotnet publish $env:Project_Path --configuration Release --runtime win10-x64 /p:Platform=x64 /p:DeployOnBuild=true /p:PublishReadyToRun=true /p:PublishSingleFile=true /p:PublishTrimmed=true /p:DebugType=None /p:PublishProfile="Create FSMosquitoClient Release" # Run WarpPacker on the built files # This is commented right now as warp packer opens a console window prior to opening the application - unclear how to prevent this. diff --git a/FSMosquitoClient.csproj b/FSMosquitoClient.csproj index 0fa2b70..6a45111 100644 --- a/FSMosquitoClient.csproj +++ b/FSMosquitoClient.csproj @@ -10,18 +10,6 @@ true - - - - - - - - PreserveNewest - true - - - @@ -55,16 +43,42 @@ + + PreserveNewest + PreserveNewest + true + + + PreserveNewest + PreserveNewest + appsettings.json + true + true + + + false + + + + + + + + + + + + diff --git a/Program.cs b/Program.cs index 12487f5..e0a569e 100644 --- a/Program.cs +++ b/Program.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; + using System.Diagnostics; using System.Drawing; using System.IO; using System.Reflection; @@ -71,7 +72,11 @@ static void Main(string[] args) // Stand up DI var services = new ServiceCollection(); var builder = new ConfigurationBuilder() - .AddJsonFile("appsettings.json") + .SetBasePath(GetBasePath()) + .AddJsonFile("appsettings.json", false) +#if DEBUG + .AddJsonFile("appsettings.dev.json", false) +#endif .AddEnvironmentVariables(); var configuration = builder.Build(); var startup = new Startup(configuration); @@ -106,7 +111,7 @@ static void Main(string[] args) } } - static void GlobalExceptionHandler(object sender, UnhandledExceptionEventArgs e) + private static void GlobalExceptionHandler(object sender, UnhandledExceptionEventArgs e) { if (_logger != null) { @@ -118,5 +123,11 @@ static void GlobalExceptionHandler(object sender, UnhandledExceptionEventArgs e) Console.ReadLine(); Environment.Exit(1); } + + private static string GetBasePath() + { + using var processModule = Process.GetCurrentProcess().MainModule; + return Path.GetDirectoryName(processModule?.FileName); + } } } diff --git a/appsettings.dev.json b/appsettings.dev.json new file mode 100644 index 0000000..4810e74 --- /dev/null +++ b/appsettings.dev.json @@ -0,0 +1,3 @@ +{ + "fs_mosquito_serverurl": "wss://dev.fsmosquito.com/mqtt/ws" +} diff --git a/appsettings.json b/appsettings.json index bc4725d..f49ca52 100644 --- a/appsettings.json +++ b/appsettings.json @@ -1,5 +1,5 @@ { - "fs_mosquito_serverurl": "wss://localhost:8443/mqtt/ws", + "fs_mosquito_serverurl": "wss://www.fsmosquito.com/mqtt/ws", "fs_mosquito_clientid": "some_pilot", "fs_mosquito_username": "guest", "fs_mosquito_password": "guest"