Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply IContextManager fix from 8.2.7 #4211

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions Source/Csla.Blazor.Test/AppContext/ContextManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IHttpContextAccessor, HttpContextAccessorFake>();
services.AddScoped<AuthenticationStateProvider, AuthenticationStateProviderFake>();
services.AddHttpContextAccessor();
services.AddCsla(o => o
.AddAspNetCore());
.AddAspNetCore()
.AddServerSideBlazor(o => o.UseInMemoryApplicationContextManager = true));
var serviceProvider = services.BuildServiceProvider();

var activeState = serviceProvider.GetRequiredService<AspNetCore.Blazor.ActiveCircuitState>();
activeState.CircuitExists = true;

var applicationContext = serviceProvider.GetRequiredService<ApplicationContext>();
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<AuthenticationStateProvider, AuthenticationStateProviderFake>();
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<AspNetCore.Blazor.ActiveCircuitState>();
activeState.CircuitExists = true;

var applicationContext = serviceProvider.GetRequiredService<ApplicationContext>();
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<AuthenticationStateProvider, AuthenticationStateProviderFake>();
services.AddHttpContextAccessor();
services.AddScoped<IHttpContextAccessor, HttpContextAccessorFake>();
services.AddCsla(o => o
.AddAspNetCore()
.AddServerSideBlazor(o => o.UseInMemoryApplicationContextManager = false));
var serviceProvider = services.BuildServiceProvider();

var activeState = serviceProvider.GetRequiredService<AspNetCore.Blazor.ActiveCircuitState>();
activeState.CircuitExists = true;
activeState.CircuitExists = false;

var applicationContext = serviceProvider.GetRequiredService<ApplicationContext>();
Assert.IsInstanceOfType(applicationContext.ContextManager, typeof(Csla.AspNetCore.Blazor.ApplicationContextManagerBlazor));
}

[TestMethod]
public void UseAspNetCoreApplicationContextManager()
{
var services = new ServiceCollection();
services.AddScoped<AuthenticationStateProvider, AuthenticationStateProviderFake>();
services.AddScoped<IHttpContextAccessor, HttpContextAccessorFake>();
services.AddCsla(o => o
.AddAspNetCore());
var serviceProvider = services.BuildServiceProvider();

var activeState = serviceProvider.GetRequiredService<AspNetCore.Blazor.ActiveCircuitState>();
activeState.CircuitExists = false;

var applicationContext = serviceProvider.GetRequiredService<ApplicationContext>();
Assert.IsInstanceOfType(applicationContext.ContextManager, typeof(Csla.AspNetCore.ApplicationContextManagerHttpContext));
}
}

Expand Down
7 changes: 4 additions & 3 deletions Source/Csla/Core/ApplicationContextAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Loading