Esempio n. 1
0
        protected override object Invoke(MethodInfo targetMethod, object[] args)
        {
            ClientActionHanler handler = ClientActionFactory.GetHandler((MethodInfo)targetMethod);
            var rinfo = handler.GetRequestInfo(args);
            var host  = handler.Host != null ? handler.Host : Host;

            if (host == null)
            {
                throw new Exception("The service host is not defined!");
            }
            var request = rinfo.GetRequest(host);
            var task    = request.Execute();

            if (!handler.Async)
            {
                throw new HttpClientException(request, Host.Uri, $"{rinfo.Method} method is not supported and the return value must be task!");
            }
            else
            {
                IAnyCompletionSource source = CompletionSourceFactory.Create(handler.ReturnType, TimeOut);
                source.Wait <Response>(task, (c, t) =>
                {
                    if (t.Result.Exception != null)
                    {
                        c.Error(t.Result.Exception);
                    }
                    else
                    {
                        c.Success(t.Result.Body);
                    }
                });
                return(source.GetTask());
            }
        }
Esempio n. 2
0
        public static ClientActionHanler GetHandler(MethodInfo method)
        {
            ClientActionHanler result;

            if (!mHandlers.TryGetValue(method, out result))
            {
                result            = new ClientActionHanler(method);
                mHandlers[method] = result;
            }
            return(result);
        }
Esempio n. 3
0
        protected override object Invoke(MethodInfo targetMethod, object[] args)
        {
            ClientActionHanler handler = Cluster.GetHandler((MethodInfo)targetMethod);
            var rinfo = handler.GetRequestInfo(args);

            if (handler.NodeAgent == null || handler.NodeAgent.Version != Cluster.Version)
            {
                handler.NodeAgent = Cluster.GetAgent(rinfo.Url);
            }
            HttpHost host = handler.NodeAgent.Node.GetClient();

            if (host == null)
            {
                Exception error = new HttpClientException(null, null, $"request {rinfo.Url} no http nodes are available");
                if (handler.Async)
                {
                    //Type gtype = typeof(AnyCompletionSource<>);
                    //Type type = gtype.MakeGenericType(handler.ReturnType);
                    IAnyCompletionSource source = CompletionSourceFactory.Create(handler.ReturnType, Cluster.TimeOut);  //(IAnyCompletionSource)Activator.CreateInstance(type);
                    source.Error(error);
                    return(source.GetTask());
                }
                else
                {
                    throw error;
                }
            }
            if (!handler.Async)
            {
                throw new Exception($"{rinfo.Method} method is not supported and the return value must be task!");
            }
            else
            {
                var request = rinfo.GetRequest(host);
                foreach (var item in Header)
                {
                    request.Header[item.Key] = item.Value;
                }
                var task = request.Execute();
                IAnyCompletionSource source = CompletionSourceFactory.Create(handler.ReturnType, Cluster.TimeOut);
                source.Wait <Response>(task, (c, t) =>
                {
                    if (t.Result.Exception != null)
                    {
                        c.Error(t.Result.Exception);
                    }
                    else
                    {
                        c.Success(t.Result.Body);
                    }
                });
                return(source.GetTask());
            }
        }