Ever heard of the ICommunicationObject Interface?

By | June 1, 2015

How do you know if a WCF service is up and running if all you have is the channel and you don’t actually have access to the real proxy? The ICommunicationObject Interface provides you with a solution for this exact problem as it provides the developer with a abstract way to open, close and check the state of connections.

The Microsoft site states the following about this interface.

“Defines the contract for the basic state machine for all communication-oriented objects in the system, including channels, the channel managers, factories, listeners, and dispatchers, and service hosts.”

The ICommunicationObject is however not a Service  Contract, it is a interface implemented by the channel. This means that it is not technology agnostic and  you cannot use a utility like the WCF Test Client to check the connection state of the WCF Service.

In the example below i show how to use the ICommunicationObject interface to see the state of a channel. In this sample the developer does not know what service he is dealing with and also does not have access to the real proxy.

protected static void DoSomething(T[] channels)
{
foreach (T channel in channels)
{
if (((ICommunicationObject)channel).State == CommunicationState.)
{

}
}
}

The state property provides the developer with the following.

ICommunicationObject

The ICommunicationObject interface also provides a developer with events that can be used to respond to when a channel is Opened, Closed and Faulted.

innerChannel = Factory.CreateChannel();
((ICommunicationObject)innerChannel).Faulted += new EventHandler(Channel_Faulted);


 

Related

WCF Connection Test Utility
Meet the Channel Model: ICommunicationObject

Leave a Reply

Your email address will not be published.