How to detect a WCF session crash before calling a contract method?
I am using a session mode for my WCF service. The problem is the following: if session is broken and no longer exists, client can't know it before calling a contract.
For example, if the service has been restarted, the client's session id is invalid, because that session has been closed on the server side.
I check the channel state before calling the contract and its value is CommunicationState.Opened even if session is already broken. So, when I call the contract after this check I get a CommunicationException with this message:
The remote endpoint no longer recognizes this sequence. This is most likely due to an abort on the remote endpoint. The value of wsrm:Identifier is not a known Sequence identifier. The reliable session was faulted.
Is there any workaround? I need a way to g开发者_开发问答et an appropriate session state before calling a contract so that I can restore it without getting an exception.
P.S. The CommunicationException type is general, so I can't detect a session crash by catching this exception.
P.P.S. I have asked the similar question here, but in that case I didn't know the reason, now I don't know how to evade it.
No, there is no workaround - all you can (and should do) is use proper defensive programming principles to be able to catch and handle those kind of exceptions as they happen.
If the server crashes or the network goes down, unfortunately, there's no mechanism to inform all potential clients of this case - they'll just find out the next time they try to call.
Update: yes, the CommunicationException
is just the common base class for all exceptions related to WCF - check out the MSDN docs to see about all the descendant exceptions you can catch to be more specific - EndpointNotFoundException
, FaultException
(or FaultException<T>
), ProtocolException
and many many more!
精彩评论