当前位置:编程学习 > 网站相关 >>

Wcf通讯基础框架方案(三)——客户端

假设定义了一个服务契约:

[ServiceContract(Namespace = "WcfExtension.Services.Inte易做图ce")]
    public inte易做图ce ITestService
    {
        [OperationContract]
        int Add(int x, int y);

        [OperationContract]
        [ServiceKnownType(typeof(TestContract))]
        ITestContract TestData(ITestContract tc);
       
        [OperationContract]
        List<string> AddList(List<string> args);
    }首先看一下,客户端是怎么使用的:

Console.WriteLine(WcfServiceLocator.Create<ITestService>().Add(1, 3));WcfServiceLocator.Create()出来的就是一个接口,可以保持这个引用以后调用,也可以每次直接这么写,那么来看看WcfServiceLocator:

    public static T Create<T>() where T : class
        {
            return (T)(new ServiceRealProxy<T>().GetTransparentProxy());
        }

        public static IWcfLogService GetLogService()
        {
            if (enableRemoteLogService)
                return (IWcfLogService)(new LogServiceRealProxy().GetTransparentProxy());
            else
                return new LocalLogService();
        }Create方法很简单,只是返回一个ServiceRealProxy的透明代理。这个之后会详细说,下面单独有一个GetLogService专门用于获取日志服务。这里通过心跳来判断远程的日志服务是否可用,如果不可用直接返回本地的日志服务。之所以日志服务的真实代理和其它业务服务的真实代理不同是因为,在业务服务中我们需要加入很多横切日志,在日志服务中不需要:

    public override IMessage Invoke(IMessage msg)
        {
            using (var client = WcfServiceClientFactory.CreateServiceClient<T>())
            {
                var channel = client.Channel;
                IMethodCallMessage methodCall = (IMethodCallMessage)msg;
                IMethodReturnMessage methodReturn = null;
                object[] copiedArgs = Array.CreateInstance(typeof(object), methodCall.Args.Length) as object[];
                methodCall.Args.CopyTo(copiedArgs, 0);

                bool isSuccessuful = false;
                var stopwatch = Stopwatch.StartNew();

                try
                {
#if DEBUG
                    Console.WriteLine("开始调用:" + methodCall.MethodName);
#endif
                    object returnValue = methodCall.MethodBase.Invoke(channel, copiedArgs);

#if DEBUG
                    if (ClientApplicationContext.Current != null)
                        Console.WriteLine("这个请求由" + ClientApplicationContext.Current.ServerMachineName + "处理完成,服务端版本号:" + ClientApplicationContext.Current.ServerVersion);
                    Console.WriteLine("结束调用:" + methodCall.MethodName);

#endif
                    methodReturn = new ReturnMessage(returnValue,
                                                    copiedArgs,
                                                    copiedArgs.Length,
                                                    methodCall.LogicalCallContext,
                                                    methodCall);
                    isSuccessuful = true;
                }
                catch (Exception ex)
                {
                    var excepionID = ClientApplicationContext.Current.ServerExceptionID ?? "";
                    if (ex.InnerException != null)
                    {
#if DEBUG
                        Console.WriteLine("调用服务端方法出现异常:" + ex.InnerException.Message);
#endif
                        var exception = ex.InnerException;
                        if (exception is FaultException)
                &nbs

补充:综合编程 , 其他综合 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,