WCF Callback Contract InvalidOperationException: Collection has been modified
- by mrlane
We are using a WCF service with a Callback contract. Communication is Asynchronous. The service contract is defined as:
[ServiceContract(Namespace = "Silverlight", CallbackContract = typeof(ISessionClient),SessionMode = SessionMode.Allowed)]
public interface ISessionService
With a method:
[OperationContract(IsOneWay = true)]
void Send(Message message);
The callback contract is defined as
[ServiceContract]
public interface ISessionClient
With methods:
[OperationContract(IsOneWay = true, AsyncPattern = true)]
IAsyncResult BeginSend(Message message, AsyncCallback callback, object state);
void EndSend(IAsyncResult result);
The implementation of BeginSend and EndSend in the callback channel are as follows:
public void Send(ActionMessage actionMessage)
{
Message message = Message.CreateMessage(_messageVersion,
CommsSettings.SOAPActionReceive, actionMessage, _messageSerializer);
lock (LO_backChannel)
{
try
{
_backChannel.BeginSend(message, OnSendComplete, null);
}
catch (Exception ex)
{
_hasFaulted = true;
}
}
}
private void OnSendComplete(IAsyncResult asyncResult)
{
lock (LO_backChannel)
{
try
{
_backChannel.EndSend(asyncResult);
}
catch (Exception ex)
{
_hasFaulted = true;
}
}
}
We are getting an InvalidOperationException: "Collection has been modified" on _backChannel.EndSend(asyncResult) seemingly randomly, and we are really out of ideas about what is causing this. I understand what the exception means, and that concurrency issues are a common cause of such exceptions (hence the locks), but it really doesn't make any sense to me in this situation.
The clients of our service are Silverlight 3.0 clients using PollingDuplexHttpBinding which is the only binding available for Silverlight. We have been running fine for ages, but recently have been doing a lot of data binding, and this is when the issues started.
Any help with this is appreciated as I am personally stumped at this time.