Example #1
0
        public override async Task Invoke(AspectContext context, AspectDelegate next)
        {
            var httpContext = AspectCoreContext.From(context);

            if (RequestHost == null || string.IsNullOrEmpty(RequestHost.OriginalString))
            {
                if (httpContext.HostAttribute.Host != null && !string.IsNullOrEmpty(httpContext.HostAttribute.Host.OriginalString))
                {
                    RequestHost = httpContext.HostAttribute.Host;
                }
                else
                {
                    throw new ArgumentNullException(nameof(context), "未定义任何请求服务器地址,请在注册时传入BaseUrl或在服务契约添加HttpHost标签");
                }
            }

            var actionContext = new HttpApiActionContext
            {
                HttpApiClient             = this,
                RequestMessage            = new HttpRequestMessage(),
                RouteAttributes           = httpContext.RouteAttributes,
                ApiReturnAttribute        = httpContext.ApiReturnAttribute,
                ApiActionFilterAttributes = httpContext.ApiActionFilterAttributes,
                ApiActionDescriptor       = httpContext.ApiActionDescriptor.Clone() as ApiActionDescriptor
            };

            var parameters = actionContext.ApiActionDescriptor.Parameters;

            for (var i = 0; i < parameters.Length; i++)
            {
                parameters[i].Value = context.Parameters[i];
            }

            var apiAction = httpContext.ApiActionDescriptor;

            await next(context);

            try
            {
                context.ReturnValue = apiAction.Execute(actionContext);
            }
            catch (Exception ex)
            {
                var errMsg = ex.Message;

                while (ex.InnerException != null)
                {
                    errMsg += "--->" + ex.InnerException.Message;
                    ex      = ex.InnerException;
                }
                throw new Exception($"请求远程服务[{actionContext.RequestMessage?.Method.Method}]{actionContext.RequestMessage?.RequestUri ?? RequestHost}异常:{errMsg}", ex);
            }
        }
Example #2
0
        public override async Task Invoke(AspectContext context, AspectDelegate next)
        {
            var _context = AspectCoreContext.From(context);

            if (RequestHost == null || string.IsNullOrEmpty(RequestHost.OriginalString))
            {
                if (_context.HostAttribute.Host != null && !string.IsNullOrEmpty(_context.HostAttribute.Host.OriginalString))
                {
                    RequestHost = _context.HostAttribute.Host;
                }
                else
                {
                    throw new ArgumentNullException("BaseUrl or HttpHost attribute", "未定义任何请求服务器地址,请在注册时传入BaseUrl或在服务契约添加HttpHost标签");
                }
            }

            var actionContext = new HttpApiActionContext
            {
                HttpApiClient             = this,
                RequestMessage            = new HttpRequestMessage(),
                RouteAttributes           = _context.RouteAttributes,
                ApiReturnAttribute        = _context.ApiReturnAttribute,
                ApiActionFilterAttributes = _context.ApiActionFilterAttributes,
                ApiActionDescriptor       = _context.ApiActionDescriptor.Clone() as ApiActionDescriptor
            };

            var parameters = actionContext.ApiActionDescriptor.Parameters;

            for (var i = 0; i < parameters.Length; i++)
            {
                parameters[i].Value = context.Parameters[i];
            }

            var apiAction = _context.ApiActionDescriptor;

            await next(context);

            context.ReturnValue = apiAction.Execute(actionContext);
        }