我有以下代码:
public partial class MyServiceClient : System.ServiceModel.ClientBase<...
if (m_MyClient == null)
m_MyClient = new MyServiceClient
("BasicHttpBinding_IMyService", remoteAddress);
WriteOutput("Successfully connected to service");
我的问题是,我如何知道此时我的客户端实际上已连接到该服务?我想显示失败或成功的消息。
最佳答案
创建客户端后,没有像EndpointNotFoundException
这样的异常已经发生 - 然后您“连接”到服务,这实际上意味着:客户端和服务之间的通信 channel 已准备好用于来回发送消息。这就是全部 - 服务器端还没有任何东西可以真正处理您的调用(除了 channel 监听器,它将在消息到达时被激活)。
您还可以查看客户端 channel 的 .State
property - 理想情况下,它应该是 Opened
那时:
如果您从 ClientBase<T>
派生,请使用此选项
m_MyClient.State == CommunicationState.Opened
或者这是您正在使用由 Add Service Reference
生成的标准客户端类Visual Studio 中的功能:
(m_MyClient as IClientChannel).State == CommunicationState.Opened
关于wcf - System.ServiceModel.ClientBase 连接到服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3128157/