首页 新闻 会员 周边

WCF 的通道转换逻辑到底有多坑爹?

0
悬赏园豆:10 [已关闭问题] 关闭于 2014-02-26 14:50

项目中用到了WCF调用,非常频繁,性能优化的时候,考虑到重用通道.重用通道的过程中发现通道由于某种原因挂掉后不能被继续重用,此时就需要创建一个新的通道来用,现在碰到的问题是:

                如何检测一个wcf通道是否已经挂掉了?

 1  static ITest channel;
 2         static object channelLock = new object();
 3 
 4         public static ITest GetChannel()
 5         {
 6             if (channel != null && ((ICommunicationObject)channel).State == CommunicationState.Opened)
 7             {
 8                 try
 9                 {
10                     // ((ICommunicationObject)channel).Open();
11                     return channel;
12                 }
13                 catch (CommunicationException ex)
14                 {
15                     ((ICommunicationObject)channel).Abort();
16                     channel = null;
17                 }
18                 return GetChannel();
19             }
20             else
21             {
22                 NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
23                 EndpointAddress address = new EndpointAddress("net.tcp://localhost:1234/TestService");
24                 ITest localTest = DuplexChannelFactory<ITest>.CreateChannel(binding, address);                 
25                 ((ICommunicationObject)localTest).Open();
26                 channel = localTest;
27                 return channel;
28             }
29         }

 

发现这种方式下,之前open过的通道遇到超时问题的时候,并不会自动转换为fault状态,必须要有一次方法调用才能转换为fault状态,也就是说超时后,只有调用一次 channel.DoSomeThing();之后才会触发转换为fault状态,而DoSomeThing方法已经是业务逻辑代码了,是不能够失败的.

 

到底如何检测wcf通道是不是挂掉了?

如何更好的提高wcf客户端并发调用的性能?

 

garry的主页 garry | 菜鸟二级 | 园豆:495
提问于:2013-07-09 15:41
< >
分享
所有回答(1)
0
dudu | 园豆:30994 (高人七级) | 2013-07-09 16:11

问题的关键在于 必须要有一次方法调用才能触发通道的TimeOut逻辑

支持(0) 反对(0) garry | 园豆:495 (菜鸟二级) | 2013-07-10 14:03
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册