Example #1
0
        internal void Execute()
        {
            CreateEnvironment();

            _completedSynchronouslyThreadId = Thread.CurrentThread.ManagedThreadId;
            try
            {
                _appContext.AppFunc(_env)
                // We can't use Then/Catch here because they would re-enter the sync context.
                // The async callback must be called outside of the sync context.
                .ContinueWith(appTask =>
                {
                    if (appTask.IsFaulted)
                    {
                        Complete(ErrorState.Capture(appTask.Exception));
                    }
                    else if (appTask.IsCanceled)
                    {
                        Complete(ErrorState.Capture(new TaskCanceledException(appTask)));
                    }
                    else
                    {
                        OnEnd();
                    }
                });
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                _completedSynchronouslyThreadId = Int32.MinValue;
            }
        }
        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);
        }
        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);
        }
Example #4
0
 public void OnEnd()
 {
     try
     {
         OnStart();
     }
     catch (Exception ex)
     {
         Complete(ErrorState.Capture(ex));
         return;
     }
     Complete();
 }
 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));
     }
 }
Example #6
0
        public IAsyncResult BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, object extraData)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException("httpContext");
            }

            try
            {
                OwinAppContext appContext = _appAccessor.Invoke();

                if (appContext == null)
                {
                    throw new EntryPointNotFoundException(Resources.Exception_NoOwinEntryPointFound);
                }

                // REVIEW: the httpContext.Request.RequestContext may be used here if public property unassigned?
                RequestContext requestContext  = _requestContext ?? new RequestContext(httpContext, new RouteData());
                string         requestPathBase = _pathBase;
                string         requestPath     = _requestPath ?? httpContext.Request.AppRelativeCurrentExecutionFilePath.Substring(1) + httpContext.Request.PathInfo;

                OwinCallContext callContext = appContext.CreateCallContext(
                    requestContext,
                    requestPathBase,
                    requestPath,
                    callback,
                    extraData);

                try
                {
                    callContext.Execute();
                }
                catch (Exception ex)
                {
                    callContext.AsyncResult.Complete(true, ErrorState.Capture(ex));
                }
                return(callContext.AsyncResult);
            }
            catch (Exception ex)
            {
                var failedAsyncResult = new CallContextAsyncResult(null, callback, extraData);
                failedAsyncResult.Complete(true, ErrorState.Capture(ex));
                return(failedAsyncResult);
            }
        }