コード例 #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());
            }
        }
コード例 #2
0
ファイル: Request.cs プロジェクト: csharp2012/HttpClients
 public Task <Response> Execute()
 {
     mTaskCompletionSource         = CompletionSourceFactory.Create <Response>(TimeOut != null ? TimeOut.Value : HttpHost.Pool.TimeOut); // new AnyCompletionSource<Response>();
     mTaskCompletionSource.TimeOut = OnRequestTimeout;
     OnExecute();
     return((Task <Response>)mTaskCompletionSource.GetTask());
 }
コード例 #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());
            }
        }
コード例 #4
0
 public Task <DataFrame> ReceiveFrame()
 {
     Connect();
     lock (mLockReceive)
     {
         if (mDataFrames.TryDequeue(out object data))
         {
             if (data is Exception error)
             {
                 throw error;
             }
             return(Task.FromResult((DataFrame)data));
         }
         else
         {
             mReceiveCompletionSource         = CompletionSourceFactory.Create <DataFrame>(TimeOut);
             mReceiveCompletionSource.TimeOut = OnReceiveTimeOut;
             return((Task <DataFrame>)mReceiveCompletionSource.GetTask());
         }
     }
 }
コード例 #5
0
        public async Task <Response> Execute()
        {
#if NETCOREAPP2_1
            using (mRequestTrack = CodeTrackFactory.TrackReport(this, CodeTrackLevel.Module, null))
            {
                if (Activity.Current != null)
                {
                    Header[CODE_TREAK_PARENTID] = Activity.Current.Id;
                }
                if (mRequestTrack.Enabled)
                {
                    mRequestTrack.Activity?.AddTag("tag", "BeetleX HttpClient");
                }
#endif
            Executing?.Invoke(this);
            mTaskCompletionSource         = CompletionSourceFactory.Create <Response>(TimeOut != null ? TimeOut.Value : HttpHost.Pool.TimeOut); // new AnyCompletionSource<Response>();
            mTaskCompletionSource.TimeOut = OnRequestTimeout;
            OnExecute();
            return(await(Task <Response>) mTaskCompletionSource.GetTask());

#if NETCOREAPP2_1
        }
#endif
        }