コード例 #1
0
ファイル: CallingInterceptor.cs プロジェクト: tukzer/Seif
        public void Intercept(Castle.DynamicProxy.IInvocation invocation)
        {
            var wrapper = new InvokerWrapper(_invoker, _filters);

            IList <ParameterData> parameters = new List <ParameterData>();

            for (int i = 0; i < invocation.Arguments.Length; i++)
            {
                parameters.Add(new ParameterData
                {
                    TypeName = invocation.Arguments[i].GetType().FullName,
                    Data     = _invoker.Serializer.Serialize(invocation.Arguments[i])
                });
            }

            var         serviceType   = typeof(T);
            IInvocation rpcInvocation = new RpcInvocation
            {
                TraceId     = Guid.NewGuid().ToString("N"),
                ServiceName = serviceType.FullName,
                MethodName  = invocation.Method.Name,
                ReturnType  = invocation.Method.ReturnType,
                Parameters  = parameters,
                Attributes  = new Dictionary <string, string>()
            };

            var result = wrapper.Invoke(rpcInvocation);

            //var resultHandler = SeifApplication.AppEnv.GlobalConfiguration.ConsumerConfiguration.GetResultHandler();
            invocation.ReturnValue = result.Result;
        }
コード例 #2
0
        public async Task <T> InvokeAsync <T>(object[] args)
        {
            var watch   = Stopwatch.StartNew();
            var context = Resolve(args);

            var returnValue = default(T);

            try
            {
                //Debug.Assert(_dispatchProxyInvokeAsyncTMethod != null);
                //var genericmethod = _dispatchProxyInvokeAsyncTMethod.MakeGenericMethod(typeof(T));
                //returnValue = await (Task<T>)genericmethod.Invoke(context.Packed.DispatchProxy,
                //                                                       new object[] { context.Method, context.Packed.Args });
                var icn    = new RpcInvocation(_app, _version, context.Packed.DeclaringType, context.Method, context.Packed.Args);
                var result = await _zooyardPools.Invoke <T>(icn);

                returnValue = result.Value;
                context.Packed.ReturnValue = returnValue;
            }
            catch (TargetInvocationException tie)
            {
                ExceptionDispatchInfo.Capture(tie.InnerException).Throw();
            }
            finally
            {
                watch.Stop();
            }
            Logger().LogInformation($"async proxy generator: {watch.ElapsedMilliseconds} ms");
            return(returnValue);
        }
コード例 #3
0
        protected async Task <T> Invoke <T>(MethodInfo method, object[] args)
        {
            //Console.WriteLine("step1:"+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            var inv = new RpcInvocation(method, args);

            //JsonConvert.SerializeObject(inv);
            //Console.WriteLine("step2:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            inv.ReturnType = typeof(T);
            //todo 这里可以添加负载均衡策略、客户端过滤器等
            IResult result  = null;
            var     invoker = GetInvoker();

            //Console.WriteLine("step3:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            if (invoker == null)
            {
                throw new Exception("no service found!");
            }
            result = await invoker.Invoke(inv);

            // Console.WriteLine("step12:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            if (result.HasException || result.Exception != null)
            {
                throw result.Exception;
            }
            var value = ((Response)result.Value).Mresult;

            //Console.WriteLine("step13:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            return((T)((RpcResult)value).Value);
        }
コード例 #4
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);
            }
        }
コード例 #5
0
            private IInvocation CreateInvocation(IChannel channel, URL url, string methodKey)
            {
                var method = url.GetParameter(methodKey, "");

                if (string.IsNullOrEmpty(method))
                {
                    return(null);
                }
                RpcInvocation invocation = new RpcInvocation();

                invocation.SetAttachment(Constants.PathKey, url.Path);
                invocation.SetAttachment(Constants.GroupKey, url.GetParameter(Constants.GroupKey, ""));
                invocation.SetAttachment(Constants.InterfaceKey, url.GetParameter(Constants.InterfaceKey, ""));
                invocation.SetAttachment(Constants.VersionKey, url.GetParameter(Constants.VersionKey, ""));
                if (url.GetParameter(Constants.StubEventKey, false))
                {
                    invocation.SetAttachment(Constants.StubEventKey, "true");
                }
                return(invocation);
            }
コード例 #6
0
        public async Task <IResult> Invoke(IInvocation inv)
        {
            if (_destroyed)
            {
                throw new Exception("Rpc invoker for service " + this + " on consumer " + NetUtils.GetLocalAddress()
                                    + " use dubbo version " + Common.Version.GetVersion()
                                    + " is DESTROYED, can not be invoked any more!");
            }
            RpcInvocation invocation = (RpcInvocation)inv;

            invocation.Invoker = this;
            if (_attachment != null && _attachment.Count > 0)
            {
                invocation.AddAttachmentsIfAbsent(_attachment);
            }
            //todo RpcContext
            //var context = RpcContext.getContext().getAttachments();
            //if (context != null)
            //{
            //    invocation.AddAttachmentsIfAbsent(context);
            //}
            if (GetUrl().GetMethodParameter(invocation.MethodName, Constants.AsyncKey, false))
            {
                invocation.SetAttachment(Constants.AsyncKey, "true");
            }
            RpcUtils.AttachInvocationIdIfAsync(GetUrl(), invocation);
            //Console.WriteLine("step4:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));

            try
            {
                return(await DoInvoke(invocation));
            }
            catch (Exception e)
            {
                return(new RpcResult(e));
            }
        }
コード例 #7
0
ファイル: DubboCodec.cs プロジェクト: zhj149/dubbo.net
        protected override void EncodeRequestData(IChannel channel, IObjectOutput output, object data)
        {
            RpcInvocation inv = (RpcInvocation)data;

            output.WriteUTF(inv.GetAttachment(Constants.DubboVersionKey, DubboVersion));
            output.WriteUTF(inv.GetAttachment(Constants.PathKey));
            output.WriteUTF(inv.GetAttachment(Constants.VersionKey));
            //Console.WriteLine("encode attachment:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            output.WriteUTF(inv.MethodName);
            output.WriteUTF(ReflectUtil.GetDesc(inv.ParameterTypes));
            //Console.WriteLine("encode method and paramtype:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            object[]
            args = inv.Arguments;
            if (args != null)
            {
                foreach (var arg in args)
                {
                    output.WriteObject(arg);
                }
            }
            //Console.WriteLine("encode args:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            output.WriteObject(inv.Attachments);
            //Console.WriteLine("encode all attachments:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
        }
コード例 #8
0
ファイル: ServiceProxyBase.cs プロジェクト: zhj149/dubbo.net
        protected async Task <T> Invoke <T>(MethodInfo method, object[] args)
        {
            var inv = new RpcInvocation(method, args)
            {
                ReturnType = typeof(T)
            };
            //todo 这里可以添加负载均衡策略、客户端过滤器等
            IResult result  = null;
            var     invoker = GetInvoker();

            if (invoker == null)
            {
                throw new Exception("no service found!");
            }
            result = await invoker.Invoke(inv);

            if (result.HasException || result.Exception != null)
            {
                throw result.Exception;
            }
            var value = ((Response)result.Value).Mresult;

            return((T)((RpcResult)value).Value);
        }