CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
ApplicationContextManagerAsyncLocal.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="ApplicationContextManager.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Application context manager using AsyncLocal</summary>
7//-----------------------------------------------------------------------
8using System.Security.Principal;
9using System.Threading;
10
11namespace Csla.Core
12{
18 {
19 private readonly AsyncLocal<ContextDictionary> _localContext = new();
20 private readonly AsyncLocal<ContextDictionary> _clientContext = new();
21 private readonly AsyncLocal<IPrincipal> _principal = new();
22
27 public bool IsStatefulContext => true;
28
32 public bool IsValid
33 {
34 get { return true; }
35 }
36
41 public virtual IPrincipal GetUser()
42 {
43 IPrincipal result = _principal.Value;
44 if (result == null)
45 {
46 result = new System.Security.Claims.ClaimsPrincipal();
47 SetUser(result);
48 }
49 return result;
50 }
51
56 public virtual void SetUser(IPrincipal principal)
57 {
58 _principal.Value = principal;
59 }
60
65 {
66 return _localContext.Value;
67 }
68
73 public void SetLocalContext(ContextDictionary localContext)
74 {
75 _localContext.Value = localContext;
76 }
77
83 {
84 return _clientContext.Value;
85 }
86
92 public void SetClientContext(ContextDictionary clientContext, ApplicationContext.ExecutionLocations executionLocation)
93 {
94 _clientContext.Value = clientContext;
95 }
96
97 private readonly AsyncLocal<ApplicationContext> _applicationContext = new();
98
103 {
104 get
105 {
106 return _applicationContext.Value;
107 }
108 set
109 {
110 _applicationContext.Value = value;
112 }
113 }
114
119 protected virtual void OnApplicationContextSet()
120 { }
121 }
122}
Provides consistent context information between the client and server DataPortal objects.
ExecutionLocations
Enum representing the locations code can execute.
Application context manager using AsyncLocal for user and context dictionaries.
virtual void OnApplicationContextSet()
Method called when the ApplicationContext property has been set to a new value.
bool IsStatefulContext
Gets a value indicating whether the context manager is stateful.
void SetLocalContext(ContextDictionary localContext)
Sets the local context dictionary.
ContextDictionary GetClientContext(ApplicationContext.ExecutionLocations executionLocation)
Gets the client context dictionary.
bool IsValid
Returns a value indicating whether the context is valid.
ContextDictionary GetLocalContext()
Gets the local context dictionary.
virtual IPrincipal GetUser()
Gets the current user principal.
void SetClientContext(ContextDictionary clientContext, ApplicationContext.ExecutionLocations executionLocation)
Sets the client context dictionary.
virtual void SetUser(IPrincipal principal)
Sets the current user principal.
Dictionary type that is serializable with the SerializationFormatterFactory.GetFormatter().
Subtype of IContextManager for use by LocalProxy.