Skip to main content
 首页 » 编程设计

c#之ICommunicationObject.State 不起作用

2024年02月23日20birdshome

你好,

我使用 DuplexChannelFactory 创建我的 WCF 客户端,问题是当我这样做时:

((ICommunicationObject)this.GetMyServiceInterfaceChannel).State 

我得到以下异常:

无法在“System.ServiceModel.ICommunicationObject”类型的实例上获取字段或调用方法,因为它是远程对象的代理。

为什么?

我需要检查 channel 是否出现故障。

编辑1:

ClientService clientService = new ClientService(); 
                InstanceContext context = new InstanceContext(clientService); 
 
                DuplexChannelFactory<My.ServiceContracts.IMyClientService> factory = new DuplexChannelFactory<My.ServiceContracts.IMyClientService>(context, connectionName); 
 
                factory.Credentials.UserName.UserName = anvandarNamn; 
                factory.Credentials.UserName.Password = password; 
 
 
                return factory.CreateChannel(); 

请您参考如下方法:

转换为 IClientChannel,然后检查状态

但请注意,状态仅在您调用电话时更新。如果您想测试 channel 是否超时,只有在您发出抛出异常的调用后才能进行测试。