Exemplo n.º 1
0
        protected async Task <T> OnExecute <T>(MethodBase targetMethod, params object[] args)
        {
            var rinfo   = ClientActionFactory.GetHandler((MethodInfo)targetMethod).GetRequestInfo(args);
            var request = rinfo.GetRequest(Host);
            var respnse = await request.Execute();

            if (respnse.Exception != null)
            {
                throw respnse.Exception;
            }
            return((T)respnse.Body);
        }
Exemplo n.º 2
0
        protected override object Invoke(MethodInfo targetMethod, object[] args)
        {
            ClientActionHanler handler = ClientActionFactory.GetHandler((MethodInfo)targetMethod);
            var rinfo = handler.GetRequestInfo(args);

            if (handler.Node == null)
            {
                handler.Node = Cluster[rinfo.Url];
            }
            HttpHost host = handler.Node.GetClient();

            if (host == null)
            {
                throw new HttpClientException(null, null, "no http nodes are available");
            }
            if (!handler.Async)
            {
                var request = rinfo.GetRequest(host);
                var task    = request.Execute();
                int timeout = host.Pool.TimeOut;
                if (!task.Wait(timeout))
                {
                    throw new HttpClientException(request, host.Uri, $"{rinfo.Method} {rinfo.Url} request time out {timeout}!");
                }
                if (task.Result.Exception != null)
                {
                    throw task.Result.Exception;
                }
                return(task.Result.Body);
            }
            else
            {
                var request = rinfo.GetRequest(host);
                var task    = request.Execute();
                if (handler.MethodType == typeof(ValueTask))
                {
                    AnyCompletionSource <object> source = new AnyCompletionSource <object>();
                    source.WaitResponse(task);
                    return(new ValueTask(source.Task));
                }
                else
                {
                    Type gtype = typeof(AnyCompletionSource <>);
                    Type type  = gtype.MakeGenericType(handler.ReturnType);
                    IAnyCompletionSource source = (IAnyCompletionSource)Activator.CreateInstance(type);
                    source.WaitResponse(task);
                    return(source.GetTask());
                }
            }
        }