9using System.Security.Principal;
12using Microsoft.Extensions.DependencyInjection;
13using System.Security.Claims;
21 public static class ApplicationContext
23 #region Context Manager
27 internal static void SettingsChanged()
29 _dataPortalReturnObjectOnExceptionSet =
false;
30 _propertyChangedModeSet =
false;
31 _transactionIsolationLevelSet =
false;
32 _defaultTransactionTimeoutInSecondsSet =
false;
33 _authenticationTypeName =
null;
34 _dataPortalActivator =
null;
35 _dataPortalUrl =
null;
36 _dataPortalProxyFactory =
null;
37 _dataPortalProxy =
null;
38 _VersionRoutingTag =
null;
42 private static readonly Type _webManagerType;
44 static ApplicationContext()
46 Type _contextManagerType =
null;
47 if (_contextManagerType ==
null)
48 _contextManagerType = Type.GetType(
"Csla.Windows.ApplicationContextManager, Csla.Windows");
50 if (_contextManagerType ==
null)
51 _contextManagerType = Type.GetType(
"Csla.Xaml.ApplicationContextManager, Csla.Xaml");
53 if (_contextManagerType !=
null)
54 _contextManager = (
IContextManager)Activator.CreateInstance(_contextManagerType);
56 if (_contextManager ==
null)
59 if (_webManagerType ==
null)
61 _webManagerType = Type.GetType(
"Csla.Web.ApplicationContextManager, Csla.Web");
62 if (_webManagerType !=
null)
63 WebContextManager = (
IContextManager)Activator.CreateInstance(_webManagerType);
77 get {
return _webContextManager; }
78 set { _webContextManager = value; }
96 if (WebContextManager !=
null && WebContextManager.IsValid)
97 return WebContextManager;
98 return _contextManager;
100 set { _contextManager = value; }
111 public static ClaimsPrincipal Principal
113 get {
return (ClaimsPrincipal)ContextManager.
GetUser(); }
114 set { ContextManager.SetUser(value); }
127 public static IPrincipal User
129 get {
return ContextManager.GetUser(); }
130 set { ContextManager.SetUser(value); }
158 ContextManager.SetLocalContext(ctx);
166 #region Client/Global Context
168 private static readonly
object _syncContext =
new object();
199 ContextManager.SetClientContext(ctx);
220 [Obsolete(
"Use ClientContext",
false)]
229 ContextManager.SetGlobalContext(ctx);
235 internal static void SetContext(
240 ContextManager.SetClientContext(clientContext);
241 ContextManager.SetGlobalContext(globalContext);
247 public static void Clear()
249 SetContext(
null,
null);
250 ContextManager.SetLocalContext(
null);
267 public static bool IsOffline {
get;
set; }
274 public static bool UseReflectionFallback {
get;
set; } =
false;
277 private static readonly
object _dataPortalActivatorSync =
new object();
286 if (_dataPortalActivator ==
null)
288 lock (_dataPortalActivatorSync)
290 if (_dataPortalActivator ==
null)
292 var typeName = ConfigurationManager.AppSettings[
"CslaDataPortalActivator"];
293 if (!
string.IsNullOrWhiteSpace(typeName))
295 var type = Type.GetType(typeName);
305 return _dataPortalActivator;
309 _dataPortalActivator = value;
313 private static string _dataPortalUrl =
null;
320 public static string DataPortalUrlString
324 if (_dataPortalUrl ==
null)
326 _dataPortalUrl = ConfigurationManager.AppSettings[
"CslaDataPortalUrl"];
328 return _dataPortalUrl;
332 _dataPortalUrl = value;
336 private static string _VersionRoutingTag =
null;
342 public static string VersionRoutingTag
346 if (
string.IsNullOrWhiteSpace(_VersionRoutingTag))
347 _VersionRoutingTag = ConfigurationManager.AppSettings[
"CslaVersionRoutingTag"];
348 return _VersionRoutingTag;
352 if (!
string.IsNullOrWhiteSpace(value))
353 if (value.Contains(
"-") || value.Contains(
"/"))
354 throw new ArgumentException(
"valueRoutingToken");
355 _VersionRoutingTag = value;
368 public static Uri DataPortalUrl
370 get {
return new Uri(DataPortalUrlString); }
373 private static string _dataPortalProxyFactory;
395 public static string DataPortalProxyFactory
399 if (
string.IsNullOrEmpty(_dataPortalProxyFactory))
401 _dataPortalProxyFactory = ConfigurationManager.AppSettings[
"CslaDataPortalProxyFactory"];
402 if (
string.IsNullOrEmpty(_dataPortalProxyFactory))
403 _dataPortalProxyFactory =
"Default";
405 return _dataPortalProxyFactory;
409 _dataPortalProxyFactory = value;
410 DataPortal.ResetProxyFactory();
414 private static string _authenticationTypeName;
429 public static string AuthenticationType
433 if (
string.IsNullOrWhiteSpace(_authenticationTypeName))
434 _authenticationTypeName = ConfigurationManager.AppSettings[
"CslaAuthentication"];
435 if (
string.IsNullOrWhiteSpace(_authenticationTypeName))
436 _authenticationTypeName =
"Csla";
437 return _authenticationTypeName;
439 set { _authenticationTypeName = value; }
442 private static string _dataPortalProxy;
464 public static string DataPortalProxy
468 if (
string.IsNullOrEmpty(_dataPortalProxy))
469 _dataPortalProxy = ConfigurationManager.AppSettings[
"CslaDataPortalProxy"];
470 if (
string.IsNullOrEmpty(_dataPortalProxy))
471 _dataPortalProxy =
"Local";
472 return _dataPortalProxy;
476 _dataPortalProxy = value;
477 DataPortal.ResetProxyType();
486 public static bool AutoCloneOnUpdate
491 string setting = ConfigurationManager.AppSettings[
"CslaAutoCloneOnUpdate"];
492 if (!
string.IsNullOrEmpty(setting))
493 result =
bool.Parse(setting);
498 private static bool _dataPortalReturnObjectOnException =
false;
499 private static bool _dataPortalReturnObjectOnExceptionSet =
false;
507 public static bool DataPortalReturnObjectOnException
511 if (!_dataPortalReturnObjectOnExceptionSet)
513 string setting = ConfigurationManager.AppSettings[
"CslaDataPortalReturnObjectOnException"];
514 if (!
string.IsNullOrEmpty(setting))
515 DataPortalReturnObjectOnException =
bool.Parse(setting);
516 _dataPortalReturnObjectOnExceptionSet =
true;
518 return _dataPortalReturnObjectOnException;
522 _dataPortalReturnObjectOnException = value;
523 _dataPortalReturnObjectOnExceptionSet =
true;
530 public enum ExecutionLocations
547 public static SerializationFormatters SerializationFormatter
551 var result = SerializationFormatters.CustomFormatter;
553 string tmp = ConfigurationManager.AppSettings[
"CslaSerializationFormatter"];
554 if (
string.IsNullOrWhiteSpace(tmp))
555#if NETSTANDARD2_0 || NET5_0
556 tmp =
"MobileFormatter";
558 tmp =
"BinaryFormatter";
560 if (Enum.TryParse(tmp,
true, out SerializationFormatters serializationFormatter))
561 result = serializationFormatter;
571 public enum SerializationFormatters
573#if !NETSTANDARD2_0 && !NET5_0
579 NetDataContractSerializer,
597 private static PropertyChangedModes _propertyChangedMode = PropertyChangedModes.Xaml;
598 private static bool _propertyChangedModeSet;
603 public static PropertyChangedModes PropertyChangedMode
607 if (!_propertyChangedModeSet)
609 string tmp = ConfigurationManager.AppSettings[
"CslaPropertyChangedMode"];
610 if (
string.IsNullOrEmpty(tmp))
612 _propertyChangedMode = (PropertyChangedModes)
613 Enum.Parse(typeof(PropertyChangedModes), tmp);
614 _propertyChangedModeSet =
true;
616 return _propertyChangedMode;
620 _propertyChangedMode = value;
621 _propertyChangedModeSet =
true;
629 public enum PropertyChangedModes
643 private static ExecutionLocations _executionLocation =
644#if (ANDROID || IOS || NETFX_CORE) && !NETSTANDARD
645 ExecutionLocations.MobileClient;
647 ExecutionLocations.Client;
654 public static ExecutionLocations ExecutionLocation
656 get {
return _executionLocation; }
659 internal static void SetExecutionLocation(ExecutionLocations location)
661 _executionLocation = location;
667 public const string DefaultRuleSet =
"default";
673 public static string RuleSet
677 var ruleSet = (string)ClientContext.GetValueOrNull(
"__ruleSet");
678 return string.IsNullOrEmpty(ruleSet) ? ApplicationContext.DefaultRuleSet : ruleSet;
682 ApplicationContext.ClientContext[
"__ruleSet"] = value;
687 private static bool _transactionIsolationLevelSet =
false;
699 if (!_transactionIsolationLevelSet)
701 string tmp = ConfigurationManager.AppSettings[
"CslaDefaultTransactionIsolationLevel"];
702 if (!
string.IsNullOrEmpty(tmp))
706 _transactionIsolationLevelSet =
true;
708 return _transactionIsolationLevel;
712 _transactionIsolationLevel = value;
713 _transactionIsolationLevelSet =
true;
717 private static int _defaultTransactionTimeoutInSeconds = 600;
718 private static bool _defaultTransactionTimeoutInSecondsSet =
false;
726 public static int DefaultTransactionTimeoutInSeconds
730 if (!_defaultTransactionTimeoutInSecondsSet)
732 var tmp = ConfigurationManager.AppSettings[
"CslaDefaultTransactionTimeoutInSeconds"];
733 _defaultTransactionTimeoutInSeconds =
string.IsNullOrEmpty(tmp) ? 30 :
int.Parse(tmp);
734 _defaultTransactionTimeoutInSecondsSet =
true;
736 return _defaultTransactionTimeoutInSeconds;
740 _defaultTransactionTimeoutInSeconds = value;
741 _defaultTransactionTimeoutInSecondsSet =
true;
745 private static System.Transactions.TransactionScopeAsyncFlowOption _defaultTransactionAsyncFlowOption;
746 private static bool _defaultTransactionAsyncFlowOptionSet;
752 public static System.Transactions.TransactionScopeAsyncFlowOption DefaultTransactionAsyncFlowOption
756 if (!_defaultTransactionAsyncFlowOptionSet)
758 _defaultTransactionAsyncFlowOptionSet =
true;
759 var tmp = ConfigurationManager.AppSettings[
"CslaDefaultTransactionAsyncFlowOption"];
760 if (!Enum.TryParse<System.Transactions.TransactionScopeAsyncFlowOption>(tmp, out _defaultTransactionAsyncFlowOption))
761 _defaultTransactionAsyncFlowOption = System.Transactions.TransactionScopeAsyncFlowOption.Suppress;
763 return _defaultTransactionAsyncFlowOption;
767 _defaultTransactionAsyncFlowOption = value;
768 _defaultTransactionAsyncFlowOptionSet =
true;
774 #region Logical Execution Location
780 public enum LogicalExecutionLocations
797 public static LogicalExecutionLocations LogicalExecutionLocation
801 object location = LocalContext.GetValueOrNull(
"__logicalExecutionLocation");
802 if (location !=
null)
803 return (LogicalExecutionLocations)location;
805 return LogicalExecutionLocations.Client;
813 internal static void SetLogicalExecutionLocation(LogicalExecutionLocations location)
815 LocalContext[
"__logicalExecutionLocation"] = location;
819 #region ServiceProvider
821 private static IServiceCollection _serviceCollection;
823 internal static void SetServiceCollection(IServiceCollection serviceCollection)
825 _serviceCollection = serviceCollection;
831 public static IServiceProvider DefaultServiceProvider
835 var result = ContextManager.GetDefaultServiceProvider();
836 if (result ==
null && _serviceCollection !=
null)
838 result = _serviceCollection.BuildServiceProvider();
839 _serviceCollection =
null;
840 DefaultServiceProvider = result;
844 set => ContextManager.SetDefaultServiceProvider(value);
850#pragma warning disable CS3003
851 public static IServiceProvider CurrentServiceProvider
852#pragma warning restore CS3003
856 var result = ContextManager?.GetServiceProvider();
859 var def = DefaultServiceProvider;
862 result = CurrentServiceProvider = def.CreateScope().ServiceProvider;
867 set => ContextManager.SetServiceProvider(value);
Default context manager for the user property and local/client/global context dictionaries.
Dictionary type that is serializable with the SerializationFormatterFactory.GetFormatter().
Defines the interface for an application context manager type.
IPrincipal GetUser()
Gets the current principal.
Defines a type used to activate concrete business instances.
object CreateInstance(Type requestedType)
Gets a new instance of the requested type.
TransactionIsolationLevel
Specifies an isolation level for transactions controlled by TransactionalAttribute