From 536f98b5664f3f691c0d6713b2bfb7d10bf38d36 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Sun, 6 Oct 2019 15:47:46 +0900 Subject: [PATCH] Fix entity framework queries to work in netcoreapp3.0 --- BTCPayServer.Tests/UnitTest1.cs | 3 ++- BTCPayServer/Controllers/InvoiceController.cs | 1 + .../HostedServices/RatesHostedService.cs | 2 +- BTCPayServer/Hosting/Startup.cs | 8 ++++++-- .../Bitpay/BitpayAuthenticationHandler.cs | 2 +- .../Services/Invoices/InvoiceRepository.cs | 18 +++++++++--------- 6 files changed, 20 insertions(+), 14 deletions(-) diff --git a/BTCPayServer.Tests/UnitTest1.cs b/BTCPayServer.Tests/UnitTest1.cs index 01f9e7674..b1032e8eb 100644 --- a/BTCPayServer.Tests/UnitTest1.cs +++ b/BTCPayServer.Tests/UnitTest1.cs @@ -3037,7 +3037,8 @@ noninventoryitem: private static bool IsMapped(Invoice invoice, ApplicationDbContext ctx) { var h = BitcoinAddress.Create(invoice.BitcoinAddress, Network.RegTest).ScriptPubKey.Hash.ToString(); - return ctx.AddressInvoices.FirstOrDefault(i => i.InvoiceDataId == invoice.Id && i.GetAddress() == h) != null; + return (ctx.AddressInvoices.Where(i => i.InvoiceDataId == invoice.Id).ToArrayAsync().GetAwaiter().GetResult()) + .Where(i => i.GetAddress() == h).Any(); } } } diff --git a/BTCPayServer/Controllers/InvoiceController.cs b/BTCPayServer/Controllers/InvoiceController.cs index e0239a38b..6a1092d33 100644 --- a/BTCPayServer/Controllers/InvoiceController.cs +++ b/BTCPayServer/Controllers/InvoiceController.cs @@ -26,6 +26,7 @@ using Newtonsoft.Json; namespace BTCPayServer.Controllers { + [Filters.BitpayAPIConstraint(false)] public partial class InvoiceController : Controller { InvoiceRepository _InvoiceRepository; diff --git a/BTCPayServer/HostedServices/RatesHostedService.cs b/BTCPayServer/HostedServices/RatesHostedService.cs index 79369d88b..830bd9c65 100644 --- a/BTCPayServer/HostedServices/RatesHostedService.cs +++ b/BTCPayServer/HostedServices/RatesHostedService.cs @@ -31,7 +31,7 @@ namespace BTCPayServer.HostedServices internal override Task[] InitializeTasks() { - return new[] + return new Task[] { CreateLoopTask(RefreshCoinAverageSupportedExchanges), CreateLoopTask(RefreshCoinAverageSettings), diff --git a/BTCPayServer/Hosting/Startup.cs b/BTCPayServer/Hosting/Startup.cs index 5487a0812..2ede5c971 100644 --- a/BTCPayServer/Hosting/Startup.cs +++ b/BTCPayServer/Hosting/Startup.cs @@ -79,7 +79,7 @@ namespace BTCPayServer.Hosting // StyleSrc = "'self' 'unsafe-inline'", // ScriptSrc = "'self' 'unsafe-inline'" //}); - }).AddControllersAsServices(); + }).AddNewtonsoftJson().AddControllersAsServices(); services.TryAddScoped(); services.Configure(options => { @@ -255,10 +255,14 @@ namespace BTCPayServer.Hosting forwardingOptions.ForwardedHeaders = ForwardedHeaders.All; app.UseForwardedHeaders(forwardingOptions); #if !NETCOREAPP21 - app.UseRouting(); + app.UsePayServer(); + app.UseRouting(); #endif app.UseCors(); +#if NETCOREAPP21 app.UsePayServer(); +#endif + app.UseStaticFiles(); app.UseProviderStorage(options); app.UseAuthentication(); diff --git a/BTCPayServer/Security/Bitpay/BitpayAuthenticationHandler.cs b/BTCPayServer/Security/Bitpay/BitpayAuthenticationHandler.cs index 694e6a39f..a20baad25 100644 --- a/BTCPayServer/Security/Bitpay/BitpayAuthenticationHandler.cs +++ b/BTCPayServer/Security/Bitpay/BitpayAuthenticationHandler.cs @@ -103,7 +103,7 @@ namespace BTCPayServer.Security.Bitpay { using (StreamReader reader = new StreamReader(httpContext.Request.Body, Encoding.UTF8, true, 1024, true)) { - body = reader.ReadToEnd(); + body = await reader.ReadToEndAsync(); } httpContext.Request.Body.Position = 0; } diff --git a/BTCPayServer/Services/Invoices/InvoiceRepository.cs b/BTCPayServer/Services/Invoices/InvoiceRepository.cs index e0ce31cad..897303b09 100644 --- a/BTCPayServer/Services/Invoices/InvoiceRepository.cs +++ b/BTCPayServer/Services/Invoices/InvoiceRepository.cs @@ -86,12 +86,12 @@ retry: using (var db = _ContextFactory.CreateContext()) { return (await db.AddressInvoices + .Include(a => a.InvoiceData.Payments) + .Include(a => a.InvoiceData.RefundAddresses) #pragma warning disable CS0618 .Where(a => addresses.Contains(a.Address)) #pragma warning restore CS0618 .Select(a => a.InvoiceData) - .Include(a => a.Payments) - .Include(a => a.RefundAddresses) .ToListAsync()).Select(ToEntity); } } @@ -491,13 +491,13 @@ retry: if (queryObject.InvoiceId != null && queryObject.InvoiceId.Length > 0) { - var statusSet = queryObject.InvoiceId.ToHashSet(); + var statusSet = queryObject.InvoiceId.ToHashSet().ToArray(); query = query.Where(i => statusSet.Contains(i.Id)); } if (queryObject.StoreId != null && queryObject.StoreId.Length > 0) { - var stores = queryObject.StoreId.ToHashSet(); + var stores = queryObject.StoreId.ToHashSet().ToArray(); query = query.Where(i => stores.Contains(i.StoreDataId)); } @@ -508,8 +508,8 @@ retry: if (!string.IsNullOrEmpty(queryObject.TextSearch)) { - var ids = new HashSet(SearchInvoice(queryObject.TextSearch)); - if (ids.Count == 0) + var ids = new HashSet(SearchInvoice(queryObject.TextSearch)).ToArray(); + if (ids.Length == 0) { // Hacky way to return an empty query object. The nice way is much too elaborate: // https://stackoverflow.com/questions/33305495/how-to-return-empty-iqueryable-in-an-async-repository-method @@ -526,18 +526,18 @@ retry: if (queryObject.OrderId != null && queryObject.OrderId.Length > 0) { - var statusSet = queryObject.OrderId.ToHashSet(); + var statusSet = queryObject.OrderId.ToHashSet().ToArray(); query = query.Where(i => statusSet.Contains(i.OrderId)); } if (queryObject.ItemCode != null && queryObject.ItemCode.Length > 0) { - var statusSet = queryObject.ItemCode.ToHashSet(); + var statusSet = queryObject.ItemCode.ToHashSet().ToArray(); query = query.Where(i => statusSet.Contains(i.ItemCode)); } if (queryObject.Status != null && queryObject.Status.Length > 0) { - var statusSet = queryObject.Status.ToHashSet(); + var statusSet = queryObject.Status.ToHashSet().ToArray(); query = query.Where(i => statusSet.Contains(i.Status)); }