【发布时间】:2010-12-06 01:03:52
【问题描述】:
我正在尝试为 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,不能用于通信,因为它处于故障状态”异常。
回调通道是否仍有故障,如果是,我该如何更换回调通道?
【问题讨论】:
-
你能指定你的单元测试在触发异常时做什么吗?
-
在我的单元测试中,我创建了一个服务主机和一个客户端实例。我在服务上注册客户端,然后通过将引用设置为 null 来停止服务。之后我等待 20 秒(我正在使用 InactivityTimeoutof 10 秒的可靠会话,所以我确定连接在 20 秒后丢失),然后我在客户端上调用 DoSomething() 方法。现在我重新创建服务并期望客户端重新连接。在那一刻,我得到了例外。