public async Task ExecuteTasksAsync(object sender, EventArgs e, CancellationToken cancellationToken, AspNetSynchronizationContextBase syncContext, IRequestCompletedNotifier requestCompletedNotifier) { try { while (_registeredTasks.Count > 0) { // if canceled, propagate exception to caller and stop executing tasks cancellationToken.ThrowIfCancellationRequested(); // if request finished, stop executing tasks if (requestCompletedNotifier.IsRequestCompleted) { return; } // execute this task IPageAsyncTask task = _registeredTasks.Dequeue(); using (syncContext.AllowVoidAsyncOperationsBlock()) { await task.ExecuteAsync(sender, e, cancellationToken); } } } finally { _executeTasksAsyncHasCompleted = true; } }
// References should be nulled a.s.a.p. to reduce working set internal void ClearReferences() { _appInstance = null; _handler = null; _handlerStack = null; _currentHandler = null; _remapHandler = null; if (_isIntegratedPipeline) { if (!HasWebSocketRequestTransitionStarted) { // Items is also used by AspNetWebSocketContext and should only be cleared if we're not transitioning to WebSockets _items = null; } _syncContext = null; } }
internal void RestoreSavedAspNetSynchronizationContext(AspNetSynchronizationContextBase syncContext) { AsyncOperationManager.SynchronizationContext = syncContext; _syncContext = syncContext; }
internal AspNetSynchronizationContextBase InstallNewAspNetSynchronizationContext() { AspNetSynchronizationContextBase syncContext = _syncContext; if (syncContext != null && syncContext == AsyncOperationManager.SynchronizationContext) { // using current ASP.NET synchronization context - switch it _syncContext = CreateNewAspNetSynchronizationContext(); AsyncOperationManager.SynchronizationContext = _syncContext; return syncContext; } return null; }
// Associates this ThreadContext with the current thread. This restores certain // ambient values associated with the current HttpContext, such as the current // user and cultures. It also sets HttpContext.Current. internal void AssociateWithCurrentThread(bool setImpersonationContext) { Debug.Assert(HttpContext != null); // only to be used when context is available Debug.Assert(Current != this, "This ThreadContext is already associated with this thread."); Debug.Assert(!HasBeenDisassociatedFromThread, "This ThreadContext has already been disassociated from a thread."); Debug.Trace("OnThread", GetTraceMessage("Enter1")); /* * !! IMPORTANT !! * Keep this logic in [....] with DisassociateFromCurrentThread and EnterExecutionContext. */ // attach http context to the call context _originalHttpContext = DisposableHttpContextWrapper.SwitchContext(HttpContext); // set impersonation on the current thread if (setImpersonationContext) { SetImpersonationContext(); } // set synchronization context for the current thread to support the async pattern _originalSynchronizationContext = AsyncOperationManager.SynchronizationContext; AspNetSynchronizationContextBase aspNetSynchronizationContext = HttpContext.SyncContext; AsyncOperationManager.SynchronizationContext = aspNetSynchronizationContext; // set ETW trace ID Guid g = HttpContext.WorkerRequest.RequestTraceIdentifier; if (g != Guid.Empty) { CallContext.LogicalSetData("E2ETrace.ActivityID", g); } // set SqlDependecyCookie HttpContext.ResetSqlDependencyCookie(); // set principal on the current thread _originalThreadPrincipal = Thread.CurrentPrincipal; HttpApplication.SetCurrentPrincipalWithAssert(HttpContext.User); // only set culture on the current thread if it is not initialized SetRequestLevelCulture(); // DevDivBugs 75042 // set current thread in context if there is not there // the timeout manager uses this to abort the correct thread if (HttpContext.CurrentThread == null) { _setCurrentThreadOnHttpContext = true; HttpContext.CurrentThread = Thread.CurrentThread; } // Store a reference to the original ThreadContext.Current. It is possible that a parent // ThreadContext might already be associated with the current thread, e.g. if the current // stack contains a call to MgdIndicateCompletion (via // PipelineRuntime.ProcessRequestNotificationHelper). If this is the case, the child // ThreadContext will temporarily take over. _originalThreadContextCurrent = Current; Current = this; Debug.Trace("OnThread", GetTraceMessage("Enter2")); }
public AllowAsyncOperationsBlockDisposable(AspNetSynchronizationContextBase syncContext) { _syncContext = syncContext; }