WCF客户端的调用

来源:互联网 发布:淘宝350模板 编辑:程序博客网 时间:2024/05/18 04:54

WCF客户端的调用,可能绝大多数人都是用Using的方式,如下所示

<span></span>   using (SMSServiceClient client = new SMSServiceClient())            {                return client.updateTagConfig(flag, fuid);            }
当然,我也是用的这种方式,自从看了这篇文章之后,才明白了这样是有缺陷的,作者是这样写的

如果你调用WCF服务时,像下面的代码这样在using语句中进行调用,需要注意一个问题。

using (CnblogsWcfClient client = new CnblogsWcfClient())
{
client.Say(
"Hello, cnblogs.com!");
}

上面这段代码看上去没问题,CnblogsWcfClient是一个自动生成的WCF客户端代理,继承自System.ServiceModel.ClientBase。using语句结束时,会调用ClientBase实现的System.IDisposable.Dispose接口,实际就是调用ClientBase的Close()方法。用.NET Refector打开C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.ServiceModel.dll,可以看到这样的代码,见下图:

不仅看上去没问题,似乎就是没问题。但是...问题就出在ClientBase.Close()上,Close()要关闭的是一个网络连接,如果这时网络连接出现问题,不能正常关闭会引发异常(ClientBase的Close方法就是这样设计的,引发异常,而不是强制关闭),问题就来了。本来我们使用using的目的就是不管出现什么状况,即使天塌下来,也给我关闭掉;结果,关是关了,却没有闭,天还是塌下来了。

也许我们可以用“不可抗拒力”回避这个问题,但程序员的天性是解决问题。代码中任何一个小问题都不能忽视,因为我们很难预料这个小问题会不会带来大问题。

那如何解决这个问题呢?MSDN中有答案(去MSDN看看),代码如下:

复制代码
CnblogsWcfClient client = new CnblogsWcfClient();
try
{
client.Say(
"Hello, cnblogs.com!");
client.Close();
}
catch (CommunicationException e)
{
...
client.Abort();
}
catch (TimeoutException e)
{
...
client.Abort();
}
catch (Exception e)
{
...
client.Abort();
throw;
}
复制代码

看了之后豁然开朗,然后作者又写了如下几种方法,很是牛逼

public class WCFClient    {        public static TReturn UseService<TChannel, TReturn>(Func<TChannel, TReturn> func)        {            var chanFactory = new ChannelFactory<TChannel>("*");            TChannel channel = chanFactory.CreateChannel();            ((IClientChannel)channel).Open();            TReturn result = func(channel);            try            {                ((IClientChannel)channel).Close();            }            catch            {                ((IClientChannel)channel).Abort();            }            return result;        }        //调用方法如下所示        //DataTable dt = WCFClient.UseService((ISMSService service) => (service.getUserConfig(flag)));        //        public static void UseService<TChannel>(Action<TChannel> action)         {            var chanFactory = new ChannelFactory<TChannel>("*");            TChannel channel = chanFactory.CreateChannel();            ((IClientChannel)channel).Open();            action(channel);            try            {                ((IClientChannel)channel).Close();            }            catch             {                ((IClientChannel)channel).Abort();            }        }        //调用方法如下所示        //无返回值        //WCFClient.UseService((ISMSService service) => service.insertTuConfig(fuid));        //        //有返回值        //WCFClient.UseService((ISMSService service) => { bool s = service.updateTagConfig("", ""); });        // }

还有一个改进的版本 

var client = new WcfClient<IZzkDocumentService>();
var docs = client.UseService(s => s.GetZzkDocuments("0", 10));

然后看看WcfClient的实现代码:

复制代码
public class WcfClient<TService> where TService : class
{
public TReturn UseService<TReturn>(Expression<Func<TService, TReturn>> operation)
{
var channelFactory = new ChannelFactory<TService>("*");
TService channel = channelFactory.CreateChannel();
var client = (IClientChannel)channel;
client.Open();
TReturn result = operation.Compile().Invoke(channel);
try
{
if (client.State != CommunicationState.Faulted)
{
client.Close();
}
}
catch
{
client.Abort();
}
return result;
}
}


第一种实现的相关资料

解决这个问题的主要时间花在找到上面代码中的那个星号,星号对应的参数名是endpointConfigurationName。

开始时困扰于如何给endpointConfigurationName参数传值。后来,研究了一下自动生成的代理类,也没有与endpointConfigurationName相关的信息,只是继承自System.ServiceModel.ClientBase<T>。然后,通过ILSPy反编译ClientBase<T>的代码,找到了这个星号,见下图:

小结

也许还有更“享受”的调用WCF客户端方法,但是我觉得至少比以前的方法用起来更舒服。解决问题之后,最好的庆祝方式就是写一篇博客。分享的不仅仅是解决方法,还有解决问题之后的那种兴奋!


第二种的相关资料

今天下午,借助.NET世界中一个强大的武器完成了这个小小的心愿,它就是表达式树 —— Expression<Func<TService, TReturn>> operation

先一睹调用改进版WCF Client的风采:

var client = new WcfClient<IZzkDocumentService>();
var docs = client.UseService(s => s.GetZzkDocuments("0", 10));

然后看看WcfClient的实现代码:

复制代码
public class WcfClient<TService> where TService : class
{
public TReturn UseService<TReturn>(Expression<Func<TService, TReturn>> operation)
{
var channelFactory = new ChannelFactory<TService>("*");
TService channel = channelFactory.CreateChannel();
var client = (IClientChannel)channel;
client.Open();
TReturn result = operation.Compile().Invoke(channel);
try
{
if (client.State != CommunicationState.Faulted)
{
client.Close();
}
}
catch
{
client.Abort();
}
return result;
}
}
复制代码

对于Expression<Func<TService, TReturn>> operation,我的理解是:请你告诉我,如果给你一段代码(TService),你如何给我一个改变了的世界(TReturn)。我不关心给你的是什么代码,也不关心改变后的世界是什么样子,我只关心你如何改变世界。


转自 http://www.cnblogs.com/dudu/archive/2011/11/02/wcf_client_no_using_call.html

http://www.cnblogs.com/dudu/archive/2011/12/31/wcfclient.html

感谢dudu牛人


0 0
原创粉丝点击