2013-10-02 54 views
1

基本上,我从SQL Server中获取数据。如果我选择所有的数据(这大约是80000行),然后我收到以下错误(接收100行工作正常)
WCF,序列化大量数据时内存不足异常

Exception of type 'System.OutOfMemoryException' was thrown. <br/> 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. <br/> 

Exception Details: System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown. 

Stack Trace: 

[OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.] 
    System.Text.StringBuilder.ToString() +29 
    System.IO.StringWriter.ToString() +14 
    System.Net.WebUtility.HtmlEncode(String value) +110 
    System.Web.Util.HttpEncoder.HtmlEncode(String value) +54 
    System.Web.UI.HtmlControls.HtmlContainerControl.set_InnerText(String value) +24 
    PerformanceCompare._Default.Page_Load(Object sender, EventArgs e) in C:\Users\KK33562\Documents\Visual Studio 2010\Projects\TestWebService\PerformanceCompare\Default.aspx.cs:34 
    System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 
    System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 
    System.Web.UI.Control.OnLoad(EventArgs e) +91 
    System.Web.UI.Control.LoadRecursive() +74 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207 

Web.config文件(客户端)

<system.serviceModel> 
<behaviors> 
    <endpointBehaviors> 
    <behavior name="endpointbehaviour"> 
     <dataContractSerializer maxItemsInObjectGraph="2147483647"/> 
    </behavior> 
    </endpointBehaviors> 
</behaviors> 
<bindings> 
    <basicHttpBinding> 
    <binding name="BasicHttpBinding_IService1" closeTimeout="00:50:00" 
     openTimeout="00:50:00" receiveTimeout="00:50:00" sendTimeout="00:50:00" 
     allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
     maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647" 
     messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" 
     useDefaultWebProxy="true"> 
     <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" 
     maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
     <security mode="None"> 
     <transport clientCredentialType="None" proxyCredentialType="None" 
      realm="" /> 
     <message clientCredentialType="UserName" algorithmSuite="Default" /> 
     </security> 
    </binding> 
    </basicHttpBinding> 
</bindings> 
<client> 
    <endpoint address="http://localhost:53268/Service1.svc" binding="basicHttpBinding" 
    bindingConfiguration="BasicHttpBinding_IService1" contract="WCFService.IService1" 
    name="BasicHttpBinding_IService1" behaviorConfiguration="endpointbehaviour" /> 
</client> 
</system.serviceModel> 

Web.config文件(服务器端)

<system.serviceModel> 
<behaviors>  
    <serviceBehaviors> 
    <behavior name="TestWCF.Service1Behavior"> 
     <dataContractSerializer maxItemsInObjectGraph="2147483646"/> 
     <serviceMetadata httpGetEnabled="true"/> 
     <serviceDebug includeExceptionDetailInFaults="true"/> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 
    <services> 
    <service name="TestWCF.Service1" behaviorConfiguration="TestWCF.Service1Behavior"> 
     <endpoint address="" binding="basicHttpBinding" contract="TestWCF.IService1" bindingConfiguration="BasicHttpBinding_IService1"> 
     <identity> 
      <dns value="localhost"/> 
     </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
    </service> 
    </services>  
<bindings> 
    <basicHttpBinding> 
    <binding name="BasicHttpBinding_IService1" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" openTimeout="00:50:00" closeTimeout="00:50:00" sendTimeout="00:50:00" receiveTimeout="00:10:00"> 
     <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" 
     maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> 
    </binding> 
    </basicHttpBinding> 
</bindings> 
</system.serviceModel> 
+0

似乎很简单:“没有足够的内存来继续执行程序时引发的异常”http://msdn.microsoft.com/en-us/library/system.outofmemoryexception.aspx –

+0

@ DaveZiegler感谢您的回应!我知道我有一个内存管理问题。我的问题是,我该如何解决这个问题。 – Adrian

+0

那么,你返回多少数据?有多少内存可用?我不认为这是你可以在web.config中修复的东西。你能带回更少的数据吗?或者你需要一次全部80000+行? –

回答

0

如果你从你的服务返回80K行话,我想你最好摆脱服务的都在一起。只需让客户端直接访问数据库,然后他们就可以从数据库中提取所需的所有行。

编辑

介绍服务边界,它没有必要是一个非常常见的错误,和一个我看到的情况越来越多。在大多数情况下,让客户端直接使用NHibernate甚至ADO以及存储过程或视图来调用数据库会更清晰。

如果您的客户端无法在物理上看到网络上的数据库,那么您可能必须使用服务。

+0

感谢您的回应!你能否提供一些例子?我对“一起摆脱所有服务的含义感到困惑,只是让客户端直接访问数据库”...我想保持我的客户端清洁.. – Adrian

+0

查看我的答案更新 –