Ejemplo n.º 1
0
        public IResult Invoke(IInvocation invocation)
        {
            var parameterType = Url.GetMethodParameterAndDecoded(invocation.MethodInfo.Name, PARAMETERTYPE_KEY, DEFAULT_PARAMETERTYPE).ToLower();
            var method        = Url.GetMethodParameterAndDecoded(invocation.MethodInfo.Name, METHODTYPE_KEY, DEFAULT_METHODTYPE).ToLower();
            var parameters    = invocation.MethodInfo.GetParameters();
            var stub          = new HttpStub(Instance, isOpen);
            var value         = stub.Request($"/{Url.Path}/{invocation.MethodInfo.Name.ToLower()}", parameterType, method, parameters, invocation.Arguments).GetAwaiter().GetResult();

            if (invocation.MethodInfo.ReturnType.IsValueType)
            {
                if (invocation.MethodInfo.ReturnType == typeof(void))
                {
                    return(new RpcResult());
                }
                return(new RpcResult(value.ChangeType(invocation.MethodInfo.ReturnType)));
            }

            var result = new RpcResult(JsonConvert.DeserializeObject(value, invocation.MethodInfo.ReturnType));

            return(result);
        }
Ejemplo n.º 2
0
        protected override async Task <IResult <T> > HandleInvoke <T>(IInvocation invocation)
        {
            var methodName = invocation.MethodInfo.Name;
            var endStr     = "Async";

            if (invocation.MethodInfo.Name.EndsWith(endStr, StringComparison.OrdinalIgnoreCase))
            {
                methodName = methodName.Substring(0, methodName.Length - endStr.Length);
            }
            var pathList      = _url.Path?.Split('/', StringSplitOptions.RemoveEmptyEntries) ?? new string[0];
            var pathUrl       = new List <string>(pathList);
            var method        = DEFAULT_METHODTYPE;
            var parameterType = DEFAULT_PARAMETERTYPE;


            var targetDescription = invocation.TargetType.GetCustomAttribute <DescriptionAttribute>();

            if (targetDescription != null && !string.IsNullOrWhiteSpace(targetDescription.Description))
            {
                pathUrl.AddRange(targetDescription.Description.Split('/', StringSplitOptions.RemoveEmptyEntries));
            }
            var methodDescription = invocation.MethodInfo.GetCustomAttribute <DescriptionAttribute>();

            if (methodDescription != null && !string.IsNullOrWhiteSpace(methodDescription.Description))
            {
                //ShowHello|post&json
                var actions = methodDescription.Description.Split('|');
                if (actions.Length < 2)
                {
                    throw new Exception("place set action discrption like this sample ShowHello|post&json");
                }

                if (!string.IsNullOrWhiteSpace(actions[0]) || actions[0] != "_")
                {
                    methodName = actions[0];
                }
                pathUrl.Add(methodName);

                var methodAndContentTypeList = actions[1].Split('&');
                if (methodAndContentTypeList.Length < 2)
                {
                    throw new Exception("place set action discrption like this sample ShowHello|post&json");
                }

                if (!string.IsNullOrWhiteSpace(methodAndContentTypeList[0]) || methodAndContentTypeList[0] != "_")
                {
                    method = methodAndContentTypeList[0];
                }
                if (!string.IsNullOrWhiteSpace(methodAndContentTypeList[1]) || methodAndContentTypeList[1] != "_")
                {
                    parameterType = methodAndContentTypeList[1];
                }
            }
            else
            {
                pathUrl.Add(methodName);
            }

            var    parameters = invocation.MethodInfo.GetParameters();
            var    stub       = new HttpStub(_instance, isOpen);
            var    watch      = Stopwatch.StartNew();
            string value      = null;

            try
            {
                using var stream = await stub.Request($"/{string.Join('/', pathUrl)}", parameterType, method, parameters, invocation.Arguments);

                var genType = typeof(T);
                //文件流处理
                if (genType == typeof(byte[]))
                {
                    byte[] bytes = new byte[stream.Length];
                    stream.Read(bytes, 0, bytes.Length);
                    // 设置当前流的位置为流的开始
                    stream.Seek(0, SeekOrigin.Begin);
                    watch.Stop();
                    return(new RpcResult <T>((T)bytes.ChangeType(genType), watch.ElapsedMilliseconds));
                }
                else
                {
                    using var sr = new StreamReader(stream);
                    value        = sr.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                Debug.Print(ex.StackTrace);
                throw;
            }
            finally
            {
                if (watch.IsRunning)
                {
                    watch.Stop();
                }

                Logger().LogInformation($"Http Invoke {watch.ElapsedMilliseconds} ms");
            }

            if (invocation.MethodInfo.ReturnType == typeof(void) || invocation.MethodInfo.ReturnType == typeof(Task))
            {
                return(new RpcResult <T>(watch.ElapsedMilliseconds));
            }

            if (invocation.MethodInfo.ReturnType.IsValueType || invocation.MethodInfo.ReturnType == typeof(string))
            {
                return(new RpcResult <T>((T)value.ChangeType(typeof(T)), watch.ElapsedMilliseconds));
            }

            if (invocation.MethodInfo.ReturnType.IsGenericType &&
                invocation.MethodInfo.ReturnType.GetGenericTypeDefinition() == typeof(Task <>))
            {
                var tastGenericType = invocation.MethodInfo.ReturnType.GenericTypeArguments[0];

                if (tastGenericType.IsValueType || tastGenericType == typeof(string))
                {
                    return(new RpcResult <T>((T)value.ChangeType(typeof(T)), watch.ElapsedMilliseconds));
                }

                var genericData = value.DeserializeJson <T>();
                return(new RpcResult <T>(genericData, watch.ElapsedMilliseconds));
            }

            var result = new RpcResult <T>(value.DeserializeJson <T>(), watch.ElapsedMilliseconds);

            return(result);
        }