WCF服务通道中出现CommunicationObjectAbortedException异常,同时加载多个数据。

8

我在我的客户端-服务器应用程序中使用WCF服务,但在服务器和客户端之间通信时遇到以下错误。

Error Message =>> System.ServiceModel.CommunicationObjectAbortedException: The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it has been Aborted.

Server stack trace: 
   at System.ServiceModel.Channels.CommunicationObject.ThrowIfDisposedOrNotOpen()
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at ServiceLib.ServiceCallbackInterfaces.IHostServiceCallback.SendEventAndStatus(String message, Boolean isBackupGenerated)
   at ServiceLib.Services.DriversService.objDriver_EventReceived(Object sender, EventReceivedEventArgs e)

应用场景:客户端通过WCF服务并使用nettcp绑定从服务器获取数据。服务器连接实际设备,并每秒生成20-30个事件。生成的事件作为字符串消息通过WCF的回调接口推送到所有连接的客户端。如果客户端没有任何进程负载(仅接收事件并显示),则运行良好。但是,当我们在客户端进行一些加载过程并忙碌一段时间时,会出现错误。
注意:我已经设置了nettcp绑定中的所有超时时间的最大值,但问题仍未解决。
NetTcpBinding tcpBinding = new NetTcpBinding(SecurityMode.None);
                tcpBinding.MaxBufferPoolSize = 2147483647;
                tcpBinding.MaxReceivedMessageSize = 2147483647;
                tcpBinding.MaxBufferSize = 2147483647;
                tcpBinding.ReaderQuotas.MaxStringContentLength = 2147483647;
                tcpBinding.ReaderQuotas.MaxDepth = 2147483647;
                tcpBinding.ReaderQuotas.MaxBytesPerRead = 2147483647;
                tcpBinding.ReaderQuotas.MaxNameTableCharCount = 2147483647;
                tcpBinding.ReaderQuotas.MaxArrayLength = 2147483647;
                tcpBinding.SendTimeout = TimeSpan.MaxValue;
                tcpBinding.ReceiveTimeout = TimeSpan.MaxValue;
                tcpBinding.ReliableSession.InactivityTimeout = TimeSpan.MaxValue;

请指导我是否需要与通信渠道或绑定相关的其他设置?
更新:svc跟踪器抛出的错误:
<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
<System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
<EventID>131075</EventID>
<Type>3</Type>
<SubType Name="Error">0</SubType>
<Level>2</Level>
<TimeCreated SystemTime="2011-09-30T08:37:01.5691715Z" />
<Source Name="System.ServiceModel" />
<Correlation ActivityID="{00000000-0000-0000-0000-000000000000}" />
<Execution ProcessName="ServerUtility" ProcessID="2396" ThreadID="5" />
<Channel />
<Computer>TEST</Computer>
</System>
<ApplicationData>
<TraceData>
<DataItem>
<TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Error">
<TraceIdentifier>http://msdn.microsoft.com/it-IT/library/System.ServiceModel.Diagnostics.ThrowingException.aspx</TraceIdentifier>
<Description>Throwing an exception.</Description>
<AppDomain>ServerUtility.exe</AppDomain>
<Exception>
<ExceptionType>System.ServiceModel.CommunicationException, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
<Message>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'.</Message>
<StackTrace>
at System.ServiceModel.Channels.SocketConnection.EndRead()
at System.ServiceModel.Channels.DelegatingConnection.EndRead()
at System.ServiceModel.Channels.SessionConnectionReader.OnAsyncReadComplete(Object state)
at System.ServiceModel.Channels.SocketConnection.FinishRead()
at System.ServiceModel.Channels.SocketConnection.AsyncReadCallback(Boolean haveResult, Int32 error, Int32 bytesRead)
at System.ServiceModel.Channels.OverlappedContext.CompleteCallback(UInt32 error, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
at System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
</StackTrace>
<ExceptionString>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'. ---&gt; System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
   --- End of inner exception stack trace ---</ExceptionString>
<InnerException>
<ExceptionType>System.Net.Sockets.SocketException, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
<Message>An existing connection was forcibly closed by the remote host</Message>
<StackTrace>
at System.ServiceModel.Channels.SocketConnection.EndRead()
at System.ServiceModel.Channels.DelegatingConnection.EndRead()
at System.ServiceModel.Channels.SessionConnectionReader.OnAsyncReadComplete(Object state)
at System.ServiceModel.Channels.SocketConnection.FinishRead()
at System.ServiceModel.Channels.SocketConnection.AsyncReadCallback(Boolean haveResult, Int32 error, Int32 bytesRead)
at System.ServiceModel.Channels.OverlappedContext.CompleteCallback(UInt32 error, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
at System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
</StackTrace>
<ExceptionString>System.Net.Sockets.SocketException (0x80004005): An existing connection was forcibly closed by the remote host</ExceptionString>
<NativeErrorCode>2746</NativeErrorCode>
</InnerException>
</Exception>
</TraceRecord>
</DataItem>
</TraceData>
<System.Diagnostics xmlns="http://schemas.microsoft.com/2004/08/System.Diagnostics">
<LogicalOperationStack></LogicalOperationStack>
<Timestamp>4730654290080</Timestamp>
</System.Diagnostics>
</ApplicationData>
</E2ETraceEvent>

检查服务是否抛出任何异常-未处理的异常会使通道故障;这可能不是绑定问题。 - Tim
1
按照 @Tim 的说法,要实现这个功能,可以使用 Service Trace Viewer,网址是 http://msdn.microsoft.com/zh-cn/library/ms732023.aspx。 - Jeremy McGee
@JeremyMcGee:我对如何使用服务跟踪查看器没有太多的了解。你能指导一下我吗? - Upendra Chaudhari
@UpendraChaudhari - 请检查Jeremy发布的链接。 - Tim
确实。实质上,您需要调整配置以创建跟踪文件,然后使用跟踪查看器查看。我建议同时追踪客户端和服务器。还值得记住,您也可以跟踪序列化问题:http://msdn.microsoft.com/en-us/library/dd788183(v=bts.70).aspx - Jeremy McGee
显示剩余15条评论
1个回答

4
正如您在问题中提到的,您已将所有超时设置为最大值。因此,在我看来,您定义的InstanceContextMode可能存在问题。您在服务中定义了哪种模式?PerSession、PerCall还是Single?如果您定义了PerCall并且使用相同对象(服务的全局声明对象)调用服务,则可能会导致通道进入故障状态或连接断开。

因此,如果您定义了PerSession模式,则从客户端使用单个对象(全局声明)获取来自服务的数据。如果您定义了PerCall选项,则每次创建新对象。

希望这可以帮助您。请随时提出任何疑问......


我有多个服务,其中一个名为 HostService 的主要服务具有 PerSession 模式,而所有其他服务都具有 PerCall 模式。在主服务中,我使用静态哈希表维护连接的客户端列表,因为我需要使用回调方法向所有客户端推送事件。因此,我将所有已连接客户端的上下文对象存储在该列表中。在客户端方面,我每次都会创建新对象来调用服务。 - Upendra Chaudhari
请问你能描述一下如何向所有客户端发送事件通知吗?我的意思是,你可以提供一些代码吗?还有一件事是,当你向客户端发送事件通知时,同时调用了另一个以 PerCall 模式运行的服务? - Chief
正如我之前告诉你的那样,我正在使用回调方法发送事件通知。当我在客户端应用程序中更改工作模式(监督员、设置、离线)时,我会调用EventLogService来保存工作模式数据,以便它作为服务器通知显示给所有连接的客户端。 - Upendra Chaudhari
您能否通过将PerSession模式更改为EventLogService,并使用全局声明的对象在客户端调用服务来检查它。 - Chief
是的,我通过执行 PerSession 和全局对象进行了检查,一切都正常。我不知道实际问题是什么,但现在对我来说一切正常。感谢您的支持。 - Upendra Chaudhari

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接