CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
Csla/Core/ApplicationContextManager.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>Default context manager for the user property</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Security.Principal;
10using System.Threading;
11using Csla;
12using Microsoft.Extensions.DependencyInjection;
13
14namespace Csla.Core
15{
21 {
22 private AsyncLocal<ContextDictionary> _localContext = new AsyncLocal<ContextDictionary>();
23 private AsyncLocal<ContextDictionary> _clientContext = new AsyncLocal<ContextDictionary>();
24 private const string _globalContextName = "Csla.GlobalContext";
25
29 public bool IsValid
30 {
31 get { return true; }
32 }
33
38 public virtual IPrincipal GetUser()
39 {
40 IPrincipal result = Thread.CurrentPrincipal;
41 if (result == null)
42 {
43 result = new System.Security.Claims.ClaimsPrincipal();
44 SetUser(result);
45 }
46 return result;
47 }
48
53 public virtual void SetUser(IPrincipal principal)
54 {
55 Thread.CurrentPrincipal = principal;
56 }
57
62 {
63 return _localContext.Value;
64 }
65
70 public void SetLocalContext(ContextDictionary localContext)
71 {
72 _localContext.Value = localContext;
73 }
74
79 {
80 return _clientContext.Value;
81 }
82
87 public void SetClientContext(ContextDictionary clientContext)
88 {
89 _clientContext.Value = clientContext;
90 }
91
96 {
97 LocalDataStoreSlot slot = Thread.GetNamedDataSlot(_globalContextName);
98 return (ContextDictionary)Thread.GetData(slot);
99 }
100
105 public void SetGlobalContext(ContextDictionary globalContext)
106 {
107 LocalDataStoreSlot slot = Thread.GetNamedDataSlot(_globalContextName);
108 Thread.SetData(slot, globalContext);
109 }
110
111 private static IServiceProvider _provider;
112
116 public IServiceProvider GetDefaultServiceProvider()
117 {
118 return _provider;
119 }
120
125 public void SetDefaultServiceProvider(IServiceProvider serviceProvider)
126 {
127 _provider = serviceProvider;
128 }
129
134#pragma warning disable CS3002 // Return type is not CLS-compliant
135 public IServiceProvider GetServiceProvider()
136#pragma warning restore CS3002 // Return type is not CLS-compliant
137 {
138 return (IServiceProvider)ApplicationContext.LocalContext["__sps"] ?? GetDefaultServiceProvider();
139 }
140
145#pragma warning disable CS3001 // Argument type is not CLS-compliant
146 public void SetServiceProvider(IServiceProvider scope)
147#pragma warning restore CS3001 // Argument type is not CLS-compliant
148 {
149 Csla.ApplicationContext.LocalContext["__sps"] = scope;
150 }
151 }
152}
Default context manager for the user property and local/client/global context dictionaries.
void SetLocalContext(ContextDictionary localContext)
Sets the local context dictionary.
IServiceProvider GetDefaultServiceProvider()
Gets the default IServiceProvider
virtual IPrincipal GetUser()
Gets the current user principal.
virtual void SetUser(IPrincipal principal)
Sets the current user principal.
void SetDefaultServiceProvider(IServiceProvider serviceProvider)
Sets the default IServiceProvider
ContextDictionary GetGlobalContext()
Gets the global context dictionary.
ContextDictionary GetClientContext()
Gets the client context dictionary.
IServiceProvider GetServiceProvider()
Gets the service provider for current scope
bool IsValid
Returns a value indicating whether the context is valid.
void SetClientContext(ContextDictionary clientContext)
Sets the client context dictionary.
void SetGlobalContext(ContextDictionary globalContext)
Sets the global context dictionary.
void SetServiceProvider(IServiceProvider scope)
Sets the service provider for current scope
ContextDictionary GetLocalContext()
Gets the local context dictionary.
Dictionary type that is serializable with the SerializationFormatterFactory.GetFormatter().
Defines the interface for an application context manager type.