Ejemplo n.º 1
0
        protected virtual object ResponseHandle(ProxyContext ctx, HttpResponseMessage response)
        {
            var serializer         = ctx.Serializer;
            var expectedReturnType = this.GetExpectedReturnType(ctx);            //返回的是IAOPResult的实现
            var stream             = response.Content.ReadAsStreamAsync().GetAwaiter().GetResult();

            if (stream == null)
            {
                throw new ResultException(-1, "Server No Response");
            }
            var aop = (IAOPResult)serializer.Deserialize(stream, expectedReturnType, null);

            if (aop == null)
            {
                throw new ResultException(-2, "Unexpected Response");
            }

            this.Factory?.OnResultHandling(ctx, aop);
            if (ctx.ResultInitialized)
            {
                return(ctx.ResultValue);
            }
            var methodInfo = ctx.CallMethod;
            var returnType = methodInfo.ReturnType;

            return(this.ResultHandle(aop, returnType, serializer));
        }
Ejemplo n.º 2
0
        protected virtual ProxyContext PrepareProxyContext(IDictionary <string, object> parameters, MethodInfo targetMethod, string contentType)
        {
            var serializer = SerializerFactory.Current.GetSerializer(contentType) ?? SerializerFactory.Current.Default;
            var ctx        = new ProxyContext {
                ProxyInstance = this,
                Serializer    = serializer,
                CallMethod    = targetMethod,
                Parameters    = parameters,
                ProxyUrl      = this.GetRealUrl(this.ProxyUrl),
            };

            return(ctx);
        }
Ejemplo n.º 3
0
        private void OnRequest(ProxyContext ctx)
        {
            if (ctx.ExpectedReturnType != null)
            {
                return;
            }
            var name = $"{ctx.ProxyInstance.ProxyType}.{ctx.CallMethod.Name}";

            if (this.returnTypes.TryGetValue(name, out var type))
            {
                ctx.ExpectedReturnType = type;
            }
        }
Ejemplo n.º 4
0
        //获取方法期望的返回值,如果期望返回的不是IAOPResult,则包装成此类型
        protected virtual Type GetExpectedReturnType(ProxyContext ctx)
        {
            var type = ctx.ExpectedReturnType;

            if (type == null)
            {
                var methodInfo = ctx.CallMethod;
                var returnType = methodInfo.ReturnType;
                if (returnType.IsInterface)                   //方法定义的返回值是接口,需要转换成实现
                {
                    if (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(IAOPResult <>))
                    {
                        //返回值是IAOPResult<>(接口),则返回默认实现
                        var esbType = typeof(AOPResult <>);
                        type = esbType.MakeGenericType(returnType.GetGenericArguments());
                    }
                    else
                    {
                        type = typeof(AOPResult);
                    }
                }
                else if (returnType == typeof(void))
                {
                    type = typeof(AOPResult);
                }
                else                     //方法定义的返回值是实际类型,则需要判断是否为IAOPResult的实现
                {
                    if (typeof(IAOPResult).IsAssignableFrom(returnType))
                    {
                        type = returnType;
                    }
                    else
                    {
                        //不是IAOPResult的实现,则包装
                        var esbType = typeof(AOPResult <>);
                        type = esbType.MakeGenericType(returnType);
                    }
                }
            }
            return(type);
        }
Ejemplo n.º 5
0
        protected virtual void PrepareRequest(ProxyContext ctx)
        {
            var parameters = ctx.Parameters;
            var serializer = ctx.Serializer;
            var request    = ctx.HttpRequest;
            var methodInfo = ctx.CallMethod;

            parameters.Add("$method", methodInfo.ToString());
            var methodName = this.GetCallMethodName(ctx);

            parameters.Add("$action", methodName);
            request.Headers.Add("$action", methodName);
            ctx.ProxyUrl += "/" + methodName;
            using (var ms = new MemoryStream()) {
                serializer.Serialize(ms, parameters, null);
                ms.Position = 0;
                using (var sr = new StreamReader(ms)) {
                    request.Content = new StringContent(sr.ReadToEnd(), Encoding.UTF8, this.ContentType);
                }
            }
        }
Ejemplo n.º 6
0
 public void OnCalled(ProxyContext ctx)
 {
     this.Called?.Invoke(ctx);
 }
Ejemplo n.º 7
0
 public void OnResultHandling(ProxyContext ctx, IAOPResult result)
 {
     this.ResultHandling?.Invoke(ctx, result);
 }
Ejemplo n.º 8
0
 public void OnRequest(ProxyContext ctx)
 {
     this.Request?.Invoke(ctx);
 }
Ejemplo n.º 9
0
 public void OnCalling(ProxyContext ctx)
 {
     this.Calling?.Invoke(ctx);
 }
Ejemplo n.º 10
0
 protected override object ResponseHandle(ProxyContext ctx, HttpResponseMessage response)
 {
     return(ElapseTime("ResponseHandle`2", () => base.ResponseHandle(ctx, response)));
 }
Ejemplo n.º 11
0
 protected override void PrepareRequest(ProxyContext ctx)
 {
     ElapseTime("PrepareRequest", () => base.PrepareRequest(ctx));
 }
Ejemplo n.º 12
0
 protected virtual string GetCallMethodName(ProxyContext ctx)
 {
     return(ctx.CallMethod.Name);
 }