Search code examples
c#.netwcfchannelfactory

TChannel state change events?


hopefully a simple WCF beginners question..

I have a WCF channel factory, returning a service proxy TChannel:

// setup connection to server
var endpointAddress = new EndpointAddress(GetAppSetting("Endpoint"));
var tcpBinding = new NetTcpBinding();
channelFactory = new DuplexChannelFactory<IExcelServer>(this, tcpBinding, endpointAddress);
server = channelFactory.CreateChannel();

I would like to know when this service proxy changes state (Faulted, Closed etc). I can see events on the ChannelFactory itself, however I'm not sure this is the same as the channel itself, and even here stopping the server process doesn't cause a state transition.

This is a CallbackContract service, and in almost all interactions the server is sending data to the client. Therefore I can't simply rely on catching the failure when I make a server call from the client.

Should I send a heartbeat from client to server to trigger the state change?


Solution

  • The instance you get back from CreateChannel implements TChannel but also IClientChannel which has state changed events like Closed, Closing, Opened, etc.:

    server = channelFactory.CreateChannel();
    ((IClientChannel)server).Faulted += FaultedHandler;
    

    P.S: As you pointed out, the states of the channel and channel factory are related but not the same. If a Channel is faulted it doesn't necessarily mean that the ChannelFactory is.