UPDATE: Since writing this question, I have come across this Bug Report which provides a method of replicating the symptoms of the bug I am seeing consistently.
I cannot, yet, confirm if this is the cause of the bug I am experiencing, but it warrants some investigation. I have not come across a work-around for the bug described in the report yet.
To summarise the report: If the WCF client closes during authentication at the WCF service then an asynchronous exception is thrown at the service, which (in my case) is not caught until it reaches the program.cs file and so makes it difficult to handle gracefully.
Original Question:
I have a WCF Service hosted within a Windows Service.
The WCF service allows clients to subscribe (providing a callback), after which, the service publishes updates to all subscribers.
The service maintains a Dictionary<int, ICallback>
of all current subscribers' callback interfaces.
When an event occurs requiring information to be published, the service loops through the collection and calls the appropriate method defined in ICallback.
This all works fine and all occasions where the ICallback objects are used are wrapped in a try catch in case of object disposed exceptions and any other exceptions which might occur.
The problem I am having is that a Communication Exception is being raised at the Service:
System.ServiceModel.CommunicationException:
The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue.
Local socket timeout was '10675199.02:48:05.4775807'.
System.IO.IOException: The read operation failed, see inner exception.
System.ServiceModel.CommunicationException: The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue.
Local socket timeout was '10675199.02:48:05.4775807'.
System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
This exception is not being caught and is causing the application to crash.
Because it is not being caught, I am assuming that the exception is not being thrown during a time when the service is attempting to publish data because this always occurs within a try - catch.
Could this exception be caused by the client application closing the channel/socket incorrectly?
More importantly, how is it possible to catch an exception raised in this way when the object from which the exception originates is not currently being accessed within a running thread?
i.e. the ICallback object is sat in a dictionary, not being used, when the exception is thrown - so how do I handle the exception? where do I put the try catch?
WCF Service Subscribe Method
public static int Subscribe()
{
int id = -1;
OperationContext currentContext = OperationContext.Current;
if (currentContext != null)
{
ICallback callback = currentContext.GetCallbackChannel<ICallback>();
if (callback != null)
{
id = GetNextId();
lock (SubscriberLock)
{
Subscribers.Add(id, callback);
}
}
}
return id;
}
The exception is thrown some period of time after the completion of this method.
I don't feel totally happy that I have explained the problem very clearly so please ask for clarification if there is a specific point which isn't very clear.
Thanks!