internal static ExecutionContextSwitcher SetExecutionContext(ExecutionContext executionContext) { StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; BCLDebug.Assert(executionContext != null, "ExecutionContext cannot be null here."); // Set up the switcher object to return; ExecutionContextSwitcher ecsw = new ExecutionContextSwitcher(); ecsw.thread = Thread.CurrentThread; ecsw.prevEC = Thread.CurrentThread.GetExecutionContextNoCreate(); // prev ecsw.currEC = executionContext; //current // Update the EC on thread Thread.CurrentThread.SetExecutionContext(executionContext); RuntimeHelpers.PrepareConstrainedRegions(); try { if (executionContext != null) { //set the security context SecurityContext sc = executionContext.SecurityContext; if (sc != null) { // non-null SC: needs to be set SecurityContext prevSeC = (ecsw.prevEC != null) ? ecsw.prevEC.SecurityContext : null; ecsw.scsw = SecurityContext.SetSecurityContext(sc, prevSeC, ref stackMark); } else if (!SecurityContext.CurrentlyInDefaultFTSecurityContext(ecsw.prevEC)) { // null incoming SC, but we're currently not in FT: use static FTSC to set SecurityContext prevSeC = (ecsw.prevEC != null) ? ecsw.prevEC.SecurityContext : null; ecsw.scsw = SecurityContext.SetSecurityContext(SecurityContext.FullTrustSecurityContext, prevSeC, ref stackMark); } // set the sync context SynchronizationContext syncContext = executionContext.SynchronizationContext; if (syncContext != null) { SynchronizationContext prevSyC = (ecsw.prevEC != null) ? ecsw.prevEC.SynchronizationContext : null; ecsw.sysw = SynchronizationContext.SetSynchronizationContext(syncContext, prevSyC); } // set the Host Context HostExecutionContext hostContext = executionContext.HostExecutionContext; if (hostContext != null) { ecsw.hecsw = HostExecutionContextManager.SetHostExecutionContextInternal(hostContext); } } } catch { ecsw.UndoNoThrow(); throw; } return(ecsw); }
internal static ExecutionContextSwitcher SetExecutionContext(ExecutionContext executionContext) { StackCrawlMark lookForMyCaller = StackCrawlMark.LookForMyCaller; ExecutionContextSwitcher switcher = new ExecutionContextSwitcher { thread = System.Threading.Thread.CurrentThread, prevEC = System.Threading.Thread.CurrentThread.GetExecutionContextNoCreate(), currEC = executionContext }; System.Threading.Thread.CurrentThread.SetExecutionContext(executionContext); RuntimeHelpers.PrepareConstrainedRegions(); try { if (executionContext == null) { return(switcher); } System.Security.SecurityContext securityContext = executionContext.SecurityContext; if (securityContext != null) { System.Security.SecurityContext prevSecurityContext = (switcher.prevEC != null) ? switcher.prevEC.SecurityContext : null; switcher.scsw = System.Security.SecurityContext.SetSecurityContext(securityContext, prevSecurityContext, ref lookForMyCaller); } else if (!System.Security.SecurityContext.CurrentlyInDefaultFTSecurityContext(switcher.prevEC)) { System.Security.SecurityContext context3 = (switcher.prevEC != null) ? switcher.prevEC.SecurityContext : null; switcher.scsw = System.Security.SecurityContext.SetSecurityContext(System.Security.SecurityContext.FullTrustSecurityContext, context3, ref lookForMyCaller); } System.Threading.SynchronizationContext synchronizationContext = executionContext.SynchronizationContext; if (synchronizationContext != null) { System.Threading.SynchronizationContext prevSyncContext = (switcher.prevEC != null) ? switcher.prevEC.SynchronizationContext : null; switcher.sysw = System.Threading.SynchronizationContext.SetSynchronizationContext(synchronizationContext, prevSyncContext); } System.Threading.HostExecutionContext hostExecutionContext = executionContext.HostExecutionContext; if (hostExecutionContext != null) { switcher.hecsw = HostExecutionContextManager.SetHostExecutionContextInternal(hostExecutionContext); } } catch { switcher.UndoNoThrow(); throw; } return(switcher); }
internal static ExecutionContextSwitcher SetExecutionContext(ExecutionContext executionContext, bool preserveSyncCtx) { StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; ExecutionContextSwitcher executionContextSwitcher = new ExecutionContextSwitcher(); Thread currentThread = Thread.CurrentThread; ExecutionContext.Reader executionContextReader = currentThread.GetExecutionContextReader(); executionContextSwitcher.thread = currentThread; executionContextSwitcher.outerEC = executionContextReader; executionContextSwitcher.outerECBelongsToScope = currentThread.ExecutionContextBelongsToCurrentScope; if (preserveSyncCtx) { executionContext.SynchronizationContext = executionContextReader.SynchronizationContext; } executionContext.SynchronizationContextNoFlow = executionContextReader.SynchronizationContextNoFlow; currentThread.SetExecutionContext(executionContext, true); RuntimeHelpers.PrepareConstrainedRegions(); try { ExecutionContext.OnAsyncLocalContextChanged(executionContextReader.DangerousGetRawExecutionContext(), executionContext); SecurityContext securityContext1 = executionContext.SecurityContext; if (securityContext1 != null) { SecurityContext.Reader securityContext2 = executionContextReader.SecurityContext; executionContextSwitcher.scsw = SecurityContext.SetSecurityContext(securityContext1, securityContext2, false, ref stackMark); } else if (!SecurityContext.CurrentlyInDefaultFTSecurityContext(executionContextSwitcher.outerEC)) { SecurityContext.Reader securityContext2 = executionContextReader.SecurityContext; executionContextSwitcher.scsw = SecurityContext.SetSecurityContext(SecurityContext.FullTrustSecurityContext, securityContext2, false, ref stackMark); } HostExecutionContext executionContext1 = executionContext.HostExecutionContext; if (executionContext1 != null) { executionContextSwitcher.hecsw = HostExecutionContextManager.SetHostExecutionContextInternal(executionContext1); } } catch { executionContextSwitcher.UndoNoThrow(); throw; } return(executionContextSwitcher); }
[HandleProcessCorruptedStateExceptions] // #endif // FEATURE_CORRUPTING_EXCEPTIONS internal static ExecutionContextSwitcher SetExecutionContext(ExecutionContext executionContext, bool preserveSyncCtx) { #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; #endif // #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK Contract.Assert(executionContext != null); Contract.Assert(executionContext != s_dummyDefaultEC); // Set up the switcher object to return; ExecutionContextSwitcher ecsw = new ExecutionContextSwitcher(); Thread currentThread = Thread.CurrentThread; ExecutionContext.Reader outerEC = currentThread.GetExecutionContextReader(); ecsw.thread = currentThread; ecsw.outerEC = outerEC; ecsw.outerECBelongsToScope = currentThread.ExecutionContextBelongsToCurrentScope; if (preserveSyncCtx) { executionContext.SynchronizationContext = outerEC.SynchronizationContext; } executionContext.SynchronizationContextNoFlow = outerEC.SynchronizationContextNoFlow; currentThread.SetExecutionContext(executionContext, belongsToCurrentScope: true); RuntimeHelpers.PrepareConstrainedRegions(); try { #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK //set the security context SecurityContext sc = executionContext.SecurityContext; if (sc != null) { // non-null SC: needs to be set SecurityContext.Reader prevSeC = outerEC.SecurityContext; ecsw.scsw = SecurityContext.SetSecurityContext(sc, prevSeC, false, ref stackMark); } else if (!SecurityContext.CurrentlyInDefaultFTSecurityContext(ecsw.outerEC)) { // null incoming SC, but we're currently not in FT: use static FTSC to set SecurityContext.Reader prevSeC = outerEC.SecurityContext; ecsw.scsw = SecurityContext.SetSecurityContext(SecurityContext.FullTrustSecurityContext, prevSeC, false, ref stackMark); } #endif // #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK #if FEATURE_CAS_POLICY // set the Host Context HostExecutionContext hostContext = executionContext.HostExecutionContext; if (hostContext != null) { ecsw.hecsw = HostExecutionContextManager.SetHostExecutionContextInternal(hostContext); } #endif // FEATURE_CAS_POLICY } catch { ecsw.UndoNoThrow(); throw; } return(ecsw); }