Skip to content

Commit

Permalink
#4182 Fix context manager accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
rockfordlhotka committed Oct 2, 2024
1 parent 879648c commit c929053
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
36 changes: 27 additions & 9 deletions Source/Csla.AspNetCore/Blazor/ApplicationContextManagerBlazor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ private void AuthenticationStateProvider_AuthenticationStateChanged(Task<Authent
/// </summary>
public bool IsValid
{
get { return ActiveCircuitState.CircuitExists; }
get
{
var result = ActiveCircuitState.CircuitExists;
return result;
}
}

/// <summary>
Expand All @@ -137,19 +141,33 @@ public IPrincipal GetUser()
/// Not supported in Blazor.
/// </summary>
/// <param name="principal">Principal object.</param>
public virtual void SetUser(IPrincipal principal) { /* noop */ } // => throw new NotSupportedException(nameof(SetUser));
public virtual void SetUser(IPrincipal principal)
{
ArgumentNullException.ThrowIfNull(principal);
if (HttpContext != null)
{
HttpContext.User = (ClaimsPrincipal)principal;
}
else
{
throw new NotSupportedException(nameof(SetUser));
}
}

private const string _localContextName = "Csla.LocalContext";
private const string _clientContextName = "Csla.ClientContext";

/// <summary>
/// Gets the local context.
/// </summary>
/// <exception cref="InvalidOperationException"><see cref="ApplicationContext"/> is <see langword="null"/>.</exception>
public IContextDictionary GetLocalContext()
public IContextDictionary? GetLocalContext()
{
ThrowIfApplicationContextIsNull();
IContextDictionary localContext;
IContextDictionary? localContext;
var sessionManager = ApplicationContext.GetRequiredService<ISessionManager>();
var session = sessionManager.GetSession();
session.TryGetValue("localContext", out var result);
session.TryGetValue(_localContextName, out var result);
if (result is IContextDictionary context)
{
localContext = context;
Expand All @@ -172,7 +190,7 @@ public void SetLocalContext(IContextDictionary? localContext)
ThrowIfApplicationContextIsNull();
var sessionManager = ApplicationContext.GetRequiredService<ISessionManager>();
var session = sessionManager.GetSession();
session["localContext"] = localContext;
session[_localContextName] = localContext;
}

/// <summary>
Expand All @@ -186,7 +204,7 @@ public IContextDictionary GetClientContext(ApplicationContext.ExecutionLocations
IContextDictionary clientContext;
var sessionManager = ApplicationContext.GetRequiredService<ISessionManager>();
var session = sessionManager.GetSession();
session.TryGetValue("clientContext", out var result);
session.TryGetValue(_clientContextName, out var result);
if (result is IContextDictionary context)
{
clientContext = context;
Expand All @@ -202,15 +220,15 @@ public IContextDictionary GetClientContext(ApplicationContext.ExecutionLocations
/// <summary>
/// Sets the client context.
/// </summary>
/// <param name="clientContext">Client context.</param>
/// <param name="clientContext"></param>
/// <param name="executionLocation"></param>
/// <exception cref="InvalidOperationException"><see cref="ApplicationContext"/> is <see langword="null"/>.</exception>
public void SetClientContext(IContextDictionary? clientContext, ApplicationContext.ExecutionLocations executionLocation)
{
ThrowIfApplicationContextIsNull();
var sessionManager = ApplicationContext.GetRequiredService<ISessionManager>();
var session = sessionManager.GetSession();
session["clientContext"] = clientContext;
session[_clientContextName] = clientContext;
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion Source/Csla.AspNetCore/Blazor/State/SessionIdManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ public string GetSessionId()
else
{
result = Guid.NewGuid().ToString();
httpContext.Response.Cookies.Append(sessionIdName, result);
httpContext.Items[sessionIdName] = result;
if (!httpContext.Response.HasStarted)
httpContext.Response.Cookies.Append(sessionIdName, result);
}

return result ?? throw new InvalidOperationException(Csla.Properties.Resources.SessionIdManagerIdMustBeNotNull);
Expand Down
2 changes: 1 addition & 1 deletion Source/Csla.test/AppContext/AppContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public void Initialize()
}

[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void UseDefaultApplicationContextManager()
{
var services = new ServiceCollection();
Expand All @@ -45,6 +44,7 @@ public void UseDefaultApplicationContextManager()
var serviceProvider = services.BuildServiceProvider();

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

[TestMethod]
Expand Down
9 changes: 3 additions & 6 deletions Source/Csla/Core/ApplicationContextAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// </copyright>
// <summary>Provides access to the correct current application</summary>
//-----------------------------------------------------------------------
#nullable enable

using Csla.Runtime;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -40,14 +41,10 @@ public ApplicationContextAccessor(
break;
}
}
if (ContextManager is null)
{
throw new InvalidOperationException("ContextManager == null");
}
}

internal IServiceProvider ServiceProvider { get; }
private IContextManager ContextManager { get; set; }
private IContextManager? ContextManager { get; set; }
private IContextManager LocalContextManager { get; set; }

/// <summary>
Expand All @@ -57,7 +54,7 @@ public ApplicationContextAccessor(
public IContextManager GetContextManager()
{
var runtimeInfo = ServiceProvider.GetRequiredService<IRuntimeInfo>();
if (!runtimeInfo.LocalProxyNewScopeExists)
if (ContextManager != null && !runtimeInfo.LocalProxyNewScopeExists)
return ContextManager;
else
return LocalContextManager;
Expand Down

0 comments on commit c929053

Please sign in to comment.