private IAsyncResult BeginFinalWork(object sender, EventArgs e, AsyncCallback cb, object extradata)
        {
            var result = new StageAsyncResult(cb, extradata, () => { });
            TaskCompletionSource <object> tcs = TakeLastCompletionSource();

            if (tcs != null)
            {
                tcs.TrySetResult(null);
            }
            if (_state.OriginalTask != null)
            {
                _state.OriginalTask
                .Then(() =>
                {
                    _state.CallContext.OnEnd();
                    CallContextAsyncResult.End(_state.CallContext.AsyncResult);
                    result.TryComplete();
                })
                .Catch(error =>
                {
                    result.Fail(ErrorState.Capture(error.Exception));
                    return(error.Handled());
                });
            }
            else
            {
                result.TryComplete();
            }
            result.InitialThreadReturning();
            return(result);
        }
Exemple #2
0
        public IAsyncResult BeginEvent(object sender, EventArgs e, AsyncCallback cb, object extradata)
        {
            if (_result != null)
            {
                throw new InvalidOperationException();
            }

            if (_context.PreventNextStage)
            {
                var noop = new StageAsyncResult(cb, extradata, () => { });
                noop.TryComplete();
                noop.InitialThreadReturning();
                return(noop);
            }

            _context.PreventNextStage = true;
            _responseShouldEnd        = true;
            _context.PushExecutingStage(this);

            Func <IDictionary <string, object>, Task> entryPoint = _stage.EntryPoint ?? _context.PrepareInitialContext((HttpApplication)sender);
            IDictionary <string, object>  environment            = _context.TakeLastEnvironment();
            TaskCompletionSource <object> tcs = _context.TakeLastCompletionSource();

            var result = new StageAsyncResult(cb, extradata, () =>
            {
                var application = ((HttpApplication)sender);

                if (_responseShouldEnd)
                {
                    application.CompleteRequest();
                }
            });

            _result = result;

            environment[Constants.IntegratedPipelineCurrentStage] = _stage.Name;

            try
            {
                entryPoint.Invoke(environment)
                .CopyResultToCompletionSource(tcs, null)
                .ContinueWith(t => result.TryComplete(), TaskContinuationOptions.ExecuteSynchronously)
                .Catch(ci => ci.Handled());
            }
            catch (Exception ex)
            {
                // Flow the exception back through the OWIN pipeline.
                tcs.TrySetException(ex);
                result.TryComplete();
                return(result);
            }

            result.InitialThreadReturning();
            return(result);
        }
        public IAsyncResult BeginEvent(object sender, EventArgs e, AsyncCallback cb, object extradata)
        {
            if (_result != null)
            {
                throw new InvalidOperationException();
            }

            if (_context.PreventNextStage)
            {
                var noop = new StageAsyncResult(cb, extradata, () => { });
                noop.TryComplete();
                noop.InitialThreadReturning();
                return noop;
            }

            _context.PreventNextStage = true;
            _responseShouldEnd = true;
            _context.PushExecutingStage(this);

            Func<IDictionary<string, object>, Task> entryPoint = _stage.EntryPoint ?? _context.PrepareInitialContext((HttpApplication)sender);
            IDictionary<string, object> environment = _context.TakeLastEnvironment();
            TaskCompletionSource<object> tcs = _context.TakeLastCompletionSource();

            var result = new StageAsyncResult(cb, extradata, () =>
            {
                var application = ((HttpApplication)sender);

                if (_responseShouldEnd)
                {
                    application.CompleteRequest();
                }
            });

            _result = result;

            environment[Constants.IntegratedPipelineCurrentStage] = _stage.Name;

            try
            {
                entryPoint.Invoke(environment)
                    .CopyResultToCompletionSource(tcs, null)
                    .ContinueWith(t => result.TryComplete(), TaskContinuationOptions.ExecuteSynchronously)
                    .Catch(ci => ci.Handled());
            }
            catch (Exception ex)
            {
                result.Fail(ErrorState.Capture(ex));
                tcs.TrySetException(ex);
                return result;
            }

            result.InitialThreadReturning();
            return result;
        }
Exemple #4
0
        private Task Epilog(IDictionary <string, object> env)
        {
            var tcs = new TaskCompletionSource <object>();

            _responseShouldEnd = false;
            _context.PushLastObjects(env, tcs);
            StageAsyncResult result = Interlocked.Exchange(ref _result, null);

            if (result != null)
            {
                result.TryComplete();
            }
            return(tcs.Task);
        }
 private async Task DoFinalWork(StageAsyncResult result)
 {
     try
     {
         await _state.OriginalTask;
         _state.CallContext.OnEnd();
         CallContextAsyncResult.End(_state.CallContext.AsyncResult);
         result.TryComplete();
     }
     catch (Exception ex)
     {
         _state.CallContext.AbortIfHeaderSent();
         result.Fail(ErrorState.Capture(ex));
     }
 }
 private async Task RunApp(AppFunc entryPoint, IDictionary<string, object> environment, TaskCompletionSource<object> tcs, StageAsyncResult result)
 {
     try
     {
         await entryPoint(environment);
         tcs.TrySetResult(null);
         result.TryComplete();
     }
     catch (Exception ex)
     {
         // Flow the exception back through the OWIN pipeline.
         tcs.TrySetException(ex);
         result.TryComplete();
     }
 }
Exemple #7
0
        public IAsyncResult BeginEvent(object sender, EventArgs e, AsyncCallback cb, object extradata)
        {
            if (_result != null)
            {
                throw new InvalidOperationException();
            }

            if (_context.PreventNextStage)
            {
                var noop = new StageAsyncResult(cb, extradata, () => { });
                noop.TryComplete();
                noop.InitialThreadReturning();
                return(noop);
            }

            _context.PreventNextStage = true;
            _responseShouldEnd        = true;
            _context.PushExecutingStage(this);

            AppFunc entryPoint = _stage.EntryPoint ?? _context.PrepareInitialContext((HttpApplication)sender);
            IDictionary <string, object>  environment = _context.TakeLastEnvironment();
            TaskCompletionSource <object> tcs         = _context.TakeLastCompletionSource();

            var result = new StageAsyncResult(cb, extradata, () =>
            {
                var application = ((HttpApplication)sender);

                if (_responseShouldEnd)
                {
                    application.CompleteRequest();
                }
            });

            _result = result;

            environment[Constants.IntegratedPipelineCurrentStage] = _stage.Name;

            // System.Web does not allow us to use async void methods to complete the IAsyncResult due to the special sync context.
            #pragma warning disable 4014
            RunApp(entryPoint, environment, tcs, result);
            #pragma warning restore 4014
            result.InitialThreadReturning();
            return(result);
        }
        public IAsyncResult BeginEvent(object sender, EventArgs e, AsyncCallback cb, object extradata)
        {
            if (_result != null)
            {
                throw new InvalidOperationException();
            }

            if (_context.PreventNextStage)
            {
                var noop = new StageAsyncResult(cb, extradata, () => { });
                noop.TryComplete();
                noop.InitialThreadReturning();
                return noop;
            }

            _context.PreventNextStage = true;
            _responseShouldEnd = true;
            _context.PushExecutingStage(this);

            AppFunc entryPoint = _stage.EntryPoint ?? _context.PrepareInitialContext((HttpApplication)sender);
            IDictionary<string, object> environment = _context.TakeLastEnvironment();
            TaskCompletionSource<object> tcs = _context.TakeLastCompletionSource();

            var result = new StageAsyncResult(cb, extradata, () =>
            {
                var application = ((HttpApplication)sender);

                if (_responseShouldEnd)
                {
                    application.CompleteRequest();
                }
            });

            _result = result;

            environment[Constants.IntegratedPipelineCurrentStage] = _stage.Name;

            // System.Web does not allow us to use async void methods to complete the IAsyncResult due to the special sync context.
            #pragma warning disable 4014
            RunApp(entryPoint, environment, tcs, result);
            #pragma warning restore 4014
            result.InitialThreadReturning();
            return result;
        }
        private IAsyncResult BeginFinalWork(object sender, EventArgs e, AsyncCallback cb, object extradata)
        {
            var result = new StageAsyncResult(cb, extradata, () => { });
            TaskCompletionSource <object> tcs = TakeLastCompletionSource();

            if (tcs != null)
            {
                tcs.TrySetResult(null);
            }
            if (_state.OriginalTask != null)
            {
                // System.Web does not allow us to use async void methods to complete the IAsyncResult due to the special sync context.
                #pragma warning disable 4014
                DoFinalWork(result);
                #pragma warning restore 4014
            }
            else
            {
                result.TryComplete();
            }
            result.InitialThreadReturning();
            return(result);
        }
 private void EndFinalWork(IAsyncResult ar)
 {
     Reset();
     StageAsyncResult.End(ar);
 }
 private async Task DoFinalWork(StageAsyncResult result)
 {
     try
     {
         await _state.OriginalTask;
         _state.CallContext.OnEnd();
         CallContextAsyncResult.End(_state.CallContext.AsyncResult);
         result.TryComplete();
     }
     catch (Exception ex)
     {
         _state.CallContext.AbortIfHeaderSent();
         result.Fail(ErrorState.Capture(ex));
     }
 }
 private IAsyncResult BeginFinalWork(object sender, EventArgs e, AsyncCallback cb, object extradata)
 {
     var result = new StageAsyncResult(cb, extradata, () => { });
     TaskCompletionSource<object> tcs = TakeLastCompletionSource();
     if (tcs != null)
     {
         tcs.TrySetResult(null);
     }
     if (_state.OriginalTask != null)
     {
         // System.Web does not allow us to use async void methods to complete the IAsyncResult due to the special sync context.
         #pragma warning disable 4014
         DoFinalWork(result);
         #pragma warning restore 4014
     }
     else
     {
         result.TryComplete();
     }
     result.InitialThreadReturning();
     return result;
 }
Exemple #13
0
 public void EndEvent(IAsyncResult ar)
 {
     StageAsyncResult.End(ar);
 }
Exemple #14
0
 public void Reset()
 {
     _result            = null;
     _responseShouldEnd = false;
 }
 private IAsyncResult BeginFinalWork(object sender, EventArgs e, AsyncCallback cb, object extradata)
 {
     var result = new StageAsyncResult(cb, extradata, () => { });
     TaskCompletionSource<object> tcs = TakeLastCompletionSource();
     if (tcs != null)
     {
         tcs.TrySetResult(null);
     }
     if (_state.OriginalTask != null)
     {
         _state.OriginalTask
             .Then(() =>
             {
                 _state.CallContext.OnEnd();
                 CallContextAsyncResult.End(_state.CallContext.AsyncResult);
                 result.TryComplete();
             })
             .Catch(error =>
             {
                 result.Fail(ErrorState.Capture(error.Exception));
                 return error.Handled();
             });
     }
     else
     {
         result.TryComplete();
     }
     result.InitialThreadReturning();
     return result;
 }
Exemple #16
0
        private async Task RunApp(AppFunc entryPoint, IDictionary <string, object> environment, TaskCompletionSource <object> tcs, StageAsyncResult result)
        {
            try
            {
                await entryPoint(environment);

                tcs.TrySetResult(null);
                result.TryComplete();
            }
            catch (Exception ex)
            {
                // Flow the exception back through the OWIN pipeline.
                tcs.TrySetException(ex);
                result.TryComplete();
            }
        }
 public void Reset()
 {
     _result = null;
     _responseShouldEnd = false;
 }