WCF基本知识
1.开通WCF调试服务:
须在服务端的行为中作如下配置:includeExceptionDetailInFaults="true" 代码如下:
<behaviors>
<serviceBehaviors>
<behavior name="MyBehavior">
<serviceDebug includeExceptionDetailInFaults="true" ></serviceDebug>
</behavior>
</serviceBehaviors>
</behaviors>
2.关于WCF客户端信道异常的处理方法,须在catch中新建,且体代码如下:
ChannelFactory<IContract> factory=new ChannelFactory<IContract>("myclient");
IContract proxy=factory.CreateChannel();
try
{
proxy.GoFunction();
}
catch(Exception ex)
{
if(proxy.InnerChannel.State == CommunicationState.Faulted)
{
factory=new ChannelFactory<IContract>("myclient");
// proxy=factory.CreateChannel();
}
}
3.当WCf服务出现TimeOutException或CommunicationException时,当前信道壮态变成Faulted,表示信道出现错误,
将使信道不能用于后续通信,即使调用close方法关闭也不行,此时客户端须调用Abort方法强制中断该信道。
using(ChannelFactory<IContract> factory=new ChannelFactory<IContract>("MyClient"))
{
IContract proxy=factory.CreateChannle();
try
{
proxy.GoFunction();
(proxy as ICommunicationObject).Close(); // 这里是关闭信道
}
catch(CommunicationException ex)
{
(proxy as ICommunicationObject).Abort(); // 强制杀死信道
}
catch(TimeoutException ex)
{
(proxy as ICommunicationObject).Abort(); // 强行释放信道
}
catch(Exception ex)
{
LogError.Message(ex.Message);
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步