Esempio n. 1
0
        internal RequestHandlerResult Execute()
        {
            //_request = request;
            RequestHandlerResult result = null;

            if (this.Policy != null)
            {
                this.Policy.Execute(() =>
                {
                    result = HandleRequest();
                });
            }
            else if (this.PolicyRequestHandler != null)
            {
                result = this.PolicyRequestHandler.Execute(() =>
                {
                    if (this.Context.Response != null && this.Context.Response.IsSuccess() == false && this.Context.Response.RequestHandlerId != this.RequestHandleId())
                    {
                        throw new PipelinePolicyException(this.Context.Response);
                    }

                    return(HandleRequest());
                });

                if (result.IsSuccess() == false)
                {
                    throw new PipelinePolicyException(this.Context.Response);
                }
            }
            else
            {
                result = HandleRequest();
            }

            return(result);
        }
Esempio n. 2
0
        public RequestHandlerResult Execute(TRequest request, string idempotencyKey)
        {
            if (this._validator != null)
            {
                var validateResult = this._validator.Validate(request);

                if (validateResult.IsValid == false)
                {
                    var errors = (validateResult.Errors.Select(p =>
                                                               new ErrorResult(null, p.ErrorMessage, p.PropertyName))).ToList();
                    return(new RequestHandlerResult(errors, 400));
                }
            }

            if (this._requestHandler == null)
            {
                throw new ArgumentNullException("No started handlers");
            }

            RequestHandlerResult result = null;

            var      lastRequestHandlerId = string.Empty;
            var      nextRequestHandlerId = string.Empty;
            TContext context = null;

            var hash = idempotencyKey == string.Empty ? request.GenerateHash() : idempotencyKey;

            if (this._useReuseRequisitionHash)
            {
                var snapshot = this._cacheProvider.Get <PipelineSnapshot>(hash).Result;
                if (snapshot != null)
                {
                    if (snapshot.Success)
                    {
                        result = snapshot.Context.Response;
                        result.SetStatusCode(200);
                        return(result);
                    }
                    else
                    {
                        context              = (TContext)snapshot.Context;
                        context.Request      = request;
                        nextRequestHandlerId = snapshot.LastRequestHandlerId;
                        this._requestHandler.UpdateContext(context);
                    }
                }
            }

            lastRequestHandlerId = Execute(request, nextRequestHandlerId, ref result);

            if (this._useReuseRequisitionHash)
            {
                var sucess   = result?.IsSuccess() ?? false;
                var snapshot = new PipelineSnapshot(sucess,
                                                    lastRequestHandlerId,
                                                    this._requestHandler.Context);

                this._cacheProvider.Add <PipelineSnapshot>(snapshot, hash);
            }
            return(result);
        }