让WCF客户端的“调用”成为一种“享受”

来源:岁月联盟 编辑:exp 时间:2011-11-03

 

刚开始使用WCF时,总是在using中进行调用,比如:

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

后来发现,这是微软的一个“骗局”,当时我写了篇博客“不要在using语句中调用WCF服务”。

从此改为这样调用:

		CnblogsWcfClient client = new CnblogsWcfClient();client.Say("Hello, cnblogs.com!");try{    client.Close();}catch{    client.Abort();}

每当写到这样的代码,心理总是有些不舒服。经过近10个月这样的不舒服之后,再也无法忍受。。。

于是,今天决定解决这个问题。。。

从 What is the best workaround for the WCF client `using` block issue? 找到 Practical Functional C# - Part II,发现了解决之道,但其中提供的代码不完整,经过几个小时的摸索,终于找到满意的解决方法。

让WCF客户端的调用成为“享受”的代码如下:

应用程序中调用代码:

		//IUserService就是WCF的ServiceContract,是客户端自动生成的代理类WcfClient.UseService((IUserService userService) => (userService.GetUser(userId)));

WcfClient实现代码:

		public class WcfClient{    public static TReturn UseService<TChannel, TReturn>(Func<TChannel, TReturn> func)    {        var chanFactory = new ChannelFactory<TChannel>("*");        TChannel channel = chanFactory.CreateChannel();        TReturn result = func(channel);        try        {            ((IClientChannel)channel).Close();        }        catch        {            ((IClientChannel)channel).Abort();        }        return result;    }}

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

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

/

小结

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

 

 

作者 dudu