From 2865c58118b0695d2e9b8feac0c6c6ba9303e3d3 Mon Sep 17 00:00:00 2001 From: Rockford Lhotka Date: Tue, 18 Jun 2024 22:04:15 -0500 Subject: [PATCH 1/2] Fix selection of custom ContextManager through CslaOptions --- Source/Csla/Configuration/ConfigurationExtensions.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Source/Csla/Configuration/ConfigurationExtensions.cs b/Source/Csla/Configuration/ConfigurationExtensions.cs index a95f2b83f0..163b1c5e23 100644 --- a/Source/Csla/Configuration/ConfigurationExtensions.cs +++ b/Source/Csla/Configuration/ConfigurationExtensions.cs @@ -1,4 +1,4 @@ -#if NET462_OR_GREATER || NETSTANDARD2_0 || NET8_0_OR_GREATER +#if NET462_OR_GREATER || NETSTANDARD2_0 || NET8_0_OR_GREATER //----------------------------------------------------------------------- // // Copyright (c) Marimer LLC. All rights reserved. @@ -82,7 +82,7 @@ private static void RegisterContextManager(IServiceCollection services) if (LoadContextManager(services, "Csla.Windows.Forms.ApplicationContextManager, Csla.Windows.Forms")) return; // default to AsyncLocal context manager - services.AddScoped(contextManagerType, typeof(Core.ApplicationContextManager)); + services.TryAddScoped(contextManagerType, typeof(Core.ApplicationContextManagerAsyncLocal)); } private static bool LoadContextManager(IServiceCollection services, string managerTypeName) @@ -90,11 +90,11 @@ private static bool LoadContextManager(IServiceCollection services, string manag var managerType = Type.GetType(managerTypeName, false); if (managerType != null) { - services.AddScoped(typeof(Core.IContextManager), managerType); + services.TryAddScoped(typeof(Core.IContextManager), managerType); return true; } return false; } } } -#endif \ No newline at end of file +#endif From 06c2ba5b9fbbe039b16ac02928e3af48fa0838fc Mon Sep 17 00:00:00 2001 From: Rockford lhotka Date: Wed, 19 Jun 2024 19:53:17 -0500 Subject: [PATCH 2/2] #4047 Change how IContextManager is set; ensure custom manager is used --- .../Csla.test/AppContext/AppContextTests.cs | 18 ++++++---- .../Configuration/ConfigurationExtensions.cs | 34 +++++++++---------- .../Csla/Configuration/Fluent/CslaOptions.cs | 14 ++++++-- 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/Source/Csla.test/AppContext/AppContextTests.cs b/Source/Csla.test/AppContext/AppContextTests.cs index d6d1f40368..0206b79630 100644 --- a/Source/Csla.test/AppContext/AppContextTests.cs +++ b/Source/Csla.test/AppContext/AppContextTests.cs @@ -10,6 +10,7 @@ using Csla.Core; using Csla.TestHelpers; using Microsoft.AspNetCore.Mvc.ModelBinding.Validation; +using Microsoft.Extensions.DependencyInjection; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace Csla.Test.AppContext @@ -31,7 +32,16 @@ public void Initialize() TestResults.Reinitialise(); } - #region Simple Test + [TestMethod] + public void UseCustomApplicationContext() + { + var services = new ServiceCollection(); + services.AddCsla(o => o.UseContextManager()); + var serviceProvider = services.BuildServiceProvider(); + + var applicationContext = serviceProvider.GetRequiredService(); + Assert.IsInstanceOfType(applicationContext.ContextManager, typeof(ApplicationContextManagerTls)); + } [TestMethod] public void SimpleTest() @@ -47,10 +57,6 @@ public void SimpleTest() Assert.AreEqual("Fetched", TestResults.GetResult("Root"), "global context missing server value"); } - #endregion - - #region ClientContext - /// /// Test the Client Context /// @@ -163,8 +169,6 @@ public void ContextDictionaryRemoveThrowsCorrectException() Assert.ThrowsException(() => contextDectionary.Remove(key)); } - #endregion - #region FailCreateContext /// diff --git a/Source/Csla/Configuration/ConfigurationExtensions.cs b/Source/Csla/Configuration/ConfigurationExtensions.cs index 163b1c5e23..949dc076c2 100644 --- a/Source/Csla/Configuration/ConfigurationExtensions.cs +++ b/Source/Csla/Configuration/ConfigurationExtensions.cs @@ -45,9 +45,7 @@ public static IServiceCollection AddCsla(this IServiceCollection services, Actio // ApplicationContext defaults services.AddScoped(); - RegisterContextManager(services); - if (cslaOptions.ContextManagerType != null) - services.AddScoped(typeof(Core.IContextManager), cslaOptions.ContextManagerType); + RegisterContextManager(services, cslaOptions.ContextManagerType); // Runtime Info defaults services.TryAddScoped(typeof(IRuntimeInfo), typeof(RuntimeInfo)); @@ -65,24 +63,26 @@ public static IServiceCollection AddCsla(this IServiceCollection services, Actio return services; } - private static void RegisterContextManager(IServiceCollection services) + private static void RegisterContextManager(IServiceCollection services, Type contextManagerType) { services.AddScoped(); services.TryAddScoped(typeof(Core.IContextManagerLocal), typeof(Core.ApplicationContextManagerAsyncLocal)); - var contextManagerType = typeof(Core.IContextManager); - - var managerInit = services.Any(i => i.ServiceType.Equals(contextManagerType)); - if (managerInit) return; - - if (LoadContextManager(services, "Csla.Blazor.WebAssembly.ApplicationContextManager, Csla.Blazor.WebAssembly")) return; - if (LoadContextManager(services, "Csla.Xaml.ApplicationContextManager, Csla.Xaml")) return; - if (LoadContextManager(services, "Csla.Web.Mvc.ApplicationContextManager, Csla.Web.Mvc")) return; - if (LoadContextManager(services, "Csla.Web.ApplicationContextManager, Csla.Web")) return; - if (LoadContextManager(services, "Csla.Windows.Forms.ApplicationContextManager, Csla.Windows.Forms")) return; + if (contextManagerType == null) + { + if (LoadContextManager(services, "Csla.Blazor.WebAssembly.ApplicationContextManager, Csla.Blazor.WebAssembly")) return; + if (LoadContextManager(services, "Csla.Xaml.ApplicationContextManager, Csla.Xaml")) return; + if (LoadContextManager(services, "Csla.Web.Mvc.ApplicationContextManager, Csla.Web.Mvc")) return; + if (LoadContextManager(services, "Csla.Web.ApplicationContextManager, Csla.Web")) return; + if (LoadContextManager(services, "Csla.Windows.Forms.ApplicationContextManager, Csla.Windows.Forms")) return; - // default to AsyncLocal context manager - services.TryAddScoped(contextManagerType, typeof(Core.ApplicationContextManagerAsyncLocal)); + // default to AsyncLocal context manager + services.AddScoped(typeof(Core.IContextManager), typeof(Core.ApplicationContextManagerAsyncLocal)); + } + else + { + services.AddScoped(typeof(Core.IContextManager), contextManagerType); + } } private static bool LoadContextManager(IServiceCollection services, string managerTypeName) @@ -90,7 +90,7 @@ private static bool LoadContextManager(IServiceCollection services, string manag var managerType = Type.GetType(managerTypeName, false); if (managerType != null) { - services.TryAddScoped(typeof(Core.IContextManager), managerType); + services.AddScoped(typeof(Core.IContextManager), managerType); return true; } return false; diff --git a/Source/Csla/Configuration/Fluent/CslaOptions.cs b/Source/Csla/Configuration/Fluent/CslaOptions.cs index b89c89c151..a2712f2beb 100644 --- a/Source/Csla/Configuration/Fluent/CslaOptions.cs +++ b/Source/Csla/Configuration/Fluent/CslaOptions.cs @@ -31,10 +31,20 @@ public CslaOptions(IServiceCollection services) public IServiceCollection Services { get; } /// - /// Gets or sets the type for the IContextManager to + /// Sets the type for the IContextManager to /// be used by ApplicationContext. /// - public Type ContextManagerType { get; set; } + public CslaOptions UseContextManager() where T : IContextManager + { + ContextManagerType = typeof(T); + return this; + } + + /// + /// Gets the type for the IContextManager + /// used by ApplicationContext. + /// + public Type ContextManagerType { get; private set; } /// /// Sets a value indicating whether CSLA