views:

222

answers:

2

How do I test the state of my proxy before making calls to my WCF service.

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.

Is it better to just catch exceptions?

A: 

If you create your client proxy using a DuplexChannelFactory<T>, you should get back a regular old WCF channel:

  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 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??

marc_s
A: 

I've got same problem. I can't cast proxy into ICommunicationObject.

 ICommunicationObject comobj = (ICommunicationObject)proy; 

That line gives me exactly same exception:

Cannot obtain fields or call methods on the instance of type 'System.ServiceModel.ICommunicationObject' because it is a proxy to a remote object. 
Tomas Jecha