예제 #1
0
            public static async Task <DevInfo> CheckDevice2Async(ServiceAsync svc, int devId)
            {
                var lastKnownState = svc.GetKnownDevStateAsync(devId);

                DevAddr addr = await svc.GetDevAddrAsync(devId);

                DevReport devReport = await svc.GetCurrDevReportAsync(addr);

                return(new DevInfo(await lastKnownState, devReport));
            }
예제 #2
0
            public static Task <DevInfo> CheckDevice3Async(ServiceAsync svc, int devId)
            {
                var lastKnownState = svc.GetKnownDevStateAsync(devId);

                var t2 = svc.GetDevAddrAsync(devId)
                         .ContinueWith(ant => svc.GetCurrDevReportAsync(ant.Result))
                         .Unwrap();

                return(Task.WhenAll(lastKnownState, t2)
                       .ContinueWith(_ => new DevInfo(lastKnownState.Result, t2.Result)));
            }
        public void Intercept(IInvocation invocation)
        {
            //HACK:接口代理类型自动查找第一个接口作为服务类型
            var serviceType = invocation.TargetType ?? invocation.Proxy.GetType().GetInterfaces()[0];
            var service     = this._endpoint.ServiceTable.Services.FirstOrDefault(o => o.Type != null && o.Type.Equals(serviceType));

            //TODO:优化查找
            //重试一次
            if (service == null)
            {
                service = this._endpoint.ServiceTable.Services.FirstOrDefault(o => o.Type != null && o.Type.Equals(serviceType));
            }
            if (service == null)
            {
                throw new ServiceException(
                          string.Format("当前服务节点不存在类型为{0}的服务配置信息", serviceType.FullName));
            }
            //生成ServiceCall
            var call = this.ParseCall(service, invocation);
            //调用模式
            var mode = ServiceAsync.Mode();

            //同步调用
            if (!ServiceAsync.IsAsync())
            {
                invocation.ReturnValue = this._endpoint.Invoke(call, invocation.Method.ReturnType);
            }
            else
            {
                //HACK:必须设置默认值,否则值类型会出现异常
                //TODO:优化此处异步调用默认值的反射处理
                if (invocation.Method.ReturnType.IsValueType && invocation.Method.ReturnType != typeof(void))
                {
                    invocation.ReturnValue = Activator.CreateInstance(invocation.Method.ReturnType);
                }
                //客户端异步 无回调
                if (mode == ServiceAsyncMode.Client && invocation.Method.ReturnType.Equals(typeof(void)))
                {
                    this._endpoint.InvokeAsync(call);
                }
                //客户端异步 回调 双工 TODO:使用Saga模式 不支持longrunning
                else if (mode == ServiceAsyncMode.Client)
                {
                    this._endpoint.InvokeAsync(call, invocation.Method.ReturnType, ServiceAsync.Callback());
                }
                //服务器端异步 通过异步接收节点中转
                else
                {
                    this._endpoint.AsyncInvokeAt(mode == ServiceAsyncMode.Server
                        ? new Uri(call.Target.HostUri)
                        : this._endpoint.AsyncReceiverUri
                                                 , call);
                }
            }
            this._log.InfoFormat("{0}调用服务节点:{1},服务:{2}.{3},身份:{4}|{5},参数:{6},返回:{7}"
                                 , ServiceAsync.IsAsync() ? "以" + mode + "异步模式" : "同步"
                                 , call.Target.HostUri
                                 , call.Target.Name
                                 , call.TargetMethod
                                 , call.Identity.Source
                                 , call.Identity.AuthKey
                                 , string.Join("$", invocation.Arguments.Select(o => (o ?? "null").ToString()).ToArray())
                                 , ServiceAsync.IsAsync() ? "异步调用无返回值" : invocation.ReturnValue);
        }