Reputation: 35915
I have two application connected by a duplex WCF connection. I works well, as long the connection is consistent.
I am checking now how to handle the re-connecting scenarios, when the connection is lost and it has to reconnect again. And I'm struggling to understand how work this out in WCF.
As far as I knew, the IChannel
is expendable, but the ChannelFactory
is expensive. So I create one factory, and then the channel. Whenever I detect the Closed
or Faulted
events in the channel, I try
to close the channel, deattach the event handlers and then create another channel.
But this approach is not working very well, because sometimes the DuplexChannelFactory<T>.CreateChannel
gets faulted as well, and throws this exception:
System.ServiceModel.CommunicationObjectAbortedException occurred
HResult=-2146233087
Message=The communication object, System.ServiceModel.InstanceContext, cannot be used for communication because it has been Aborted.
How is this possible that the factory itself gets faulted this way?
What is the right approach to handle disconnections/reconnections in WCF?
Upvotes: 2
Views: 425
Reputation: 2788
I would not say it is the right way to do things but....
My approach to handle disconnections/reconnections is to use a timer (on the client) to call a "ping" method on the service over the existing connection and recreate it if needed. No idea if there is a better way, not that I didn't look.
My problem was that the connection was being lost silently and the client ends up listening to a dead line and missing out on notifications.
Upvotes: 1
Reputation: 704
Trying to answer this part:
"How is this possible that the factory itself gets faulted this way?"
If you are IIS hosting your application your app pool could get recycled resulting in this behavior. You check the logs to confirm if that is the root cause.
Upvotes: 0