Exemple #1
0
        private async static Task CallGetServiceName()
        {
            System.Console.WriteLine("------------------------------GetServiceName----------------------------------");

            try
            {
                var result = await serviceProxy.CallRemoteServiceAsync(GetCustomerServiceActionType("GetServiceName"), null) as string;

                System.Console.WriteLine($"Result={result}");
            }
            catch (RequestTimeoutExcption ex)
            {
                System.Console.WriteLine($"Timeout: RequestId={ex.Request.Id}");
            }
            catch (ServiceCallException ex)
            {
                System.Console.WriteLine($"Service call exception: ExceptionId={ex.ExceptionId}, ExceptionMessage={ex.Message}");
            }
            catch (Exception ex)
            {
                System.Console.WriteLine($"Error: {ex.Message}");
            }
        }
        /// <summary>
        /// 根据服务代理配置决定调用远程服务或本地服务
        /// </summary>
        /// <param name="serviceProxy"></param>
        /// <param name="actionProxyType">Action代理类型</param>
        /// <param name="paramList">服务调用参数列表</param>
        /// <param name="localServiceInvoker">本地服务调用委托,当服务代理配置为本地服务时会被调用,参数:MethodInfo=Action代理类型,object[]=服务调用参数列表</param>
        /// <param name="remoteServiceInvokeCallback">远程服务执行完成回调,当服务代理配置为远程服务时会被调用,参数:MethodInfo=Action代理类型,object=服务返回值</param>
        public static void Invoke(this IServiceProxy serviceProxy, MethodInfo actionProxyType, object[] paramList, Action <MethodInfo, object[]> localServiceInvoker, Action <MethodInfo, object, Exception> remoteServiceInvokeCallback)
        {
            var serviceProxyInfo = serviceProxy.GetServiceProxyInfo(actionProxyType);

            if (serviceProxyInfo != null && serviceProxyInfo.Enabled)
            {
                var task = serviceProxy.CallRemoteServiceAsync(actionProxyType, paramList);
                //服务返回值为Task或Task的子类
                if (typeof(Task).IsAssignableFrom(actionProxyType.ReturnType))
                {
                    //服务返回值为Task<T>
                    if (actionProxyType.ReturnType.GetTypeInfo().IsGenericType)
                    {
                        //Task<object>无法转换为Task<T>,因此通过反射创建Task<T>对象
                        var realType = actionProxyType.ReturnType.GenericTypeArguments[0];
                        //创建Task<T>构造函数所需的第一个参数Func<,>
                        Type funcType = typeof(Func <,>).MakeGenericType(typeof(object), realType);
                        //为Func<,>创建实现方法
                        MethodInfo method = GetResultMethodInfo.MakeGenericMethod(typeof(object), realType);
                        //创建Func<,>委托
                        var handler = Delegate.CreateDelegate(funcType, method);
                        //创建Task<T>实例
                        var newTask = (Task)Activator.CreateInstance(actionProxyType.ReturnType, handler, task);
                        //在Task<T>中同步调用原始task
                        newTask.Start();

                        remoteServiceInvokeCallback?.Invoke(actionProxyType, newTask, null);
                    }
                    //服务返回值为Task
                    else
                    {
                        remoteServiceInvokeCallback?.Invoke(actionProxyType, task, null);
                    }
                }
                //服务返回值为非Task类型
                else if (actionProxyType.ReturnType != typeof(void))
                {
                    try
                    {
                        task.Wait();
                        remoteServiceInvokeCallback?.Invoke(actionProxyType, task.Result, null);
                    }
                    catch (AggregateException ex)
                    {
                        remoteServiceInvokeCallback?.Invoke(actionProxyType, null, ex.InnerException);
                    }
                    catch (Exception ex)
                    {
                        remoteServiceInvokeCallback?.Invoke(actionProxyType, null, ex);
                    }
                }
                else
                {
                    try
                    {
                        task.Wait();
                        remoteServiceInvokeCallback?.Invoke(actionProxyType, null, null);
                    }
                    catch (AggregateException ex)
                    {
                        remoteServiceInvokeCallback?.Invoke(actionProxyType, null, ex.InnerException);
                    }
                    catch (Exception ex)
                    {
                        remoteServiceInvokeCallback?.Invoke(actionProxyType, null, ex);
                    }
                }
            }
            else
            {
                localServiceInvoker?.Invoke(actionProxyType, paramList);
            }
        }