I'm developing an application which uses an SAP.Middleware.Connector from SAP AG (version 3.0.2).
This application executes an RFC in the background (using Task.Run) but after a few minutes, the app crashes:
ArgumentException caught in DefaultDomain: Collection was modified; enumeration operation may not execute . Uncaught Exception in DefaultDomain Unhandled Exception: System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) at System.Collections.Generic.Dictionary'2.KeyCollection.Enumerator.MoveNext() at SAP.Middleware.Connector.RfcSessionManager.UpdateSessions(Object state) at System.Threading.TimerQueueTimer.CallCallbackInContext(Object state) at System.Threading.Executioncontext.RunInternal(ExecutionContext executionContext, ContextCallback, Object state, Boolean preserveSyncCtx) at System.Threading.Executioncontext.Run(ExecutionContext executionContext, ContextCallback, Object state, Boolean preserveSyncCtx) at System.Threading.TimerQueueTimer.CallCallback() at System.Threading.TimerQueueTimer.Fire() at System.Threading.TimerQueueTimer.FireNextTimers() at System.Threading.TimerQueueTimer.AppDomainTimerCallback()
This is the code of the RFC execution:
return Task.Run(() =>
ConexionSAP cSap = new ConexionSAP();
cSap.ConectaSAP();
RfcDestination rfcDest = RfcDestinationManager.GetDestination(cSap.rfc);
RfcRepository rfcRep = rfcDest.Repository;
IRfcFunction function;
function = rfcRep.CreateFunction("XXXNAMEOFTHEFUNCTIONXXX");
//import parameters
RfcSessionManager.BeginContext(rfcDest);
function.Invoke(rfcDest);
//export parameters
RfcSessionManager.EndContext(rfcDest);
});
Please, can you help please with this?
Thank you for your help!
I'm running on the answer of this. If you use BeginContext and EndContext on a Thread, the connector will try to renew the connection but now the thread containing the connection its gone causing the error, Collection was modified; enumeration operation may not execute.
The solution was to delete the BeginContext and EndContext line, everything is fine and working.