Skip to content

Commit dfaf152

Browse files
Merge pull request #2487 from rockfordlhotka/2480-context
Flow all context values to logical server in all scenarios
2 parents 767a6ff + 79baf8b commit dfaf152

File tree

3 files changed

+32
-112
lines changed

3 files changed

+32
-112
lines changed

Source/Csla.Shared/DataPortalT.cs

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -29,47 +29,6 @@ public class DataPortal<T> : IDataPortal<T>
2929
/// </summary>
3030
public Csla.Core.ContextDictionary GlobalContext { get; set; }
3131

32-
private class DataPortalAsyncRequest
33-
{
34-
public object Argument { get; set; }
35-
public System.Security.Principal.IPrincipal Principal { get; set; }
36-
public Csla.Core.ContextDictionary ClientContext { get; set; }
37-
public Csla.Core.ContextDictionary GlobalContext { get; set; }
38-
public object UserState { get; set; }
39-
// passes CurrentCulture and CurrentUICulture to the async thread
40-
public CultureInfo CurrentCulture;
41-
public CultureInfo CurrentUICulture;
42-
43-
public DataPortalAsyncRequest(object argument, object userState)
44-
{
45-
this.Argument = argument;
46-
this.Principal = Csla.ApplicationContext.User;
47-
this.ClientContext = Csla.ApplicationContext.ClientContext;
48-
#pragma warning disable CS0618 // Type or member is obsolete
49-
this.GlobalContext = Csla.ApplicationContext.GlobalContext;
50-
#pragma warning restore CS0618 // Type or member is obsolete
51-
this.UserState = userState;
52-
this.CurrentCulture = System.Globalization.CultureInfo.CurrentCulture;
53-
this.CurrentUICulture = System.Globalization.CultureInfo.CurrentUICulture;
54-
}
55-
}
56-
57-
private class DataPortalAsyncResult
58-
{
59-
public T Result { get; set; }
60-
public Csla.Core.ContextDictionary GlobalContext { get; set; }
61-
public object UserState { get; set; }
62-
public Exception Error { get; set; }
63-
64-
public DataPortalAsyncResult(T result, Csla.Core.ContextDictionary globalContext, Exception error, object userState)
65-
{
66-
this.Result = result;
67-
this.GlobalContext = globalContext;
68-
this.UserState = userState;
69-
this.Error = error;
70-
}
71-
}
72-
7332
private async Task<object> DoCreateAsync(Type objectType, object criteria, bool isSync)
7433
{
7534
Server.DataPortalResult result = null;

Source/Csla.Shared/Server/DataPortal.cs

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -674,79 +674,53 @@ internal void Initialize(InterceptArgs e)
674674

675675
private void SetContext(DataPortalContext context)
676676
{
677+
// set the logical execution location
677678
_oldLocation = Csla.ApplicationContext.LogicalExecutionLocation;
678679
ApplicationContext.SetLogicalExecutionLocation(ApplicationContext.LogicalExecutionLocations.Server);
679680

680-
if (!context.IsRemotePortal && ApplicationContext.WebContextManager != null && !ApplicationContext.WebContextManager.IsValid)
681+
if (context.IsRemotePortal)
681682
{
682-
// local data portal and no valid HttpContext
683-
// if context already exists, then use existing context (from AsyncLocal or TLS)
684-
if (ApplicationContext.ClientContext == null || ApplicationContext.ClientContext.Count == 0)
685-
ApplicationContext.SetContext(context.ClientContext, context.GlobalContext);
683+
// indicate that the code is physically running on the server
684+
ApplicationContext.SetExecutionLocation(ApplicationContext.ExecutionLocations.Server);
686685
}
687686

688-
// if the dataportal is not remote then
689-
// do nothing
690-
if (!context.IsRemotePortal) return;
691-
692-
// set the context value so everyone knows the
693-
// code is running on the server
694-
ApplicationContext.SetExecutionLocation(ApplicationContext.ExecutionLocations.Server);
695-
696-
// set the app context to the value we got from the
697-
// client
687+
// set the app context to the value we got from the caller
698688
ApplicationContext.SetContext(context.ClientContext, context.GlobalContext);
699689

700-
// set the thread's culture to match the client
701-
#if !PCL46 && !PCL259// rely on NuGet bait-and-switch for actual implementation
702-
#if NETCORE
703-
System.Globalization.CultureInfo.CurrentCulture =
704-
new System.Globalization.CultureInfo(context.ClientCulture);
705-
System.Globalization.CultureInfo.CurrentUICulture =
706-
new System.Globalization.CultureInfo(context.ClientUICulture);
707-
#elif NETFX_CORE
708-
var list = new System.Collections.ObjectModel.ReadOnlyCollection<string>(new List<string> { context.ClientUICulture });
709-
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Languages = list;
710-
list = new System.Collections.ObjectModel.ReadOnlyCollection<string>(new List<string> { context.ClientCulture });
711-
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Languages = list;
712-
#else
713-
System.Threading.Thread.CurrentThread.CurrentCulture =
714-
new System.Globalization.CultureInfo(context.ClientCulture);
715-
System.Threading.Thread.CurrentThread.CurrentUICulture =
716-
new System.Globalization.CultureInfo(context.ClientUICulture);
717-
#endif
718-
#endif
690+
// set the thread's culture to match the caller
691+
SetCulture(context);
692+
693+
// set current user principal
694+
SetPrincipal(context);
695+
}
719696

697+
private static void SetPrincipal(DataPortalContext context)
698+
{
720699
if (ApplicationContext.AuthenticationType == "Windows")
721700
{
722701
// When using integrated security, Principal must be null
723702
if (context.Principal != null)
724-
{
725-
Csla.Security.SecurityException ex =
726-
new Csla.Security.SecurityException(Resources.NoPrincipalAllowedException);
727-
//ex.Action = System.Security.Permissions.SecurityAction.Deny;
728-
throw ex;
729-
}
730-
#if !(ANDROID || IOS) && !NETFX_CORE
703+
throw new Csla.Security.SecurityException(Resources.NoPrincipalAllowedException);
731704
// Set .NET to use integrated security
732705
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
733-
#endif
734706
}
735707
else
736708
{
737-
// We expect the some Principal object
709+
// We expect some Principal object
738710
if (context.Principal == null)
739-
{
740-
Csla.Security.SecurityException ex =
741-
new Csla.Security.SecurityException(
742-
Resources.BusinessPrincipalException + " Nothing");
743-
//ex.Action = System.Security.Permissions.SecurityAction.Deny;
744-
throw ex;
745-
}
711+
throw new Csla.Security.SecurityException(Resources.BusinessPrincipalException + " Nothing");
746712
ApplicationContext.User = context.Principal;
747713
}
748714
}
749715

716+
private static void SetCulture(DataPortalContext context)
717+
{
718+
System.Threading.Thread.CurrentThread.CurrentCulture =
719+
new System.Globalization.CultureInfo(context.ClientCulture);
720+
System.Threading.Thread.CurrentThread.CurrentUICulture =
721+
new System.Globalization.CultureInfo(context.ClientUICulture);
722+
}
723+
750724
private void ClearContext(DataPortalContext context)
751725
{
752726
ApplicationContext.SetLogicalExecutionLocation(_oldLocation);

Source/Csla.Shared/Server/DataPortalContext.cs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -106,27 +106,14 @@ public ObjectFactoryAttribute FactoryInfo
106106
/// <param name="isRemotePortal">Indicates whether the DataPortal is remote.</param>
107107
public DataPortalContext(IPrincipal principal, bool isRemotePortal)
108108
{
109-
if (isRemotePortal)
110-
{
111-
_principal = principal;
112-
_remotePortal = isRemotePortal;
113-
#if NETFX_CORE
114-
_clientCulture = System.Globalization.CultureInfo.CurrentCulture.Name;
115-
_clientUICulture = System.Globalization.CultureInfo.CurrentUICulture.Name;
116-
#else
117-
_clientCulture =
118-
System.Threading.Thread.CurrentThread.CurrentCulture.Name;
119-
_clientUICulture =
120-
System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
121-
#endif
122-
_clientContext = Csla.ApplicationContext.ContextManager.GetClientContext();
123-
_globalContext = Csla.ApplicationContext.ContextManager.GetGlobalContext();
124-
}
125-
else if (ApplicationContext.WebContextManager != null && ApplicationContext.WebContextManager.IsValid)
126-
{
127-
_clientContext = Csla.ApplicationContext.ContextManager.GetClientContext();
128-
_globalContext = Csla.ApplicationContext.ContextManager.GetGlobalContext();
129-
}
109+
_principal = principal;
110+
_remotePortal = isRemotePortal;
111+
_clientCulture =
112+
System.Threading.Thread.CurrentThread.CurrentCulture.Name;
113+
_clientUICulture =
114+
System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
115+
_clientContext = Csla.ApplicationContext.ContextManager.GetClientContext();
116+
_globalContext = Csla.ApplicationContext.ContextManager.GetGlobalContext();
130117
}
131118

132119
/// <summary>

0 commit comments

Comments
 (0)