コード例 #1
0
ファイル: DubboInvoker.cs プロジェクト: zhj149/dubbo.net
        protected override async Task <IResult> DoInvoke(IInvocation invocation)
        {
            RpcInvocation inv        = (RpcInvocation)invocation;
            var           methodName = RpcUtils.GetMethodName(invocation);

            inv.SetAttachment(Constants.PathKey, GetUrl().Path);
            inv.SetAttachment(Constants.VersionKey, _version);

            IExchangeClient currentClient;

            if (_clients.Length == 1)
            {
                currentClient = _clients[0];
            }
            else
            {
                currentClient = _clients[Interlocked.Increment(ref _index) % _clients.Length];
            }
            try
            {
                //Console.WriteLine("step5:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                var isAsync  = RpcUtils.IsAsync(GetUrl(), invocation);
                var isOneway = RpcUtils.IsOneway(GetUrl(), invocation);
                var timeout  = GetUrl().GetMethodParameter(methodName, Constants.TimeoutKey, Constants.DefaultTimeout.ToString());
                if (isOneway)
                {
                    var isSent = GetUrl().GetMethodParameter(methodName, Constants.SentKey, false);
                    await currentClient.SendAsync(inv, isSent);

                    return(new RpcResult());
                }
                else
                {
                    int.TryParse(timeout, out var time);
                    var future = await currentClient.Request(inv, time);

                    return(new RpcResult(future));
                }
            }
            catch (TimeoutException e)
            {
                throw new Exception("Invoke remote method timeout. method: " + invocation.MethodName + ", provider: " + GetUrl() + ", cause: " + e.Message, e);
            }
            catch (RemotingException e)
            {
                throw new Exception("Failed to invoke remote method: " + invocation.MethodName + ", provider: " + GetUrl() + ", cause: " + e.Message, e);
            }
        }