From 833efced386528bd5bd364cb07b7df538aafbfb9 Mon Sep 17 00:00:00 2001 From: Rockford lhotka Date: Fri, 6 Sep 2024 15:14:32 -0500 Subject: [PATCH] #4208 Apply IContextManager fix from 8.2.7 --- .../AppContext/ContextManagerTests.cs | 41 ++++++++++++++----- .../Csla/Core/ApplicationContextAccessor.cs | 7 ++-- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/Source/Csla.Blazor.Test/AppContext/ContextManagerTests.cs b/Source/Csla.Blazor.Test/AppContext/ContextManagerTests.cs index 312d308032..b07675e9b1 100644 --- a/Source/Csla.Blazor.Test/AppContext/ContextManagerTests.cs +++ b/Source/Csla.Blazor.Test/AppContext/ContextManagerTests.cs @@ -23,53 +23,74 @@ namespace Csla.Blazor.Test.AppContext; public class ContextManagerTests { [TestMethod] - public void UseApplicationContextManagerAspNetCore() + public void UseApplicationContextManagerInMemory() { var services = new ServiceCollection(); - services.AddTransient(); + services.AddScoped(); + services.AddHttpContextAccessor(); services.AddCsla(o => o - .AddAspNetCore()); + .AddAspNetCore() + .AddServerSideBlazor(o => o.UseInMemoryApplicationContextManager = true)); var serviceProvider = services.BuildServiceProvider(); + var activeState = serviceProvider.GetRequiredService(); + activeState.CircuitExists = true; + var applicationContext = serviceProvider.GetRequiredService(); - Assert.IsInstanceOfType(applicationContext.ContextManager, typeof(Csla.AspNetCore.ApplicationContextManagerHttpContext)); + Assert.IsInstanceOfType(applicationContext.ContextManager, typeof(Csla.AspNetCore.Blazor.ApplicationContextManagerInMemory)); } [TestMethod] - public void UseApplicationContextManagerInMemory() + public void UseApplicationContextManagerBlazor() { var services = new ServiceCollection(); services.AddScoped(); services.AddHttpContextAccessor(); services.AddCsla(o => o .AddAspNetCore() - .AddServerSideBlazor(o => o.UseInMemoryApplicationContextManager = true)); + .AddServerSideBlazor(o => o.UseInMemoryApplicationContextManager = false)); var serviceProvider = services.BuildServiceProvider(); var activeState = serviceProvider.GetRequiredService(); activeState.CircuitExists = true; var applicationContext = serviceProvider.GetRequiredService(); - Assert.IsInstanceOfType(applicationContext.ContextManager, typeof(Csla.AspNetCore.Blazor.ApplicationContextManagerInMemory)); + Assert.IsInstanceOfType(applicationContext.ContextManager, typeof(Csla.AspNetCore.Blazor.ApplicationContextManagerBlazor)); } [TestMethod] - public void UseApplicationContextManagerBlazor() + public void UseBlazorApplicationContextManager() { var services = new ServiceCollection(); services.AddScoped(); - services.AddHttpContextAccessor(); + services.AddScoped(); services.AddCsla(o => o .AddAspNetCore() .AddServerSideBlazor(o => o.UseInMemoryApplicationContextManager = false)); var serviceProvider = services.BuildServiceProvider(); var activeState = serviceProvider.GetRequiredService(); - activeState.CircuitExists = true; + activeState.CircuitExists = false; var applicationContext = serviceProvider.GetRequiredService(); Assert.IsInstanceOfType(applicationContext.ContextManager, typeof(Csla.AspNetCore.Blazor.ApplicationContextManagerBlazor)); + } + + [TestMethod] + public void UseAspNetCoreApplicationContextManager() + { + var services = new ServiceCollection(); + services.AddScoped(); + services.AddScoped(); + services.AddCsla(o => o + .AddAspNetCore()); + var serviceProvider = services.BuildServiceProvider(); + var activeState = serviceProvider.GetRequiredService(); + activeState.CircuitExists = false; + + var applicationContext = serviceProvider.GetRequiredService(); + Assert.IsInstanceOfType(applicationContext.ContextManager, typeof(Csla.AspNetCore.ApplicationContextManagerHttpContext)); } } diff --git a/Source/Csla/Core/ApplicationContextAccessor.cs b/Source/Csla/Core/ApplicationContextAccessor.cs index 37c68e8e8a..e7bcec4284 100644 --- a/Source/Csla/Core/ApplicationContextAccessor.cs +++ b/Source/Csla/Core/ApplicationContextAccessor.cs @@ -31,11 +31,12 @@ public ApplicationContextAccessor( ServiceProvider = serviceProvider; LocalContextManager = localContextManager; - foreach (var context in contextManagerList) + var managers = contextManagerList.ToList(); + for (int i = managers.Count - 1; i >= 0; i--) { - if (context.IsValid) + if (managers[i].IsValid) { - ContextManager = context; + ContextManager = managers[i]; break; } }