internal static ExecutionContext Capture(ref StackCrawlMark stackMark, ExecutionContext.CaptureOptions options) { ExecutionContext.Reader executionContextReader = Thread.CurrentThread.GetExecutionContextReader(); if (executionContextReader.IsFlowSuppressed) { return((ExecutionContext)null); } SecurityContext securityContext = SecurityContext.Capture(executionContextReader, ref stackMark); HostExecutionContext executionContext1 = HostExecutionContextManager.CaptureHostExecutionContext(); SynchronizationContext synchronizationContext = (SynchronizationContext)null; LogicalCallContext logicalCallContext = (LogicalCallContext)null; if (!executionContextReader.IsNull) { if ((options & ExecutionContext.CaptureOptions.IgnoreSyncCtx) == ExecutionContext.CaptureOptions.None) { synchronizationContext = executionContextReader.SynchronizationContext == null ? (SynchronizationContext)null : executionContextReader.SynchronizationContext.CreateCopy(); } if (executionContextReader.LogicalCallContext.HasInfo) { logicalCallContext = executionContextReader.LogicalCallContext.Clone(); } } Dictionary <IAsyncLocal, object> dictionary = (Dictionary <IAsyncLocal, object>)null; List <IAsyncLocal> asyncLocalList = (List <IAsyncLocal>)null; if (!executionContextReader.IsNull) { dictionary = executionContextReader.DangerousGetRawExecutionContext()._localValues; asyncLocalList = executionContextReader.DangerousGetRawExecutionContext()._localChangeNotifications; } if ((options & ExecutionContext.CaptureOptions.OptimizeDefaultCase) != ExecutionContext.CaptureOptions.None && securityContext == null && (executionContext1 == null && synchronizationContext == null) && ((logicalCallContext == null || !logicalCallContext.HasInfo) && (dictionary == null && asyncLocalList == null))) { return(ExecutionContext.s_dummyDefaultEC); } ExecutionContext executionContext2 = new ExecutionContext(); executionContext2.SecurityContext = securityContext; if (executionContext2.SecurityContext != null) { executionContext2.SecurityContext.ExecutionContext = executionContext2; } executionContext2._hostExecutionContext = executionContext1; executionContext2._syncContext = synchronizationContext; executionContext2.LogicalCallContext = logicalCallContext; executionContext2._localValues = dictionary; executionContext2._localChangeNotifications = asyncLocalList; executionContext2.isNewCapture = true; return(executionContext2); }
internal static ExecutionContext Capture(ref StackCrawlMark stackMark, ExecutionContext.CaptureOptions options) { ExecutionContext.Reader executionContextReader = Thread.CurrentThread.GetExecutionContextReader(); if (executionContextReader.IsFlowSuppressed) { return(null); } SecurityContext securityContext = SecurityContext.Capture(executionContextReader, ref stackMark); HostExecutionContext hostExecutionContext = HostExecutionContextManager.CaptureHostExecutionContext(); SynchronizationContext synchronizationContext = null; LogicalCallContext logicalCallContext = null; if (!executionContextReader.IsNull) { if ((options & ExecutionContext.CaptureOptions.IgnoreSyncCtx) == ExecutionContext.CaptureOptions.None) { synchronizationContext = ((executionContextReader.SynchronizationContext == null) ? null : executionContextReader.SynchronizationContext.CreateCopy()); } if (executionContextReader.LogicalCallContext.HasInfo) { logicalCallContext = executionContextReader.LogicalCallContext.Clone(); } } IAsyncLocalValueMap asyncLocalValueMap = null; IAsyncLocal[] array = null; if (!executionContextReader.IsNull) { asyncLocalValueMap = executionContextReader.DangerousGetRawExecutionContext()._localValues; array = executionContextReader.DangerousGetRawExecutionContext()._localChangeNotifications; } if ((options & ExecutionContext.CaptureOptions.OptimizeDefaultCase) != ExecutionContext.CaptureOptions.None && securityContext == null && hostExecutionContext == null && synchronizationContext == null && (logicalCallContext == null || !logicalCallContext.HasInfo) && asyncLocalValueMap == null && array == null) { return(ExecutionContext.s_dummyDefaultEC); } ExecutionContext executionContext = new ExecutionContext(); executionContext.SecurityContext = securityContext; if (executionContext.SecurityContext != null) { executionContext.SecurityContext.ExecutionContext = executionContext; } executionContext._hostExecutionContext = hostExecutionContext; executionContext._syncContext = synchronizationContext; executionContext.LogicalCallContext = logicalCallContext; executionContext._localValues = asyncLocalValueMap; executionContext._localChangeNotifications = array; executionContext.isNewCapture = true; return(executionContext); }
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); }