public override object SetHostExecutionContext(HostExecutionContext hostExecutionContext) { var castHostExecutionContext = hostExecutionContext as DnxHostExecutionContext; if (castHostExecutionContext != null) { object baseRevertParameter = null; if (castHostExecutionContext.BaseContext != null) { baseRevertParameter = base.SetHostExecutionContext(castHostExecutionContext.BaseContext); } // Capture the current status of the thread context and then update the values // The captured snapshot will be used to revert the status later var originalThreadContext = castHostExecutionContext.ThreadContext.CaptureAndReplace(Thread.CurrentThread); return((RevertAction)(() => { originalThreadContext.CaptureAndReplace(Thread.CurrentThread); if (baseRevertParameter != null) { base.Revert(baseRevertParameter); } })); } else { return(base.SetHostExecutionContext(hostExecutionContext)); } }
public static void BasicTest() { ThreadTestHelpers.RunTestInBackgroundThread(() => { var hecm = new HostExecutionContextManager(); Assert.Null(hecm.Capture()); Assert.Throws <InvalidOperationException>(() => hecm.SetHostExecutionContext(null)); var hec0 = new HostExecutionContext(); var hec1 = new HostExecutionContext(); object previousState0 = hecm.SetHostExecutionContext(hec0); ExecutionContext ec = ExecutionContext.Capture(); object previousState1 = hecm.SetHostExecutionContext(hec1); Assert.Throws <InvalidOperationException>(() => hecm.Revert(null)); Assert.Throws <InvalidOperationException>(() => hecm.Revert(new object())); Assert.Throws <InvalidOperationException>(() => hecm.Revert(previousState0)); object otherThreadState = null; ThreadTestHelpers.RunTestInBackgroundThread( () => otherThreadState = hecm.SetHostExecutionContext(new HostExecutionContext())); Assert.Throws <InvalidOperationException>(() => hecm.Revert(otherThreadState)); ExecutionContext.Run( ec, state => Assert.Throws <InvalidOperationException>(() => hecm.Revert(previousState1)), null); hecm.Revert(previousState1); Assert.Throws <InvalidOperationException>(() => hecm.Revert(previousState1)); // Revert always reverts to a null host execution context when it's not hosted Assert.Throws <InvalidOperationException>(() => hecm.Revert(previousState0)); }); }
public static void BasicTest() { ThreadTestHelpers.RunTestInBackgroundThread(() => { var hecm = new HostExecutionContextManager(); Assert.Null(hecm.Capture()); Assert.Throws<InvalidOperationException>(() => hecm.SetHostExecutionContext(null)); var hec0 = new HostExecutionContext(); var hec1 = new HostExecutionContext(); object previousState0 = hecm.SetHostExecutionContext(hec0); ExecutionContext ec = ExecutionContext.Capture(); object previousState1 = hecm.SetHostExecutionContext(hec1); Assert.Throws<InvalidOperationException>(() => hecm.Revert(null)); Assert.Throws<InvalidOperationException>(() => hecm.Revert(new object())); Assert.Throws<InvalidOperationException>(() => hecm.Revert(previousState0)); object otherThreadState = null; ThreadTestHelpers.RunTestInBackgroundThread( () => otherThreadState = hecm.SetHostExecutionContext(new HostExecutionContext())); Assert.Throws<InvalidOperationException>(() => hecm.Revert(otherThreadState)); ExecutionContext.Run( ec, state => Assert.Throws<InvalidOperationException>(() => hecm.Revert(previousState1)), null); hecm.Revert(previousState1); Assert.Throws<InvalidOperationException>(() => hecm.Revert(previousState1)); // Revert always reverts to a null host execution context when it's not hosted Assert.Throws<InvalidOperationException>(() => hecm.Revert(previousState0)); }); }
public HostExecutionContextSwitcher(HostExecutionContext currentContext) { _currentContext = currentContext; // Tie this instance with the current execution context for Revert validation (it must fail if an incompatible // execution context is applied to the thread) _asyncLocal = new AsyncLocal <bool>(); _asyncLocal.Value = true; }
public HostExecutionContextSwitcher(HostExecutionContext currentContext) { _currentContext = currentContext; // Tie this instance with the current execution context for Revert validation (it must fail if an incompatible // execution context is applied to the thread) _asyncLocal = new AsyncLocal<bool>(); _asyncLocal.Value = true; }
public override object SetHostExecutionContext(HostExecutionContext hostExecutionContext) { if (!(hostExecutionContext is WarpContext warp)) { return(new WarpContext()); } warp.SetManager(this); return(warp); }
public virtual object SetHostExecutionContext(HostExecutionContext hostExecutionContext) { if (hostExecutionContext == null) { throw new InvalidOperationException(SR.HostExecutionContextManager_InvalidOperation_NotNewCaptureContext); } var switcher = new HostExecutionContextSwitcher(hostExecutionContext); t_currentContext = hostExecutionContext; return switcher; }
public virtual object SetHostExecutionContext(HostExecutionContext hostExecutionContext) { if (hostExecutionContext == null) { throw new InvalidOperationException(SR.HostExecutionContextManager_InvalidOperation_NotNewCaptureContext); } var switcher = new HostExecutionContextSwitcher(hostExecutionContext); t_currentContext = hostExecutionContext; return(switcher); }
public override object SetHostExecutionContext(HostExecutionContext hostExecutionContext) { AspNetHostExecutionContext castHostExecutionContext = hostExecutionContext as AspNetHostExecutionContext; if (castHostExecutionContext != null) { // Call base.SetHostExecutionContext before calling our own logic. object baseRevertParameter = null; if (castHostExecutionContext.BaseContext != null) { baseRevertParameter = base.SetHostExecutionContext(castHostExecutionContext.BaseContext); } ThreadContext currentContext = ThreadContext.Current; if (currentContext != null && currentContext.HttpContext.ThreadContextId == castHostExecutionContext.HttpContextThreadContextId) { // If we reached this point, then 'castHostExecutionContext' was captured for the HttpContext // that is associated with the ThreadContext that is assigned to the current thread. We can // safely restore it. Action threadContextCleanupAction = currentContext.EnterExecutionContext(); // Perform cleanup in the opposite order from initialization. return((RevertAction)(() => { threadContextCleanupAction(); if (baseRevertParameter != null) { base.Revert(baseRevertParameter); } })); } else { // If we reached this point, then 'castHostExecutionContext' was captured by us // but is not applicable to the current thread. This can happen if the developer // called ThreadPool.QueueUserWorkItem, for example. We don't restore HttpContext // on such threads since they're not under the control of ASP.NET. In this case, // we have already called base.SetHostExecutionContext, so we just need to return // the result of that function directly to our caller. return(baseRevertParameter); } } else { // If we reached this point, then 'hostExecutionContext' was generated by our // base class instead of by us, so just delegate to the base implementation. return(base.SetHostExecutionContext(hostExecutionContext)); } }
public virtual void Revert(object previousState) { var switcher = previousState as HostExecutionContextSwitcher; if (switcher == null) { throw new InvalidOperationException( SR.HostExecutionContextManager_InvalidOperation_CannotOverrideSetWithoutRevert); } if (t_currentContext != switcher._currentContext || switcher._asyncLocal == null || !switcher._asyncLocal.Value) { throw new InvalidOperationException( SR.HostExecutionContextManager_InvalidOperation_CannotUseSwitcherOtherThread); } switcher._asyncLocal = null; // cannot be reused // Revert always reverts to a null host execution context when not hosted t_currentContext = null; }
public virtual object SetHostExecutionContext(HostExecutionContext hostExecutionContext) {}
public virtual object SetHostExecutionContext(HostExecutionContext hostExecutionContext) { }
private static HostExecutionContext CreateCopyHelper(HostExecutionContext hostExecutionContext) { return((hostExecutionContext != null) ? hostExecutionContext.CreateCopy() : null); }
internal DnxHostExecutionContext(HostExecutionContext baseContext, ThreadContext threadContext) { BaseContext = baseContext; ThreadContext = threadContext; }
private static HostExecutionContext CreateCopyHelper(HostExecutionContext hostExecutionContext) { // creating a copy of a null context should just itself return null return((hostExecutionContext != null) ? hostExecutionContext.CreateCopy() : null); }
internal AspNetHostExecutionContext(HostExecutionContext baseContext, object httpContextThreadContextId) { BaseContext = baseContext; HttpContextThreadContextId = httpContextThreadContextId; }