public OperationsController(OperationService operationService, IOperationTransient transientOperation, IOperationScoped scopedOperation, IOperationSingleton singletonOperation, IOperationSingletonInstance singletonInstanceOperation, IRequestEntry requestEntryMethod, IMemoryCache memoryCache)
 {
     this._operationService           = operationService;
     this._transientOperation         = transientOperation;
     this._scopedOperation            = scopedOperation;
     this._singletonOperation         = singletonOperation;
     this._singletonInstanceOperation = singletonInstanceOperation;
     this._requestEntryMethod         = requestEntryMethod;
     this._memoryCache = memoryCache;
 }
Beispiel #2
0
        Task <HttpResponseMessage> SendAsync(IRequest request, IRequestEntry nextALS, CancellationToken token)
        {
            var requestUri = this.ApiResolveService.Resolve(request.App, request.ApiName);
            HttpRequestMessage req;

            if (request.Method == HttpMethod.Post || request.Method == HttpMethod.Put)
            {
                req = new HttpRequestMessage()
                {
                    Method     = request.Method,
                    Content    = new StringContent(request.ToJson(), Encoding.UTF8, "application/json"),
                    RequestUri = requestUri
                };
            }
            else
            {
                var uri = requestUri;
                System.Text.StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append(uri.Query);
                if (uri.Query.IsNotNullOrEmpty())
                {
                    stringBuilder.Append("&");
                }
                else
                {
                    stringBuilder.Append("?");
                }

                request.GetType().GetProperties()
                .Each(p =>
                {
                    stringBuilder.Append($"{System.Web.HttpUtility.UrlEncode(p.Name)}={System.Web.HttpUtility.UrlEncode(p.GetValue(request)?.ToString())}&");
                });

                var u = new UriBuilder(uri);
                u.Query = stringBuilder.ToString();

                req = new HttpRequestMessage()
                {
                    Method     = request.Method,
                    RequestUri = u.Uri
                };
            }

            if (nextALS != null)
            {
                req.Headers.TryAddWithoutValidation(RestApiConst.TraceId, nextALS.TraceId);
                req.Headers.TryAddWithoutValidation(RestApiConst.RequestDepth, nextALS.TraceDepth);
                req.Headers.TryAddWithoutValidation(RestApiConst.UserOpenId, nextALS.UserOpenId);
                req.Headers.TryAddWithoutValidation(RestApiConst.UserTraceId, nextALS.UserTraceId);
                req.Headers.TryAddWithoutValidation(RestApiConst.AccessToken, nextALS.AccessToken);
                req.Headers.TryAddWithoutValidation(RestApiConst.RealIp, nextALS.RealIp);
            }
            return(httpClient.SendAsync(req, token));
        }
Beispiel #3
0
        async Task <(bool isSuccess, string body)> ReadAsync(IRequest request, IResponseBase resObj, CancellationToken token)
        {
            IRequestEntry nextALS = null;

            try
            {
                if (this.TraceClient != null && this.TraceClient.Current != null)
                {
                    var als = this.TraceClient.Current;
                    nextALS = als.NewChildRequest();
                }

                var res = await ReadInternalAsync(request, nextALS, resObj, token);

                if (res.isSuccess)
                {
                    return(true, res.body);
                }
                else
                {
                    nextALS.Exception = new BusinessException(this.ApiResolveService.Resolve(request.App, request.ApiName).ToString() + resObj.msg, resObj.resCode);
                }
            }
            catch (Exception ex)
            {
                if (nextALS != null)
                {
                    nextALS.Exception = ex;
                }
            }
            finally
            {
                if (nextALS != null)
                {
                    nextALS.EndTime = DateTime.Now;
                }
            }
            return(false, null);
        }
Beispiel #4
0
        public Task <HttpResponseMessage> SendAsync(HttpRequestMessage httpRequestMessage, CancellationToken token)
        {
            IRequestEntry nextALS = null;

            if (this.TraceClient != null && this.TraceClient.Current != null)
            {
                var als = this.TraceClient.Current;
                nextALS = als.NewChildRequest();
            }

            if (nextALS != null)
            {
                httpRequestMessage.Headers.TryAddWithoutValidation(RestApiConst.TraceId, nextALS.TraceId);
                httpRequestMessage.Headers.TryAddWithoutValidation(RestApiConst.RequestDepth, nextALS.TraceDepth);
                httpRequestMessage.Headers.TryAddWithoutValidation(RestApiConst.UserOpenId, nextALS.UserOpenId);
                httpRequestMessage.Headers.TryAddWithoutValidation(RestApiConst.UserTraceId, nextALS.UserTraceId);
                httpRequestMessage.Headers.TryAddWithoutValidation(RestApiConst.AccessToken, nextALS.AccessToken);
                httpRequestMessage.Headers.TryAddWithoutValidation(RestApiConst.RealIp, nextALS.RealIp);
            }
            return(httpClient.SendAsync(httpRequestMessage, token).ContinueWith(resTask =>
            {
                if (nextALS != null)
                {
                    nextALS.EndTime = DateTime.Now;
                }

                if (resTask.IsFaulted)
                {
                    if (nextALS != null)
                    {
                        nextALS.Exception = resTask.Exception;
                    }
                }
                return resTask.Result;
            }));
        }
Beispiel #5
0
        async Task <(bool isSuccess, string body, HttpResponseMessage response)> ReadInternalAsync(IRequest request, IRequestEntry nextALS, IResponseBase resObj, CancellationToken token)
        {
            var resTask = await this.SendAsync(request, nextALS, token).ContinueWith(p => p);

            token.ThrowIfCancellationRequested();

            if (resTask.IsCanceled)
            {
                resObj.resCode = ResponseErrorCode.TaskCancel;
                resObj.msg     = "req rpc task is canceled";
                return(false, null, null);
            }
            if (resTask.IsFaulted)
            {
                resObj.resCode = ResponseErrorCode.TaskFail;
                resObj.msg     = resTask.Exception.GetBaseException().GetBaseException().Message;
                this.Logger.LogError(0, resTask.Exception, "req rpc api error.{0}", request.ToJson());
                return(false, null, null);
            }

            var res = await resTask;

            if (!res.IsSuccessStatusCode)
            {
                resObj.resCode = res.StatusCode.ToInt();
                resObj.msg     = res.ReasonPhrase;
                return(false, null, null);
            }
            token.ThrowIfCancellationRequested();
            var readTask = await res.Content.ReadAsStringAsync().ContinueWith(p => p);


            token.ThrowIfCancellationRequested();
            if (readTask.IsFaulted)
            {
                resObj.resCode = ResponseErrorCode.ReadRpcContentError;
                resObj.msg     = readTask.Exception.GetBaseException().GetBaseException().Message;
                return(false, null, null);
            }
            if (readTask.IsCanceled)
            {
                resObj.resCode = ResponseErrorCode.TaskCancel;
                resObj.msg     = "read prc content task is canceled";
                return(false, null, null);
            }

            var resultStr = await readTask;

            return(true, resultStr, res);
        }