首页 > 解决方案 > SAP 中间件连接器错误

问题描述

我正在开发一个使用 SAP AG(版本 3.0.2)的 SAP.Middleware.Connector 的应用程序。

此应用程序在后台执行 RFC(使用 Task.Run),但几分钟后,应用程序崩溃,显示此图像中的堆栈跟踪。

图像


DefaultDomain 中未捕获的异常,堆栈跟踪:System.UnhandledExceptionEventArgs

未处理的异常:System.InvalidOperationException:集合已修改;枚举操作可能无法执行。

在 System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource 资源) 在 System.Collections.Generic.Dictionary'2.KeyCollection.Enumerator.MoveNext() 在 SAP.Middleware.Connector.RfcSessionManager.UpdateSessions(Object state) 在 System.Threading.TimerQueueTimer.CallCallbackInContext (对象状态)在 System.Threading.Executioncontext.RunInternal(ExecutionContext executionContext, ContextCallback, Object state, Boolean preserveSyncCtx) 在 System.Threading.Executioncontext.Run(ExecutionContext executionContext, ContextCallback, Object state, Boolean preserveSyncCtx) 在 System.Threading.TimerQueueTimer .CallCallback() 在 System.Threading.TimerQueueTimer.Fire() 在 System.Threading.TimerQueueTimer.FireNextTimers() 在 System.Threading.TimerQueueTimer.AppDomainTimerCallback()

这是 RFC 执行的代码:

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);
});

拜托,你能帮忙吗?

标签: c#sap

解决方案


谢谢您的帮助!

我正在回答这个问题。如果您在Thread上使用BeginContextEndContext,连接器将尝试更新连接,但现在包含连接的线程已经消失,导致错误,Collection 已修改;枚举操作可能无法执行。

解决方案是删除BeginContextEndContext行,一切正常。


推荐阅读