I’m writing some code for a WCF ChannelFactory Manager. My question comes from the event ” ChannelFactory.Faulted”: Both parameter (“object sender” and “EventArgs args” ) contain no info about the original fualt, How can I get ChannelFactory Fault detail in a customized fault handler?
private ChannelFactory CreateFactoryInstance<T>(string endpointConfigurationName, string endpointAddress)
{
ChannelFactory factory = null;
factory = new ChannelFactory<T>(endpointConfigurationName, new EndpointAddress(endpointAddress));
//Customizing Factory Fault handler
factory.Faulted += FactoryFaulted;
factory.Open();
return factory;
}
private void FactoryFaulted(object sender, EventArgs args)
{
ChannelFactory factory = (ChannelFactory)sender;
factory.Close();
//...
//How can I get more Fault detail, so as to throw a meaningful Exception?
throw new ApplicationException("Failure in ChannelFactory ");
}
Thanks for your attention.
I had a brief look around with ILSpy and
ChannelFactoryderives fromCommunicationObjectwhich can transition into a faulted state when an exception is thrown duringOpen/BeginOpen. So try/catch aroundOpenwill catch at least some of the possible errors. Not sure if there are other places where the channel factory can get into faulted state. Note that theFaultedevent handler is called in the finally block which means it is called before you catch the exception.