我有一个 WCF 服务正在运行,它试图返回一个DataTable
. 服务方法使用 a SqlDataReader
,然后使用DataTable.Load()
将该数据放入DataTable
它打算返回的数据中。
问题: 当服务方法返回一个大表时(我稍后会定义它),我在调试输出中得到这些异常(它们不会削弱服务):
SMDiagnostics.dll 中出现“System.OutOfMemoryException”类型的第一次机会异常
SMDiagnostics.dll 中出现了“System.InsufficientMemoryException”类型的第一次机会异常
“大”的定义: 在我的测试中返回的记录集包含 286760 条记录,当这个表导出为文本时,它的大小大约是 800MB。我知道这都是相对的,所以这一切都可能毫无意义。大多数情况下,我指出这一点是因为在我看来,抛出内存异常似乎相当小,特别是考虑到我正在测试的开发机器有 8GB 内存这一事实。同样,这都是相对的,也许是无关紧要的,但我试图提供足够的信息。
这是我的连接代码:
NetTcpBinding netBind = new NetTcpBinding();
netBind.Security.Mode = SecurityMode.Transport;
netBind.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
netBind.MaxReceivedMessageSize = Int32.MaxValue;
netBind.MaxBufferSize = Int32.MaxValue;
netBind.MaxBufferPoolSize = 0;
netBind.MaxConnections = 300;
netBind.ListenBacklog = 300;
netBind.ReaderQuotas = XmlDictionaryReaderQuotas.Max;
netBind.PortSharingEnabled = true;
netBind.OpenTimeout = new TimeSpan(0, 0, RegistryValues.DatabaseTimeout);
netBind.CloseTimeout = new TimeSpan(0, 0, RegistryValues.DatabaseTimeout);
netBind.ReceiveTimeout = new TimeSpan(0, 5, 0);
netBind.SendTimeout = new TimeSpan(0, 5, 0);
netBind.ReliableSession.InactivityTimeout = new TimeSpan(long.MaxValue);
netBind.TransferMode = TransferMode.Buffered;
uriBuilder = new UriBuilder("net.tcp", connServer, (connPort == -1 ? RegistryValues.ServerPort : connPort), "Data");
epAddress = new EndpointAddress(uriBuilder.Uri);
ChannelFactory<IData> iChannel = new ChannelFactory<IData>(netBind, epAddress);
iChannel.Credentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Identification;
IData svcCon = iChannel.CreateChannel();
((IClientChannel)svcCon).OperationTimeout = new TimeSpan(long.MaxValue);
请注意,我们正在使用Buffered
传输模式。我正在考虑Streamed
替代方案,但这将对代码的其余部分进行彻底的结构更改……如果当前模型中有解决方案,这不是我想做的事情。我再次指出一个事实,即我只是不认为我在推送过多的数据。
此连接是作为创建Sql
类对象(我的类)的一部分而建立的。iChannel
和svcCon
对象在方法被调用时一起处理(Sql.Dispose()
因为我们Sql
使用块创建这些对象using
)。
这Sql.Dispose()
是我的using
块应该触发的方法(以防万一):
public void Dispose()
{
if (this != null && this.connection.State == ConnectionState.Open)
ClearConnectionPool();
try
{
if (iChannel.State != CommunicationState.Faulted)
iChannel.Close();
}
catch { iChannel.Abort(); }
try
{
if (((IClientChannel)svcCon).State != CommunicationState.Faulted)
((IClientChannel)svcCon).Close();
}
catch { ((IClientChannel)svcCon).Abort(); }
}
总之,我正在创建一个 WCF 通信通道,该通道反过来创建一个SqlConnection
. 使用它,我们触发 SQL 调用。所有这些都会尽快处理掉。我们不会在这些连接上挂起超过执行所需数据库操作所需的时间。在极少数情况下,这些东西不是在using
块内创建的,这意味着我很确定我们正在正确清理。当然,除非有人发现我的Dispose()
方法有问题。
任何建议表示赞赏。我可以根据要求提供更多代码。
一些附加信息:这是我在调试客户端并进入服务器代码以观察它尝试返回时发生的情况时 收到的堆栈跟踪DataTable
:
A first chance exception of type 'System.OutOfMemoryException' occurred in SMDiagnostics.dll
A first chance exception of type 'System.InsufficientMemoryException' occurred in SMDiagnostics.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
Step into: Stepping over method without symbols 'System.ServiceModel.Dispatcher.MessageRpc.Process'
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
Step into: Stepping over method without symbols 'System.ServiceModel.Dispatcher.ChannelHandler.DispatchAndReleasePump'
A first chance exception of type 'System.IO.IOException' occurred in System.dll
Step into: Stepping over method without symbols 'System.ServiceModel.Dispatcher.ChannelHandler.HandleRequest'
Step into: Stepping over method without symbols 'System.ServiceModel.Dispatcher.ChannelHandler.AsyncMessagePump'
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
Step into: Stepping over method without symbols 'System.ServiceModel.Diagnostics.Utility.AsyncThunk.UnhandledExceptionFrame'
Step into: Stepping over method without symbols 'System.ServiceModel.AsyncResult.Complete'
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in mscorlib.dll
Step into: Stepping over method without symbols 'System.ServiceModel.Diagnostics.Utility.AsyncThunk.UnhandledExceptionFrame'
Step into: Stepping over method without symbols 'System.ServiceModel.AsyncResult.Complete'
A first chance exception of type 'System.ServiceModel.CommunicationObjectFaultedException' occurred in System.ServiceModel.dll
Step into: Stepping over method without symbols 'System.ServiceModel.Diagnostics.Utility.AsyncThunk.UnhandledExceptionFrame'
Step into: Stepping over method without symbols 'System.Net.LazyAsyncResult.Complete'
A first chance exception of type 'System.ServiceModel.CommunicationObjectFaultedException' occurred in mscorlib.dll
Step into: Stepping over method without symbols 'System.Net.Security.NegotiateStream.ProcessFrameBody'
A first chance exception of type 'System.ServiceModel.CommunicationObjectFaultedException' occurred in System.ServiceModel.dll
Step into: Stepping over method without symbols 'System.Net.Security.NegotiateStream.ReadCallback'
Step into: Stepping over method without symbols 'System.Net.FixedSizeReader.CheckCompletionBeforeNextRead'
Step into: Stepping over method without symbols 'System.Net.FixedSizeReader.ReadCallback'
Step into: Stepping over method without symbols 'System.ServiceModel.AsyncResult.Complete'
Step into: Stepping over method without symbols 'System.ServiceModel.Diagnostics.Utility.IOCompletionThunk.UnhandledExceptionFrame'
Step into: Stepping over method without symbols 'System.Threading._IOCompletionCallback.PerformIOCompletionCallback'