我试图实现对WCF客户端重新连接逻辑。我知道你必须在当前频道进入故障状态后创建一个新频道。我这样做的一个通道故障事件处理程序:WCF回调通道故障
internal class ServiceClient : DuplexClientBase, IServiceClient
{
public ServiceClient(ICallback callback, EndpointAddress serviceAddress)
: base(callback, MyUtility.GetServiceBinding("NetTcpBinding"), serviceAddress)
{
// Open the connection.
Open();
}
public void Register(string clientName)
{
// register to service
}
public void DoSomething()
{
// some code
}
}
public class ClientApp
{
private IServiceClient mServiceClient;
private ICallback mCallback;
public ClientApp()
{
mServiceClient = new ServiceClient(mCallback, new EndpointAddress("someAddress"));
mServiceClient.Register();
// register faulted event for the service client
((ICommunicationObject)mServiceClient).Faulted += new EventHandler(ServiceClient_Faulted);
}
void ServiceClient_Faulted(object sender, EventArgs e)
{
// Create new Service Client.
mServiceClient = new ServiceClient(mCallback, new EndpointAddress("someAddress"));
// Register the EI at Cell Controller
mServiceClient.Register();
}
public void DoSomething()
{
mServiceClient.DoSomething();
}
}
但在我的单元测试我仍然获得了“通信对象,System.ServiceModel.Channels.ServiceChannel,不能用于通信,因为它是在断陷状态“异常。
是否有可能回调通道仍然故障,如果是我怎么能代替回调通道?
你能指定你的单元测试在触发异常时做什么吗? – 2009-10-13 08:57:45
在我的单元测试中,我创建了一个服务主机和一个客户端实例。我在服务上注册客户端,然后通过将参考设置为null来停止服务。之后,我等了20秒(我使用了一个可靠的会话,InactivityTimeout为10秒,所以我确定连接在20秒后失效),然后在客户端调用DoSomething()方法。现在我重新创建服务并期望客户端重新连接。在那一刻,我得到了例外。 – phatoni 2009-10-14 16:30:13