Reputation: 53
I'm using the ChannelFactory to open/manage WCF channels (vs. the client proxy). However, when an exception occurs, the factory state doesn't report that the channel is faulted...
ChannelFactory<IContract> factory
= new ChannelFactory<IContract>("NetTcpBinding_IContract",
new EndpointAddress("net.tcp://localhost:8509/WCFSvc"));
try
{
IContract contrct = factory.CreateChannel();
contrct.DoWork(); //throws a non-FaultContract<ExceptionDetail>() exception
factory.Close();
}
catch (Exception)
{
CommunicationState s = factory.State; //returns CommunicationState.Opened
}
Where IContract.DoWork() looks like:
[FaultContract(typeof(ExceptionDetail))]
void DoWork();
I would have expected that the factory state returned CommunicationState.Faulted in the catch(...) block.
In the end, I'm looking for a way to accurately get the channel state when using a ChannelFactory vs. ClientProxy cuz I want to preserve session state if possible; and not cycle the entire session if a legit FaultContact<ExceptionDetail> comes through...
When a legit FaultContact<ExceptionDetail> is raised, the channel is still valid & I can continue to use it as expected. BUT when a non-FaultContract<ExceptionDetail> is raised, the channel is not usable and should be cycled. However, in both cases the .State property is returning CommunicationState.Open so there isn't a nice way to tell if the channel should be cycled or not...
Once a non-FaultContract is thrown, future calls through the channel raise a "the channel is faulted" error even while the .State property stubbornly reports the channel is in an Opened state.
Thanks in advance for any input/ideas/pointers/thoughts,
T
Upvotes: 3
Views: 2936
Reputation: 28530
I believe you need to check the state of the channel, not the factory. You can do this by casting the channel to ICommunicationObject
, which is implemented both in the channel and the factory.
Something like this:
ChannelFactory<IContract> factory
= new ChannelFactory<IContract>("NetTcpBinding_IContract",
new EndpointAddress("net.tcp://localhost:8509/WCFSvc"));
IContract contrct;
try
{
contrct = factory.CreateChannel();
contrct.DoWork(); //throws a non-FaultContract<ExceptionDetail>() exception
factory.Close();
}
catch (Exception)
{
//CommunicationState s = factory.State; //returns CommunicationState.Opened
CommunicationState s = ((ICommunicationObject)contrct).State;
}
Upvotes: 9