Exemple #1
0
        public async Task <object> Invoke(CancellationToken token)
        {
            var stopwatch = new Stopwatch();

            var result = default(object);

            var workerData = _actionInvoker is IActionInvokerData data ? data.Data : default(object);

            try
            {
                stopwatch.Start();

                result = await _actionInvoker.Invoke(token);

                stopwatch.Stop();

                _progress.Report(new WorkerProgressData
                {
                    Duration = stopwatch.ElapsedMilliseconds,
                    Index    = _index,
                    Result   = result,
                    Data     = workerData
                });
            }
            catch (TaskCanceledException ex)
            {
                stopwatch.Stop();

                _progress.Report(new WorkerProgressData
                {
                    Duration    = stopwatch.ElapsedMilliseconds,
                    Index       = _index,
                    IsError     = true,
                    IsCancelled = true,
                    Data        = workerData,
                    Error       = ex
                });

                throw;
            }
            catch (Exception ex)
            {
                stopwatch.Stop();

                _progress.Report(new WorkerProgressData
                {
                    Duration = stopwatch.ElapsedMilliseconds,
                    Index    = _index,
                    IsError  = true,
                    Data     = workerData,
                    Error    = ex
                });

                throw;
            }

            return(result);
        }
Exemple #2
0
        private void OnMessage(IMessage message)
        {
            if (!message.IsResponse)
            {
                IActionInvoker invoker = this;

                invoker.Invoke(message).ContinueWith((t) => channelDispatcher.Post(t.Result));
            }
        }
Exemple #3
0
        public async Task <object> Invoke(TData data, CancellationToken token)
        {
            try
            {
                var result = await _actionInvoker.Invoke(data, token);

                return(result);
            }
            finally
            {
                _counterBlocked.Decremenet();
            }
        }
Exemple #4
0
        protected virtual async Task ProcessMessage(IActionInvoker actionInvoker, CancellationToken cancellationToken)
        {
            var stopwatch = Stopwatch.StartNew();

            await actionInvoker.Invoke(cancellationToken).ContinueWith(async actionResultTask =>
            {
                stopwatch.Stop();

                if (actionResultTask.IsCanceled || actionResultTask.IsFaulted)
                {
                    _workerHandler.HandleError(actionResultTask.Exception, stopwatch.ElapsedMilliseconds, actionResultTask.IsCanceled);
                }
                else
                {
                    var result = await actionResultTask;
                    _workerHandler.HandleResult(result, stopwatch.ElapsedMilliseconds);
                }
            });
        }
 /// <summary>
 /// 拦截方法的调用
 /// </summary>
 /// <param name="actionInvoker">action执行器</param>
 /// <param name="arguments">方法的参数集合</param>
 /// <returns></returns>
 public object Intercept(IActionInvoker actionInvoker, object?[] arguments)
 {
     return(actionInvoker.Invoke(this.context, arguments));
 }
Exemple #6
0
        /// <summary>
        /// Gets called by ASP.NET framework
        /// </summary>
        /// <param name="httpCtx">Instance of <see cref="HttpContext"/> provided by ASP.NET framework</param>
        /// <returns></returns>
        public async Task Invoke(HttpContext httpCtx)
        {
            httpCtx.SetLiteApiOptions(Options);

            ILogger log = new ContextAwareLogger(_isLoggingEnabled, _logger, httpCtx.TraceIdentifier);

            log.LogInformation($"Received request: {httpCtx.Request.Path} with query: {httpCtx.Request.QueryString.ToString() ?? ""}");

            if (Options.RequiresHttps && !httpCtx.Request.IsHttps)
            {
                log.LogInformation("LiteApi options are set to require HTTPS, request rejected because request is HTTP");
                httpCtx.Response.StatusCode = 400;
                await httpCtx.Response.WriteAsync("Bad request, HTTPS request was expected.");

                return;
            }

            if (Options.DiscoveryEnabled)
            {
                bool handledOnDiscovery = await _discoveryHandler.HandleIfNeeded(httpCtx);

                if (handledOnDiscovery)
                {
                    return;
                }
            }

            ActionContext action = _pathResolver.ResolveAction(httpCtx.Request, log);

            if (action == null)
            {
                log.LogInformation("Request is skipped to next middleware");
                if (_next != null)
                {
                    log.LogInformation("Invoking next middleware");
                    await _next?.Invoke(httpCtx);

                    log.LogInformation("Next middleware invoked");
                }
                else
                {
                    log.LogInformation("There is no next middleware to invoke");
                }
            }
            else
            {
                // TODO: consider replacing RequiresHttps with global filter
                httpCtx.SetActionContext(action);

                if (!(await CheckGlobalFiltersAndWriteResponseIfAny(httpCtx, log, action.SkipAuth)))
                {
                    return;
                }

                await _actionInvoker.Invoke(httpCtx, action, log);

                log.LogInformation("Action is invoked");
            }

            log.LogInformation("Request is processed");
        }
Exemple #7
0
 protected virtual async Task <IMessage> InvokeAsync(IMessage message, IActionInvoker invoker)
 {
     return(await invoker.Invoke(message));
 }
Exemple #8
0
 public Task <IMessage> APICall(IMessage message, IActionInvoker invoker)
 {
     return(Task.Run(async() => await invoker.Invoke(message)));
 }
 public void Invoke(Type[] valueTypes, object[] values)
 {
     _invoker.Invoke(valueTypes, values);
 }
Exemple #10
0
        public virtual Task <HttpResponseMessage> Invoke()
        {
            var handler = _container.GetInstance(_actionMethod.HandlerTypeDescriptor.Type);

            return(_actionInvoker.Invoke(handler));
        }
Exemple #11
0
        public virtual Task <HttpResponseMessage> Invoke()
        {
            var handler = _container.GetInstance(_requestContext.Action.HandlerType.Type);

            return(_actionInvoker.Invoke(handler));
        }
 public virtual async Task <object> Invoke(CancellationToken token)
 {
     return(await _actionInvoker.Invoke(_data, token));
 }