Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Fix IAdminDataScope<Product> registration collision when hosts share one process
KrzysztofPajak279b5b28df42
diff --git a/src/Web/Grand.Web.Admin/Startup/StartupApplication.cs b/src/Web/Grand.Web.Admin/Startup/StartupApplication.csindex 4031d9509..23cdc2c44 100644--- a/src/Web/Grand.Web.Admin/Startup/StartupApplication.cs+++ b/src/Web/Grand.Web.Admin/Startup/StartupApplication.cs@@ -1,10 +1,7 @@using elFinder.Net.AspNetCore.Extensions;using elFinder.Net.Drivers.FileSystem.Extensions;-using Grand.Domain.Catalog;using Grand.Infrastructure;using Grand.Web.Admin.Infrastructure;-using Grand.Web.AdminShared.Interfaces;-using Grand.Web.AdminShared.Services;using Grand.Web.Common.View;namespace Grand.Web.Admin.Startup;@@ -14,7 +11,9 @@ public class StartupApplication : IStartupApplicationpublic void ConfigureServices(IServiceCollection services, IConfiguration configuration){services.AddScoped<IAreaViewFactory, AdminAreaViewFactory>();- services.AddScoped<IAdminDataScope<Product>, GlobalAdminDataScope<Product>>();+ // IAdminDataScope<Product> is registered once, centrally, by Grand.Web.AdminShared's own+ // StartupApplication via RoutedProductDataScope - see its doc comment. Registering it here+ // too would race with Store's/Vendor's registrations under the combined Grand.Web host.}public void Configure(WebApplication application, IWebHostEnvironment webHostEnvironment)diff --git a/src/Web/Grand.Web.AdminShared/Services/RoutedProductDataScope.cs b/src/Web/Grand.Web.AdminShared/Services/RoutedProductDataScope.csnew file mode 100644index 000000000..0e9131504--- /dev/null+++ b/src/Web/Grand.Web.AdminShared/Services/RoutedProductDataScope.cs@@ -0,0 +1,63 @@+#nullable enable++using Grand.Domain.Catalog;+using Grand.Web.AdminShared.Interfaces;+using Microsoft.AspNetCore.Http;++namespace Grand.Web.AdminShared.Services;++/// <summary>+/// Resolves the correct per-host <see cref="IAdminDataScope{Product}" /> implementation at+/// request time, based on the current request's "area" route value, instead of relying on+/// DI registration order.+///+/// Why this exists: Grand.Web (the combined host, run as "grand-web" in Aspire) references+/// Grand.Web.Admin, Grand.Web.Store, and Grand.Web.Vendor together in one process/one DI+/// container. Each host's own StartupApplication used to register+/// AddScoped<IAdminDataScope<Product>, X>() directly - plain AddScoped doesn't replace an+/// earlier registration, it appends one, so whichever host's StartupApplication ran last (by+/// IStartupApplication.Priority - Vendor's is highest) silently won for every area in that+/// process. That surfaced as a NullReferenceException in VendorProductDataScope.DefaultVendorId+/// when an Admin user opened the product list under the combined host, because Vendor's scope+/// assumes WorkContext.CurrentVendor is set.+///+/// Fix: register the three concrete scopes as themselves (not as IAdminDataScope<Product>)+/// and register this resolver as the single IAdminDataScope<Product> - see+/// Grand.Web.AdminShared/Startup/StartupApplication.cs. Each host's own StartupApplication no+/// longer registers IAdminDataScope<Product> at all.+/// </summary>+public class RoutedProductDataScope(+ IHttpContextAccessor httpContextAccessor,+ GlobalAdminDataScope<Product> globalScope,+ StoreAdminDataScope<Product> storeScope,+ VendorProductDataScope vendorScope) : IAdminDataScope<Product>+{+ private IAdminDataScope<Product> Resolved+ {+ get+ {+ var area = httpContextAccessor.HttpContext?.Request.RouteValues["area"] as string;+ return area switch {+ "Vendor" => vendorScope,+ "Store" => storeScope,+ //covers "Admin" and the (unexpected) case of no area route value at all - same+ //fallback GlobalAdminDataScope always represented before this resolver existed+ _ => globalScope+ };+ }+ }++ public Task<bool> HasAccess(Product entity) => Resolved.HasAccess(entity);++ public Task<bool> CanView(Product entity) => Resolved.CanView(entity);++ public IQueryable<Product> ApplyScope(IQueryable<Product> query) => Resolved.ApplyScope(query);++ public string? DefaultStoreId => Resolved.DefaultStoreId;++ public string ResourceKeyPrefix => Resolved.ResourceKeyPrefix;++ public bool ShowStoreSelector => Resolved.ShowStoreSelector;++ public string? DefaultVendorId => Resolved.DefaultVendorId;+}diff --git a/src/Web/Grand.Web.AdminShared/Startup/StartupApplication.cs b/src/Web/Grand.Web.AdminShared/Startup/StartupApplication.csindex 38ec7c761..51937c430 100644--- a/src/Web/Grand.Web.AdminShared/Startup/StartupApplication.cs+++ b/src/Web/Grand.Web.AdminShared/Startup/StartupApplication.cs@@ -1,5 +1,6 @@using elFinder.Net.AspNetCore.Extensions;using elFinder.Net.Drivers.FileSystem.Extensions;+using Grand.Domain.Catalog;using Grand.Infrastructure;using Grand.Web.AdminShared.Interfaces;using Grand.Web.AdminShared.Services;@@ -58,6 +59,17 @@ public class StartupApplication : IStartupApplicationservices.AddScoped<IElFinderViewModelService, ElFinderViewModelService>();services.AddScoped<IMenuViewModelService, MenuViewModelService>();+ // IAdminDataScope<Product>: registered once here (not per-host) via a route-driven resolver.+ // Grand.Web (the combined host) references Admin, Store, and Vendor together in one DI+ // container, so three competing AddScoped<IAdminDataScope<Product>, X>() calls (one per host's+ // own StartupApplication) would just have the last-registered host silently win for every+ // area - see RoutedProductDataScope's doc comment for the NullReferenceException this caused.+ // The three concrete scopes are registered as themselves so the resolver can pick between them+ // per-request based on the "area" route value.+ services.AddScoped<GlobalAdminDataScope<Product>>();+ services.AddScoped<StoreAdminDataScope<Product>>();+ services.AddScoped<VendorProductDataScope>();+ services.AddScoped<IAdminDataScope<Product>, RoutedProductDataScope>();}public void Configure(WebApplication application, IWebHostEnvironment webHostEnvironment)diff --git a/src/Web/Grand.Web.Store/Startup/StartupApplication.cs b/src/Web/Grand.Web.Store/Startup/StartupApplication.csindex d7103717d..ff01045b7 100644--- a/src/Web/Grand.Web.Store/Startup/StartupApplication.cs+++ b/src/Web/Grand.Web.Store/Startup/StartupApplication.cs@@ -1,7 +1,4 @@-using Grand.Domain.Catalog;using Grand.Infrastructure;-using Grand.Web.AdminShared.Interfaces;-using Grand.Web.AdminShared.Services;namespace Grand.Web.Store.Startup;@@ -9,7 +6,9 @@ public class StartupApplication : IStartupApplication{public void ConfigureServices(IServiceCollection services, IConfiguration configuration){- services.AddScoped<IAdminDataScope<Product>, StoreAdminDataScope<Product>>();+ // IAdminDataScope<Product> is registered once, centrally, by Grand.Web.AdminShared's own+ // StartupApplication via RoutedProductDataScope - see its doc comment. Registering it here+ // too would race with Admin's/Vendor's registrations under the combined Grand.Web host.}public void Configure(WebApplication application, IWebHostEnvironment webHostEnvironment)diff --git a/src/Web/Grand.Web.Vendor/Startup/StartupApplication.cs b/src/Web/Grand.Web.Vendor/Startup/StartupApplication.csindex 510afb9ef..a0da106c1 100644--- a/src/Web/Grand.Web.Vendor/Startup/StartupApplication.cs+++ b/src/Web/Grand.Web.Vendor/Startup/StartupApplication.cs@@ -1,5 +1,4 @@using Grand.Data;-using Grand.Domain.Catalog;using Grand.Infrastructure;using Grand.Web.Vendor.Interfaces;using Grand.Web.Vendor.Services;@@ -13,7 +12,13 @@ public class StartupApplication : IStartupApplicationif (!DataSettingsManager.DatabaseIsInstalled())return;- services.AddScoped<Grand.Web.AdminShared.Interfaces.IAdminDataScope<Product>, Grand.Web.AdminShared.Services.VendorProductDataScope>();+ // IAdminDataScope<Product> is registered once, centrally, by Grand.Web.AdminShared's own+ // StartupApplication via RoutedProductDataScope - see its doc comment. A plain+ // AddScoped<IAdminDataScope<Product>, VendorProductDataScope>() here used to win for every+ // area under the combined Grand.Web host (Vendor's StartupApplication has the highest+ // Priority, so its registration ran last and silently overrode Admin's/Store's), causing a+ // NullReferenceException in VendorProductDataScope.DefaultVendorId whenever an Admin/Store+ // user opened the product list under that host.// IProductViewModelService is registered by Grand.Web.AdminShared's own StartupApplication// (Grand.Web.AdminShared/Startup/StartupApplication.cs), which is discovered and run for this// host too via the IStartupApplication assembly scan in StartupBase, since Vendor references