internal void Undo() { if (this.thread == null) { return; } Thread thread = this.thread; if (this.hecsw != null) { HostExecutionContextSwitcher.Undo(this.hecsw); } ExecutionContext.Reader executionContextReader = thread.GetExecutionContextReader(); ExecutionContext.Reader reader = this.outerEC; int num = this.outerECBelongsToScope ? 1 : 0; thread.SetExecutionContext(reader, num != 0); if (this.scsw.currSC != null) { this.scsw.Undo(); } if (this.wiIsValid) { SecurityContext.RestoreCurrentWI(this.outerEC, executionContextReader, this.wi, this.cachedAlwaysFlowImpersonationPolicy); } this.thread = (Thread)null; ExecutionContext.OnAsyncLocalContextChanged(executionContextReader.DangerousGetRawExecutionContext(), this.outerEC.DangerousGetRawExecutionContext()); }
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); }