Example #1
0
        public async Task Execute(IResult result, IServiceContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            if (result == null)
            {
                return;
            }

            var asyncResult = result as IResultAsync;

            if (asyncResult != null)
            {
                await asyncResult.ExecuteAsync(context, context.Response.GetCancellationToken());
            }
            else
            {
                result.Execute(context);
            }

            TryDisposeResult(result);
        }
Example #2
0
        public static ResultCompletionEventArgs ExecuteAndWait(this IResult action, CoroutineExecutionContext context,
                                                               System.Action executeAfterActionStarted = null)
        {
            ResultCompletionEventArgs retVal = null;
            var handle = new System.Threading.ManualResetEventSlim(false);

            action.Completed += (sender, args) =>
            {
                if (args == null)
                {
                    throw new Exception("Args = null");
                }
                retVal = args;
                handle.Set();
            };
            action.Execute(context);
            if (executeAfterActionStarted != null)
            {
                executeAfterActionStarted();
            }
            handle.Wait();

            if (retVal == null)
            {
                throw new Exception("Completed not triggered");
            }
            return(retVal);
        }
        public static void ShouldRaiseCompleted(this IResult result)
        {
            result.MonitorEvents();

            result.Execute(new ActionExecutionContext());

            result.ShouldRaise("Completed");
        }
Example #4
0
 /// <summary>
 ///   Executes a result with optional callback upon completion.
 /// </summary>
 /// <param name="result"> The result to execute </param>
 /// <param name="callback"> Optional callback </param>
 public static void Execute(this IResult result, Action <ResultCompletionEventArgs> callback = null)
 {
     if (callback != null)
     {
         result.Completed += (sender, args) => Caliburn.Micro.Execute.OnUIThread(() => callback(args));
     }
     result.Execute(null);
 }
Example #5
0
        public static void ExecuteSynchronized(this IResult result, ActionExecutionContext actionExecutionContext = null)
        {
            IoC.BuildUp(result);
            var reset = new ManualResetEvent(false);

            result.Completed += (s, e) => reset.Set();
            result.Execute(actionExecutionContext);
            reset.WaitOne();
        }
Example #6
0
 /// <summary>
 /// Executes the result using the specified context.
 /// </summary>
 /// <param name="context">The context.</param>
 public virtual void Execute(ActionExecutionContext context)
 {
     try {
         innerResult.Completed += InnerResultCompleted;
         IoC.BuildUp(innerResult);
         innerResult.Execute(context);
     }
     catch (Exception ex) {
         InnerResultCompleted(innerResult, new ResultCompletionEventArgs {
             Error = ex
         });
     }
 }
Example #7
0
        public void Start()
        {
            if (IsListening || _starting)
            {
                return;
            }

            _starting = true;

            try
            {
                if (!_hasRun)
                {
                    _router.Initialize();
                    _httpListener.Prefixes.Add(_uriBuilder.Uri.ToString());
                }

                _httpListener.Start();
                _httpListenerTask = Task.Factory.StartNew(async() =>
                {
                    while (IsListening)
                    {
                        HttpContext context = await _httpListener.GetContextAsync();
                        _logger.Information("Received Request {0} {1}", context.Request.HttpMethod,
                                            context.Request.Path);
                        try
                        {
                            IResult result = _router.Route(context);
                            result.Execute(context);
                        }
                        catch (Exception ex)
                        {
                            context.Response.StatusCode = HttpStatusCode.InternalServerError;
                            context.Response.Send();
                            _logger.Error("Internal Server Error ({0}): {1}", ex.GetType(), ex.Message);
                        }
                    }
                }, TaskCreationOptions.LongRunning);
            }
            catch (Exception ex)
            {
                _logger.Error($"RestFulServer: {ex.Message}\r\n{ex.StackTrace}");
            }
            finally
            {
                _starting = false;
                _hasRun   = true;
            }
        }
        /// <summary>
        /// Executes the result using the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public void Execute(CoroutineExecutionContext context)
        {
            this.context = context;

            try {
                innerResult.Completed += InnerResultCompleted;
                IoC.BuildUp(innerResult);
                innerResult.Execute(this.context);
            }
            catch (Exception ex) {
                InnerResultCompleted(innerResult, new ResultCompletionEventArgs {
                    Error = ex
                });
            }
        }
Example #9
0
        public static ResultCompletionEventArgs BlockingExecute(this IResult result, ActionExecutionContext context = null)
        {
            var wait = new AutoResetEvent(false);

            ResultCompletionEventArgs args = null;

            result.Completed += (sender, eventArgs) =>
            {
                args = eventArgs;
                wait.Set();
            };

            result.Execute(context);

            wait.WaitOne();

            return(args);
        }
Example #10
0
        private static Task <TResult> InternalExecuteAsync <TResult>(IResult result, CoroutineExecutionContext context)
        {
            var taskSource = new TaskCompletionSource <TResult>();

            EventHandler <ResultCompletionEventArgs> completed = null;

            completed = (s, e) =>
            {
                result.Completed -= completed;

                if (e.Error != null)
                {
                    taskSource.SetException(e.Error);
                }
                else if (e.WasCancelled)
                {
                    taskSource.SetCanceled();
                }
                else
                {
                    var rr = result as IResult <TResult>;
                    taskSource.SetResult(rr != null ? rr.Result : default(TResult));
                }
            };

            try
            {
                IoC.BuildUp(result);
                result.Completed += completed;
                result.Execute(context ?? new CoroutineExecutionContext());
            }
            catch (Exception ex)
            {
                result.Completed -= completed;
                taskSource.SetException(ex);
            }

            return(taskSource.Task);
        }
Example #11
0
        /// <summary>
        ///   Extension method to convert an IResult to <see cref="Task" />
        /// </summary>
        /// <param name="source"> The IResult to be converted. </param>
        /// <param name="context"> Optional execution context. </param>
        public static Task AsTask(this IResult source, CoroutineExecutionContext context = null)
        {
            var tcs = new TaskCompletionSource <bool>();

            source.Completed += (sender, args) =>
            {
                if (args.WasCancelled)
                {
                    tcs.SetCanceled();
                }
                else if (args.Error != null)
                {
                    tcs.SetException(args.Error);
                }
                else
                {
                    tcs.SetResult(true);
                }
            };
            source.Execute(context);
            return(tcs.Task);
        }
Example #12
0
 /// <inheritdoc />
 public Task Execute(IHttpContext context)
 {
     return(Result.Execute(context));
 }
 /// <summary>
 /// Allows to customize the dispatch of the execution
 /// </summary>
 protected virtual void Execute(IResult inner, CoroutineExecutionContext context)
 {
     inner.Execute(context);
 }
 protected override void Execute(IResult inner, CoroutineExecutionContext context)
 {
     ThreadPool.QueueUserWorkItem(state => { inner.Execute(context); });
 }
 protected override void Execute(IResult inner, CoroutineExecutionContext context)
 {
     ThreadPool.QueueUserWorkItem(state => { inner.Execute(context); });
 }
Example #16
0
 /// <summary>
 /// Allows to customize the dispatch of the execution
 /// </summary>
 protected virtual void Execute(IResult inner, ActionExecutionContext context)
 {
     inner.Execute(context);
 }
Example #17
0
 public virtual string DoWork()
 {
     return(result.Execute());
 }