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.
ApplicationContextManagerTls.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="ApplicationContextManagerTls.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;
11
12namespace Csla.Core
13{
21 {
22 private const string _localContextName = "Csla.LocalContext";
23 private const string _clientContextName = "Csla.ClientContext";
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 LocalDataStoreSlot slot = Thread.GetNamedDataSlot(_localContextName);
64 return (ContextDictionary)Thread.GetData(slot);
65 }
66
71 public void SetLocalContext(ContextDictionary localContext)
72 {
73 LocalDataStoreSlot slot = Thread.GetNamedDataSlot(_localContextName);
74 Thread.SetData(slot, localContext);
75 }
76
81 {
82 if (ApplicationContext.ExecutionLocation == ApplicationContext.ExecutionLocations.Client)
83 {
84 return (ContextDictionary)AppDomain.CurrentDomain.GetData(_clientContextName);
85 }
86 else
87 {
88 LocalDataStoreSlot slot = Thread.GetNamedDataSlot(_clientContextName);
89 return (ContextDictionary)Thread.GetData(slot);
90 }
91 }
92
97 public void SetClientContext(ContextDictionary clientContext)
98 {
99 if (ApplicationContext.ExecutionLocation == ApplicationContext.ExecutionLocations.Client)
100 {
101 AppDomain.CurrentDomain.SetData(_clientContextName, clientContext);
102 }
103 else
104 {
105 LocalDataStoreSlot slot = Thread.GetNamedDataSlot(_clientContextName);
106 Thread.SetData(slot, clientContext);
107 }
108 }
109
114 {
115 LocalDataStoreSlot slot = Thread.GetNamedDataSlot(_globalContextName);
116 return (ContextDictionary)Thread.GetData(slot);
117 }
118
123 public void SetGlobalContext(ContextDictionary globalContext)
124 {
125 LocalDataStoreSlot slot = Thread.GetNamedDataSlot(_globalContextName);
126 Thread.SetData(slot, globalContext);
127 }
128
129 private static IServiceProvider _provider;
130
134 public IServiceProvider GetDefaultServiceProvider()
135 {
136 return _provider;
137 }
138
143 public void SetDefaultServiceProvider(IServiceProvider serviceProvider)
144 {
145 _provider = serviceProvider;
146 }
147
152#pragma warning disable CS3002 // Return type is not CLS-compliant
153 public IServiceProvider GetServiceProvider()
154#pragma warning restore CS3002 // Return type is not CLS-compliant
155 {
156 return (IServiceProvider)ApplicationContext.LocalContext["__sps"] ?? GetDefaultServiceProvider();
157 }
158
163#pragma warning disable CS3001 // Argument type is not CLS-compliant
164 public void SetServiceProvider(IServiceProvider scope)
165#pragma warning restore CS3001 // Argument type is not CLS-compliant
166 {
167 Csla.ApplicationContext.LocalContext["__sps"] = scope;
168 }
169 }
170}
Context manager for the user property and local/client/global context dictionaries that uses thread l...
IServiceProvider GetDefaultServiceProvider()
Gets the default IServiceProvider
ContextDictionary GetGlobalContext()
Gets the global context dictionary.
void SetServiceProvider(IServiceProvider scope)
Sets the service provider for current scope
void SetLocalContext(ContextDictionary localContext)
Sets the local context dictionary.
virtual IPrincipal GetUser()
Gets the current user principal.
IServiceProvider GetServiceProvider()
Gets the service provider for current scope
virtual void SetUser(IPrincipal principal)
Sets the current user principal.
bool IsValid
Returns a value indicating whether the context is valid.
void SetDefaultServiceProvider(IServiceProvider serviceProvider)
Sets the default IServiceProvider
ContextDictionary GetLocalContext()
Gets the local context dictionary.
void SetClientContext(ContextDictionary clientContext)
Sets the client context dictionary.
void SetGlobalContext(ContextDictionary globalContext)
Sets the global context dictionary.
ContextDictionary GetClientContext()
Gets the client context dictionary.
Dictionary type that is serializable with the SerializationFormatterFactory.GetFormatter().
Defines the interface for an application context manager type.