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.
ApplicationContext.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="ApplicationContext.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Provides consistent context information between the client</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Security.Principal;
10using Csla.Core;
12using Microsoft.Extensions.DependencyInjection;
13using System.Security.Claims;
14
15namespace Csla
16{
21 public static class ApplicationContext
22 {
23 #region Context Manager
24
25 private static IContextManager _contextManager;
26
27 internal static void SettingsChanged()
28 {
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;
39 }
40
41 private static IContextManager _webContextManager;
42 private static readonly Type _webManagerType;
43
44 static ApplicationContext()
45 {
46 Type _contextManagerType = null;
47 if (_contextManagerType == null)
48 _contextManagerType = Type.GetType("Csla.Windows.ApplicationContextManager, Csla.Windows");
49
50 if (_contextManagerType == null)
51 _contextManagerType = Type.GetType("Csla.Xaml.ApplicationContextManager, Csla.Xaml");
52
53 if (_contextManagerType != null)
54 _contextManager = (IContextManager)Activator.CreateInstance(_contextManagerType);
55
56 if (_contextManager == null)
57 _contextManager = new ApplicationContextManager();
58
59 if (_webManagerType == null)
60 {
61 _webManagerType = Type.GetType("Csla.Web.ApplicationContextManager, Csla.Web");
62 if (_webManagerType != null)
63 WebContextManager = (IContextManager)Activator.CreateInstance(_webManagerType);
64 }
65 }
66
75 public static IContextManager WebContextManager
76 {
77 get { return _webContextManager; }
78 set { _webContextManager = value; }
79 }
80
92 public static IContextManager ContextManager
93 {
94 get
95 {
96 if (WebContextManager != null && WebContextManager.IsValid)
97 return WebContextManager;
98 return _contextManager;
99 }
100 set { _contextManager = value; }
101 }
102
103 #endregion
104
105 #region User
106
111 public static ClaimsPrincipal Principal
112 {
113 get { return (ClaimsPrincipal)ContextManager.GetUser(); }
114 set { ContextManager.SetUser(value); }
115 }
116
127 public static IPrincipal User
128 {
129 get { return ContextManager.GetUser(); }
130 set { ContextManager.SetUser(value); }
131 }
132
133 #endregion
134
135 #region LocalContext
136
150 public static ContextDictionary LocalContext
151 {
152 get
153 {
154 ContextDictionary ctx = ContextManager.GetLocalContext();
155 if (ctx == null)
156 {
157 ctx = new ContextDictionary();
158 ContextManager.SetLocalContext(ctx);
159 }
160 return ctx;
161 }
162 }
163
164 #endregion
165
166 #region Client/Global Context
167
168 private static readonly object _syncContext = new object();
169
189 public static ContextDictionary ClientContext
190 {
191 get
192 {
193 lock (_syncContext)
194 {
195 ContextDictionary ctx = ContextManager.GetClientContext();
196 if (ctx == null)
197 {
198 ctx = new ContextDictionary();
199 ContextManager.SetClientContext(ctx);
200 }
201 return ctx;
202 }
203 }
204 }
205
220 [Obsolete("Use ClientContext", false)]
221 public static ContextDictionary GlobalContext
222 {
223 get
224 {
225 ContextDictionary ctx = ContextManager.GetGlobalContext();
226 if (ctx == null)
227 {
228 ctx = new ContextDictionary();
229 ContextManager.SetGlobalContext(ctx);
230 }
231 return ctx;
232 }
233 }
234
235 internal static void SetContext(
236 ContextDictionary clientContext,
237 ContextDictionary globalContext)
238 {
239 lock (_syncContext)
240 ContextManager.SetClientContext(clientContext);
241 ContextManager.SetGlobalContext(globalContext);
242 }
243
247 public static void Clear()
248 {
249 SetContext(null, null);
250 ContextManager.SetLocalContext(null);
251 }
252
253 #endregion
254
255 #region Settings
256
267 public static bool IsOffline { get; set; }
268
274 public static bool UseReflectionFallback { get; set; } = false;
275
276 private static Csla.Server.IDataPortalActivator _dataPortalActivator = null;
277 private static readonly object _dataPortalActivatorSync = new object();
278
282 public static Csla.Server.IDataPortalActivator DataPortalActivator
283 {
284 get
285 {
286 if (_dataPortalActivator == null)
287 {
288 lock (_dataPortalActivatorSync)
289 {
290 if (_dataPortalActivator == null)
291 {
292 var typeName = ConfigurationManager.AppSettings["CslaDataPortalActivator"];
293 if (!string.IsNullOrWhiteSpace(typeName))
294 {
295 var type = Type.GetType(typeName);
296 _dataPortalActivator = (Csla.Server.IDataPortalActivator)Reflection.MethodCaller.CreateInstance(type);
297 }
298 else
299 {
300 _dataPortalActivator = new Csla.Server.DefaultDataPortalActivator();
301 }
302 }
303 }
304 }
305 return _dataPortalActivator;
306 }
307 set
308 {
309 _dataPortalActivator = value;
310 }
311 }
312
313 private static string _dataPortalUrl = null;
314
320 public static string DataPortalUrlString
321 {
322 get
323 {
324 if (_dataPortalUrl == null)
325 {
326 _dataPortalUrl = ConfigurationManager.AppSettings["CslaDataPortalUrl"];
327 }
328 return _dataPortalUrl;
329 }
330 set
331 {
332 _dataPortalUrl = value;
333 }
334 }
335
336 private static string _VersionRoutingTag = null;
337
342 public static string VersionRoutingTag
343 {
344 get
345 {
346 if (string.IsNullOrWhiteSpace(_VersionRoutingTag))
347 _VersionRoutingTag = ConfigurationManager.AppSettings["CslaVersionRoutingTag"];
348 return _VersionRoutingTag;
349 }
350 internal set
351 {
352 if (!string.IsNullOrWhiteSpace(value))
353 if (value.Contains("-") || value.Contains("/"))
354 throw new ArgumentException("valueRoutingToken");
355 _VersionRoutingTag = value;
356 }
357 }
358
368 public static Uri DataPortalUrl
369 {
370 get { return new Uri(DataPortalUrlString); }
371 }
372
373 private static string _dataPortalProxyFactory;
395 public static string DataPortalProxyFactory
396 {
397 get
398 {
399 if (string.IsNullOrEmpty(_dataPortalProxyFactory))
400 {
401 _dataPortalProxyFactory = ConfigurationManager.AppSettings["CslaDataPortalProxyFactory"];
402 if (string.IsNullOrEmpty(_dataPortalProxyFactory))
403 _dataPortalProxyFactory = "Default";
404 }
405 return _dataPortalProxyFactory;
406 }
407 set
408 {
409 _dataPortalProxyFactory = value;
410 DataPortal.ResetProxyFactory();
411 }
412 }
413
414 private static string _authenticationTypeName;
415
429 public static string AuthenticationType
430 {
431 get
432 {
433 if (string.IsNullOrWhiteSpace(_authenticationTypeName))
434 _authenticationTypeName = ConfigurationManager.AppSettings["CslaAuthentication"];
435 if (string.IsNullOrWhiteSpace(_authenticationTypeName))
436 _authenticationTypeName = "Csla";
437 return _authenticationTypeName;
438 }
439 set { _authenticationTypeName = value; }
440 }
441
442 private static string _dataPortalProxy;
443
464 public static string DataPortalProxy
465 {
466 get
467 {
468 if (string.IsNullOrEmpty(_dataPortalProxy))
469 _dataPortalProxy = ConfigurationManager.AppSettings["CslaDataPortalProxy"];
470 if (string.IsNullOrEmpty(_dataPortalProxy))
471 _dataPortalProxy = "Local";
472 return _dataPortalProxy;
473 }
474 set
475 {
476 _dataPortalProxy = value;
477 DataPortal.ResetProxyType();
478 }
479 }
480
486 public static bool AutoCloneOnUpdate
487 {
488 get
489 {
490 bool result = true;
491 string setting = ConfigurationManager.AppSettings["CslaAutoCloneOnUpdate"];
492 if (!string.IsNullOrEmpty(setting))
493 result = bool.Parse(setting);
494 return result;
495 }
496 }
497
498 private static bool _dataPortalReturnObjectOnException = false;
499 private static bool _dataPortalReturnObjectOnExceptionSet = false;
500
507 public static bool DataPortalReturnObjectOnException
508 {
509 get
510 {
511 if (!_dataPortalReturnObjectOnExceptionSet)
512 {
513 string setting = ConfigurationManager.AppSettings["CslaDataPortalReturnObjectOnException"];
514 if (!string.IsNullOrEmpty(setting))
515 DataPortalReturnObjectOnException = bool.Parse(setting);
516 _dataPortalReturnObjectOnExceptionSet = true;
517 }
518 return _dataPortalReturnObjectOnException;
519 }
520 set
521 {
522 _dataPortalReturnObjectOnException = value;
523 _dataPortalReturnObjectOnExceptionSet = true;
524 }
525 }
526
530 public enum ExecutionLocations
531 {
535 Client,
539 Server
540 }
541
547 public static SerializationFormatters SerializationFormatter
548 {
549 get
550 {
551 var result = SerializationFormatters.CustomFormatter;
552
553 string tmp = ConfigurationManager.AppSettings["CslaSerializationFormatter"];
554 if (string.IsNullOrWhiteSpace(tmp))
555#if NETSTANDARD2_0 || NET5_0
556 tmp = "MobileFormatter";
557#else
558 tmp = "BinaryFormatter";
559#endif
560 if (Enum.TryParse(tmp, true, out SerializationFormatters serializationFormatter))
561 result = serializationFormatter;
562
563 return result;
564 }
565 }
566
571 public enum SerializationFormatters
572 {
573#if !NETSTANDARD2_0 && !NET5_0
579 NetDataContractSerializer,
580#endif
585 BinaryFormatter,
590 CustomFormatter,
594 MobileFormatter
595 }
596
597 private static PropertyChangedModes _propertyChangedMode = PropertyChangedModes.Xaml;
598 private static bool _propertyChangedModeSet;
603 public static PropertyChangedModes PropertyChangedMode
604 {
605 get
606 {
607 if (!_propertyChangedModeSet)
608 {
609 string tmp = ConfigurationManager.AppSettings["CslaPropertyChangedMode"];
610 if (string.IsNullOrEmpty(tmp))
611 tmp = "Xaml";
612 _propertyChangedMode = (PropertyChangedModes)
613 Enum.Parse(typeof(PropertyChangedModes), tmp);
614 _propertyChangedModeSet = true;
615 }
616 return _propertyChangedMode;
617 }
618 set
619 {
620 _propertyChangedMode = value;
621 _propertyChangedModeSet = true;
622 }
623 }
624
629 public enum PropertyChangedModes
630 {
635 Windows,
640 Xaml
641 }
642
643 private static ExecutionLocations _executionLocation =
644#if (ANDROID || IOS || NETFX_CORE) && !NETSTANDARD
645 ExecutionLocations.MobileClient;
646#else
647 ExecutionLocations.Client;
648#endif
649
654 public static ExecutionLocations ExecutionLocation
655 {
656 get { return _executionLocation; }
657 }
658
659 internal static void SetExecutionLocation(ExecutionLocations location)
660 {
661 _executionLocation = location;
662 }
663
667 public const string DefaultRuleSet = "default";
668
673 public static string RuleSet
674 {
675 get
676 {
677 var ruleSet = (string)ClientContext.GetValueOrNull("__ruleSet");
678 return string.IsNullOrEmpty(ruleSet) ? ApplicationContext.DefaultRuleSet : ruleSet;
679 }
680 set
681 {
682 ApplicationContext.ClientContext["__ruleSet"] = value;
683 }
684 }
685
686 private static TransactionIsolationLevel _transactionIsolationLevel = TransactionIsolationLevel.Unspecified;
687 private static bool _transactionIsolationLevelSet = false;
688
695 public static TransactionIsolationLevel DefaultTransactionIsolationLevel
696 {
697 get
698 {
699 if (!_transactionIsolationLevelSet)
700 {
701 string tmp = ConfigurationManager.AppSettings["CslaDefaultTransactionIsolationLevel"];
702 if (!string.IsNullOrEmpty(tmp))
703 {
704 _transactionIsolationLevel = (TransactionIsolationLevel)Enum.Parse(typeof(TransactionIsolationLevel), tmp);
705 }
706 _transactionIsolationLevelSet = true;
707 }
708 return _transactionIsolationLevel;
709 }
710 set
711 {
712 _transactionIsolationLevel = value;
713 _transactionIsolationLevelSet = true;
714 }
715 }
716
717 private static int _defaultTransactionTimeoutInSeconds = 600;
718 private static bool _defaultTransactionTimeoutInSecondsSet = false;
719
726 public static int DefaultTransactionTimeoutInSeconds
727 {
728 get
729 {
730 if (!_defaultTransactionTimeoutInSecondsSet)
731 {
732 var tmp = ConfigurationManager.AppSettings["CslaDefaultTransactionTimeoutInSeconds"];
733 _defaultTransactionTimeoutInSeconds = string.IsNullOrEmpty(tmp) ? 30 : int.Parse(tmp);
734 _defaultTransactionTimeoutInSecondsSet = true;
735 }
736 return _defaultTransactionTimeoutInSeconds;
737 }
738 set
739 {
740 _defaultTransactionTimeoutInSeconds = value;
741 _defaultTransactionTimeoutInSecondsSet = true;
742 }
743 }
744
745 private static System.Transactions.TransactionScopeAsyncFlowOption _defaultTransactionAsyncFlowOption;
746 private static bool _defaultTransactionAsyncFlowOptionSet;
747
752 public static System.Transactions.TransactionScopeAsyncFlowOption DefaultTransactionAsyncFlowOption
753 {
754 get
755 {
756 if (!_defaultTransactionAsyncFlowOptionSet)
757 {
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;
762 }
763 return _defaultTransactionAsyncFlowOption;
764 }
765 set
766 {
767 _defaultTransactionAsyncFlowOption = value;
768 _defaultTransactionAsyncFlowOptionSet = true;
769 }
770 }
771
772 #endregion
773
774 #region Logical Execution Location
780 public enum LogicalExecutionLocations
781 {
785 Client,
790 Server
791 }
792
797 public static LogicalExecutionLocations LogicalExecutionLocation
798 {
799 get
800 {
801 object location = LocalContext.GetValueOrNull("__logicalExecutionLocation");
802 if (location != null)
803 return (LogicalExecutionLocations)location;
804 else
805 return LogicalExecutionLocations.Client;
806 }
807 }
808
813 internal static void SetLogicalExecutionLocation(LogicalExecutionLocations location)
814 {
815 LocalContext["__logicalExecutionLocation"] = location;
816 }
817 #endregion
818
819 #region ServiceProvider
820
821 private static IServiceCollection _serviceCollection;
822
823 internal static void SetServiceCollection(IServiceCollection serviceCollection)
824 {
825 _serviceCollection = serviceCollection;
826 }
827
831 public static IServiceProvider DefaultServiceProvider
832 {
833 internal get
834 {
835 var result = ContextManager.GetDefaultServiceProvider();
836 if (result == null && _serviceCollection != null)
837 {
838 result = _serviceCollection.BuildServiceProvider();
839 _serviceCollection = null;
840 DefaultServiceProvider = result;
841 }
842 return result;
843 }
844 set => ContextManager.SetDefaultServiceProvider(value);
845 }
846
850#pragma warning disable CS3003 // Type is not CLS-compliant
851 public static IServiceProvider CurrentServiceProvider
852#pragma warning restore CS3003 // Type is not CLS-compliant
853 {
854 internal get
855 {
856 var result = ContextManager?.GetServiceProvider();
857 if (result == null)
858 {
859 var def = DefaultServiceProvider;
860 if (def != null)
861 {
862 result = CurrentServiceProvider = def.CreateScope().ServiceProvider;
863 }
864 }
865 return result;
866 }
867 set => ContextManager.SetServiceProvider(value);
868 }
869
870 #endregion
871 }
872}
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