diff --git a/BTCPayServer/BTCPayServer.csproj b/BTCPayServer/BTCPayServer.csproj index dccf78fd1..2e9d08d4e 100644 --- a/BTCPayServer/BTCPayServer.csproj +++ b/BTCPayServer/BTCPayServer.csproj @@ -60,7 +60,7 @@ - + @@ -70,6 +70,10 @@ + + + + diff --git a/BTCPayServer/Controllers/ManageController.cs b/BTCPayServer/Controllers/ManageController.cs index 16940ea91..9b8ae9291 100644 --- a/BTCPayServer/Controllers/ManageController.cs +++ b/BTCPayServer/Controllers/ManageController.cs @@ -20,6 +20,9 @@ using System.Globalization; using BTCPayServer.Security; using BTCPayServer.U2F; using BTCPayServer.Data; +#if NETCOREAPP21 +using IWebHostEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment; +#endif namespace BTCPayServer.Controllers { @@ -33,7 +36,7 @@ namespace BTCPayServer.Controllers private readonly ILogger _logger; private readonly UrlEncoder _urlEncoder; TokenRepository _TokenRepository; - IHostingEnvironment _Env; + IWebHostEnvironment _Env; private readonly U2FService _u2FService; private readonly BTCPayServerEnvironment _btcPayServerEnvironment; StoreRepository _StoreRepository; @@ -49,7 +52,7 @@ namespace BTCPayServer.Controllers TokenRepository tokenRepository, BTCPayWalletProvider walletProvider, StoreRepository storeRepository, - IHostingEnvironment env, + IWebHostEnvironment env, U2FService u2FService, BTCPayServerEnvironment btcPayServerEnvironment) { diff --git a/BTCPayServer/Controllers/StoresController.cs b/BTCPayServer/Controllers/StoresController.cs index b52fb9f6b..51def6328 100644 --- a/BTCPayServer/Controllers/StoresController.cs +++ b/BTCPayServer/Controllers/StoresController.cs @@ -31,6 +31,10 @@ using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.Extensions.Options; using NBitcoin; using NBitcoin.DataEncoders; +using Newtonsoft.Json; +#if NETCOREAPP21 +using IWebHostEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment; +#endif namespace BTCPayServer.Controllers { @@ -57,8 +61,7 @@ namespace BTCPayServer.Controllers IFeeProviderFactory feeRateProvider, LanguageService langService, ChangellyClientProvider changellyClientProvider, - IOptions mvcJsonOptions, - IHostingEnvironment env, IHttpClientFactory httpClientFactory, + IWebHostEnvironment env, IHttpClientFactory httpClientFactory, PaymentMethodHandlerDictionary paymentMethodHandlerDictionary, CssThemeManager cssThemeManager) { @@ -68,7 +71,6 @@ namespace BTCPayServer.Controllers _UserManager = userManager; _LangService = langService; _changellyClientProvider = changellyClientProvider; - MvcJsonOptions = mvcJsonOptions; _TokenController = tokenController; _WalletProvider = walletProvider; _Env = env; @@ -95,7 +97,7 @@ namespace BTCPayServer.Controllers UserManager _UserManager; private LanguageService _LangService; private readonly ChangellyClientProvider _changellyClientProvider; - IHostingEnvironment _Env; + IWebHostEnvironment _Env; private IHttpClientFactory _httpClientFactory; private readonly PaymentMethodHandlerDictionary _paymentMethodHandlerDictionary; private readonly CssThemeManager _CssThemeManager; @@ -731,7 +733,6 @@ namespace BTCPayServer.Controllers } public string GeneratedPairingCode { get; set; } - public IOptions MvcJsonOptions { get; } [HttpGet] [Route("/api-tokens")] diff --git a/BTCPayServer/Controllers/WalletsController.cs b/BTCPayServer/Controllers/WalletsController.cs index 0bc3000ff..72d23ce55 100644 --- a/BTCPayServer/Controllers/WalletsController.cs +++ b/BTCPayServer/Controllers/WalletsController.cs @@ -44,7 +44,7 @@ namespace BTCPayServer.Controllers public ExplorerClientProvider ExplorerClientProvider { get; } private readonly UserManager _userManager; - private readonly IOptions _mvcJsonOptions; + private readonly JsonSerializerSettings _serializerSettings; private readonly NBXplorerDashboard _dashboard; private readonly IFeeProviderFactory _feeRateProvider; @@ -59,7 +59,7 @@ namespace BTCPayServer.Controllers CurrencyNameTable currencyTable, BTCPayNetworkProvider networkProvider, UserManager userManager, - IOptions mvcJsonOptions, + MvcNewtonsoftJsonOptions mvcJsonOptions, NBXplorerDashboard dashboard, RateFetcher rateProvider, ExplorerClientProvider explorerProvider, @@ -72,7 +72,7 @@ namespace BTCPayServer.Controllers RateFetcher = rateProvider; NetworkProvider = networkProvider; _userManager = userManager; - _mvcJsonOptions = mvcJsonOptions; + _serializerSettings = mvcJsonOptions.SerializerSettings; _dashboard = dashboard; ExplorerClientProvider = explorerProvider; _feeRateProvider = feeRateProvider; @@ -795,7 +795,7 @@ namespace BTCPayServer.Controllers if (result != null) { UTF8Encoding UTF8NOBOM = new UTF8Encoding(false); - var bytes = UTF8NOBOM.GetBytes(JsonConvert.SerializeObject(result, _mvcJsonOptions.Value.SerializerSettings)); + var bytes = UTF8NOBOM.GetBytes(JsonConvert.SerializeObject(result, _serializerSettings)); await webSocket.SendAsync(new ArraySegment(bytes), WebSocketMessageType.Text, true, new CancellationTokenSource(2000).Token); } } diff --git a/BTCPayServer/Hosting/BTCPayServerServices.cs b/BTCPayServer/Hosting/BTCPayServerServices.cs index 78a11ff80..1dc2ce079 100644 --- a/BTCPayServer/Hosting/BTCPayServerServices.cs +++ b/BTCPayServer/Hosting/BTCPayServerServices.cs @@ -60,6 +60,12 @@ namespace BTCPayServer.Hosting { public static IServiceCollection AddBTCPayServer(this IServiceCollection services, IConfiguration configuration) { +#if NETCOREAPP21 + services.AddSingleton, MVCConfigureOptions>(); + services.AddSingleton(); +#else + services.AddSingleton(o => o.GetRequiredService>().Value); +#endif services.AddDbContext((provider, o) => { var factory = provider.GetRequiredService(); @@ -329,5 +335,13 @@ namespace BTCPayServer.Hosting } } - +#if NETCOREAPP21 + class MVCConfigureOptions : IConfigureOptions + { + public void Configure(MvcJsonOptions options) + { + new Serializer(null).ConfigureSerializer(options.SerializerSettings); + } + } +#endif } diff --git a/BTCPayServer/Hosting/ResourceBundleProvider.cs b/BTCPayServer/Hosting/ResourceBundleProvider.cs index 6f10cd214..7945fc045 100644 --- a/BTCPayServer/Hosting/ResourceBundleProvider.cs +++ b/BTCPayServer/Hosting/ResourceBundleProvider.cs @@ -6,7 +6,12 @@ using System.Reflection; using System.Text; using System.Threading.Tasks; using BundlerMinifier.TagHelpers; +#if NETCOREAPP21 +using IWebHostEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment; +#else +using Newtonsoft.Json.Serialization; using Microsoft.AspNetCore.Hosting; +#endif using Newtonsoft.Json.Linq; namespace BTCPayServer.Hosting @@ -15,7 +20,7 @@ namespace BTCPayServer.Hosting { BundleProvider _InnerProvider; Lazy> _BundlesByName; - public ResourceBundleProvider(IHostingEnvironment hosting, BundleOptions options) + public ResourceBundleProvider(IWebHostEnvironment hosting, BundleOptions options) { if (options.UseBundles) { diff --git a/BTCPayServer/Hosting/Startup.cs b/BTCPayServer/Hosting/Startup.cs index 8da995fb4..9ea266990 100644 --- a/BTCPayServer/Hosting/Startup.cs +++ b/BTCPayServer/Hosting/Startup.cs @@ -1,4 +1,7 @@ using Microsoft.AspNetCore.Hosting; +#if NETCOREAPP21 +using IWebHostEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment; +#endif using Microsoft.AspNetCore.Builder; using System; using Microsoft.Extensions.DependencyInjection; @@ -31,13 +34,13 @@ namespace BTCPayServer.Hosting { public class Startup { - public Startup(IConfiguration conf, IHostingEnvironment env, ILoggerFactory loggerFactory) + public Startup(IConfiguration conf, IWebHostEnvironment env, ILoggerFactory loggerFactory) { Configuration = conf; _Env = env; LoggerFactory = loggerFactory; } - IHostingEnvironment _Env; + IWebHostEnvironment _Env; public IConfiguration Configuration { get; set; @@ -199,7 +202,7 @@ namespace BTCPayServer.Hosting public void Configure( IApplicationBuilder app, - IHostingEnvironment env, + IWebHostEnvironment env, IServiceProvider prov, BTCPayServerOptions options, ILoggerFactory loggerFactory) @@ -218,7 +221,7 @@ namespace BTCPayServer.Hosting } } - private static void ConfigureCore(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider prov, ILoggerFactory loggerFactory, BTCPayServerOptions options) + private static void ConfigureCore(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider prov, ILoggerFactory loggerFactory, BTCPayServerOptions options) { if (env.IsDevelopment()) { diff --git a/BTCPayServer/Services/BTCPayServerEnvironment.cs b/BTCPayServer/Services/BTCPayServerEnvironment.cs index 3c8f6eaaa..4cc8e50e9 100644 --- a/BTCPayServer/Services/BTCPayServerEnvironment.cs +++ b/BTCPayServer/Services/BTCPayServerEnvironment.cs @@ -1,5 +1,4 @@ -using Microsoft.AspNetCore.Hosting; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -8,6 +7,10 @@ using System.Text; using NBXplorer; using NBitcoin; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Hosting; +#if NETCOREAPP21 +using IWebHostEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment; +#endif namespace BTCPayServer.Services { @@ -15,7 +18,7 @@ namespace BTCPayServer.Services { IHttpContextAccessor httpContext; TorServices torServices; - public BTCPayServerEnvironment(IHostingEnvironment env, BTCPayNetworkProvider provider, IHttpContextAccessor httpContext, TorServices torServices) + public BTCPayServerEnvironment(IWebHostEnvironment env, BTCPayNetworkProvider provider, IHttpContextAccessor httpContext, TorServices torServices) { this.httpContext = httpContext; Version = typeof(BTCPayServerEnvironment).GetTypeInfo().Assembly.GetCustomAttribute().Version; @@ -28,7 +31,7 @@ namespace BTCPayServer.Services NetworkType = provider.NetworkType; this.torServices = torServices; } - public IHostingEnvironment Environment + public IWebHostEnvironment Environment { get; set; } diff --git a/BTCPayServer/Services/LanguageService.cs b/BTCPayServer/Services/LanguageService.cs index 73e0b8fd0..37af2b607 100644 --- a/BTCPayServer/Services/LanguageService.cs +++ b/BTCPayServer/Services/LanguageService.cs @@ -6,6 +6,11 @@ using Microsoft.Extensions.Hosting; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Serialization; +#if NETCOREAPP21 +using IWebHostEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment; +#else +using Microsoft.AspNetCore.Hosting; +#endif namespace BTCPayServer.Services { @@ -27,7 +32,7 @@ namespace BTCPayServer.Services { private readonly Language[] _languages; - public LanguageService(IHostingEnvironment environment) + public LanguageService(IWebHostEnvironment environment) { var path = (environment as HostingEnvironment)?.WebRootPath; if (string.IsNullOrEmpty(path))