简体   繁体   English

WCF客户端代理状态

[英]WCF Client Proxy State

How do I test the state of my proxy before making calls to my WCF service. 在调用WCF服务之前,如何测试代理的状态。

I have a duplex channel created using a duplex channel factory. 我有一个使用双工通道工厂创建的双工通道。

Before making any calls to the server I want to check the state of the proxy object created from the channel factory. 在对服务器进行任何调用之前,我要检查从通道工厂创建的代理对象的状态。

I saw this in a book... (to be used in immediate window) 我在一本书中看到了...(将在即时窗口中使用)

? ((ICommunicationObject)flsProxy).State

But it gave this exception... 但是它给了这个例外...

Cannot obtain fields or call methods on the instance of type 'System.ServiceModel.ICommunicationObject' because it is a proxy to a remote object. 无法获取类型'System.ServiceModel.ICommunicationObject'的实例上的字段或调用方法,因为它是远程对象的代理。

Is it better to just catch exceptions? 只捕获异常更好吗?

If you create your client proxy using a DuplexChannelFactory<T> , you should get back a regular old WCF channel: 如果使用DuplexChannelFactory<T>创建客户端代理,则应返回常规的旧WCF通道:

  Callbacks myCallbacks = new Callbacks();

  DuplexChannelFactory<IMyService> factory =
     new DuplexChannelFactory<IMyService>(myCallbacks,
        new NetTcpBinding(), new EndpointAddress(.....));

  IMyService proxy = factory.CreateChannel();

and you should be able to cast that to a ICommunicationObject and check its state: 并且您应该能够将其ICommunicationObject转换为ICommunicationObject并检查其状态:

  ICommunicationObject comobj = (ICommunicationObject)proy;

  if(comobj.State != CommunicationState.Faulted)
  {
      // call the service method
  }

Where in this chain of statements does it no longer work for you?? 在此语句链中的哪个位置不再对您有用??

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM