From 663a62431b7077333882b1c2b7c33d3cf194c6af Mon Sep 17 00:00:00 2001 From: Jason Dove Date: Mon, 17 Jan 2022 16:31:22 -0600 Subject: [PATCH] properly fix startup paths (#576) --- ErsatzTV/Program.cs | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/ErsatzTV/Program.cs b/ErsatzTV/Program.cs index 670a86a8d..db1ee2527 100644 --- a/ErsatzTV/Program.cs +++ b/ErsatzTV/Program.cs @@ -1,6 +1,5 @@ using System; using System.IO; -using System.Reflection; using System.Threading.Tasks; using ErsatzTV.Core; using Microsoft.AspNetCore.Hosting; @@ -12,14 +11,32 @@ namespace ErsatzTV { public class Program { - private static IConfiguration Configuration { get; } = new ConfigurationBuilder() - .SetBasePath(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)) - .AddJsonFile("appsettings.json", false, true) - .AddJsonFile( - $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", - true) - .AddEnvironmentVariables() - .Build(); + static Program() + { + var executablePath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; + var executable = Path.GetFileNameWithoutExtension(executablePath); + + IConfigurationBuilder builder = new ConfigurationBuilder(); + + if ("dotnet".Equals(executable, StringComparison.InvariantCultureIgnoreCase)) + { + builder = builder.SetBasePath(Path.GetDirectoryName(typeof(Program).Assembly.Location)); + } + else + { + builder = builder.SetBasePath(Path.GetDirectoryName(executablePath)); + } + + Configuration = builder + .AddJsonFile("appsettings.json", false, true) + .AddJsonFile( + $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", + true) + .AddEnvironmentVariables() + .Build(); + } + + private static IConfiguration Configuration { get; } public static async Task Main(string[] args) {