From 20c89166107bb2de83a26b2b36d231e98d753e5b Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Wed, 12 Jan 2022 20:12:10 +0900 Subject: [PATCH] Make BTCPayServer insensitive to the working directory in which it is started (Fix #1894) --- .../Hosting/ResourceBundleProvider.cs | 2 +- BTCPayServer/Program.cs | 23 +++++++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/BTCPayServer/Hosting/ResourceBundleProvider.cs b/BTCPayServer/Hosting/ResourceBundleProvider.cs index cee213e96..bb92a9801 100644 --- a/BTCPayServer/Hosting/ResourceBundleProvider.cs +++ b/BTCPayServer/Hosting/ResourceBundleProvider.cs @@ -35,7 +35,7 @@ namespace BTCPayServer.Hosting } else { - _InnerProvider = new BundleProvider(); + _InnerProvider = new BundleProvider(hosting); } } public Bundle GetBundle(string name) diff --git a/BTCPayServer/Program.cs b/BTCPayServer/Program.cs index fdaca5c34..3f8c1471d 100644 --- a/BTCPayServer/Program.cs +++ b/BTCPayServer/Program.cs @@ -11,6 +11,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting.Server.Features; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; +using System.Reflection; [assembly: InternalsVisibleTo("BTCPayServer.Tests")] namespace BTCPayServer @@ -21,6 +22,7 @@ namespace BTCPayServer { if (args.Length > 0 && args[0] == "run") args = args.Skip(1).ToArray(); // Hack to make dotnet watch work + ServicePointManager.DefaultConnectionLimit = 100; IWebHost host = null; var processor = new ConsoleLoggerProcessor(); @@ -41,10 +43,8 @@ namespace BTCPayServer logs.Configure(null); ///// - host = new WebHostBuilder() + var builder = new WebHostBuilder() .UseKestrel() - .UseIISIntegration() - .UseContentRoot(Directory.GetCurrentDirectory()) .UseConfiguration(conf) .ConfigureLogging(l => { @@ -56,8 +56,21 @@ namespace BTCPayServer l.AddFilter("Fido2NetLib.DistributedCacheMetadataService", LogLevel.Error); l.AddProvider(new CustomConsoleLogProvider(processor)); }) - .UseStartup() - .Build(); + .UseStartup(); + + // When we run the app with dotnet run (typically in dev env), the wwwroot isn't in the same directory + // than this assembly. + // But when we use dotnet publish, the wwwroot is published alongside the assembly! + // This fix https://github.com/btcpayserver/btcpayserver/issues/1894 + var defaultContentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + var defaultWebRoot = Path.Combine(defaultContentPath, "wwwroot"); + var defaultWebRootExists = Directory.Exists(defaultWebRoot); + if (!defaultWebRootExists) + { + // When we use dotnet run... + builder.UseContentRoot(Directory.GetCurrentDirectory()); + } + host = builder.Build(); host.StartWithTasksAsync().GetAwaiter().GetResult(); var urls = host.ServerFeatures.Get().Addresses; foreach (var url in urls)