2013-10-22 114 views
14

我從一個WCF Web服務檢索數據,當數據超過0.2億張唱片,我得到一個例外,這是爲下:System.ServiceModel.CommunicationException:基礎連接已關閉

System.ServiceModel.CommunicationException: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server. ---> System.Net.WebException: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host 
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) 
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) 
--- End of inner exception stack trace --- 
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) 
at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size) 
at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead) 
--- End of inner exception stack trace --- 
at System.Net.HttpWebRequest.GetResponse() 
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout) 
--- End of inner exception stack trace --- 

Server stack trace: 
at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason) 
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout) 
at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout) 
at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout) 
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) 

我的網站.config看起來像這樣:

<basicHttpBinding> 
    <binding name="BasicHttpBinding_IService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
      maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" 
      allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
      messageEncoding="Text" textEncoding="utf-8" transferMode="StreamedResponse" useDefaultWebProxy="true"> 
    <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" 
        maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> 
    <security mode="None"> 
     <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> 
     <message clientCredentialType="UserName" algorithmSuite="Default" /> 
    </security> 
    </binding> 
    <binding maxReceivedMessageSize="2147483647" allowCookies="true"> 
    <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" 
maxNameTableCharCount="2147483647" /> 
    </binding> 
</basicHttpBinding> 

我確定我的web服務正在從數據庫中檢索數據。但無法從我發起呼叫的位置轉移到我的網站。預先感謝您的幫助。

+0

增加配置文件中的連接超時。 – gleng

+0

在Web.config中提到的綁定中增加maxBufferSize,maxReceivedMessageSize –

回答

15

我最近在.NET 4.0 web應用程序中遇到了與Wcf服務相同的問題。我增加了maxItemsInObjectGraph值,服務器不再拋出異常。文檔here表示默認最大值爲Int32.MaxValue但我認爲這是不正確,最大值爲65535

<serviceBehaviors> 
    <behavior name="ServiceBehaviour"> 
     <dataContractSerializer maxItemsInObjectGraph="6553500"/> 
     <serviceMetadata httpGetEnabled="true" /> 
     <serviceDebug includeExceptionDetailInFaults="true" /> 
    </behavior> 
    </serviceBehaviors> 

你可以做的另一件事是啓用跟蹤。這是什麼我在web.config中的例子:

<system.diagnostics> 
    <sources> 
    <source name="System.ServiceModel" 
      switchValue="Information, ActivityTracing" 
      propagateActivity="true"> 
     <listeners> 
     <add name="traceListener" 
      type="System.Diagnostics.XmlWriterTraceListener" 
      initializeData= "c:\temp\log\Traces.svclog" /> 
     </listeners> 
    </source> 
    </sources> 
</system.diagnostics> 

如果您在Traces.svclog雙擊,窗口應該打開Microsoft服務跟蹤查看。

如果使用Microsoft WCF Test Client測試您的服務,請確保更改默認的客戶端配置以匹配服務器設置(這包括客戶端端點行爲)以確保客戶端可以接收來自服務器的響應。

我希望這會有所幫助。

+0

我和OP有類似的問題,增加''爲我解決了這個問題。 –

+1

我也有類似的問題。使用服務跟蹤日誌我發現我的DTO類的序列化錯誤... – GiveEmTheBoot

+0

鏈接中的文檔是System.Runtime.Serialization類。以下是服務行爲的鏈接,其中顯示最大值爲65536 http://msdn.microsoft.com/zh-cn/library/ms731809(v=vs.100).aspx – mmeasor

2

使您的客戶端basicHttpBinding與您的服務器綁定相同。這是一個嚴重的常見錯誤,只能改變一個綁定而不是服務器和客戶端綁定。

如果不工作,你可以啓用WCF跟蹤: wcf tracing

這會給你的下屬是什麼問題,更多的見解。

0

我們在返回的對象圖中有一個循環。我知道這可能不是你的問題,但我在這裏添加它,以防其他人有同樣的問題。我們已啓用includeExceptionDetailInFaults,但未在任何客戶端(我們的應用程序或WCF測試客戶端)中獲取錯誤。幸運的是它出現在服務器日誌中,所以我們能夠以這種方式找到它。

我們有雙向導航的父 - >子和父 - >父,我們不得不打斷那個連接,而是有父 - >子,並且孩子有一個ID來查找父母,然後錯誤發生了遠。

希望這可以幫助別人!

0

將以下配置添加到您的wcf服務配置中。並查看c:\ log \ Traces.svclog文件。 我的例外被拋棄;

嘗試序列化參數時發生錯誤 InnerException消息'Enum value'0'對於'ThyCams2014.XrmBase.new_filingstatu'類型無效且無法序列化。如果類型具有DataContractAttribute attrienter代碼herebute,則確保存在必要的枚舉值並使用EnumMemberAttribute屬性標記。'。有關更多詳細信息,請參閱InnerException。

<configuration> 
    <system.diagnostics> 
     <sources> 
      <source name="System.ServiceModel" 
        switchValue="Information, ActivityTracing" 
        propagateActivity="true"> 
      <listeners> 
       <add name="traceListener" 
        type="System.Diagnostics.XmlWriterTraceListener" 
        initializeData= "c:\log\Traces.svclog" /> 
      </listeners> 
     </source> 
     </sources> 
    </system.diagnostics> 
</configuration> 
相關問題