2012-01-13 132 views
1

當我通過HTTP 傳輸文件大約50MB的有時我得到這個錯誤:無法從傳輸連接讀取數據:一個現有的連接被強行關閉遠程主機

無法讀取數據傳輸連接:現有連接被遠程主機強制關閉。

首先,我找不到http://stackoverflow.com下的任何解決方案。

任何線索我必須改進/改變?

的app.config:

<bindings> 
     <basicHttpBinding> 
     <binding name="BasicHttpBinding_MyDomainServicesoap" closeTimeout="00:03:00" openTimeout="00:04:00" receiveTimeout="00:10:00" sendTimeout="00:05:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="50000000" maxBufferPoolSize="50000000" maxReceivedMessageSize="50000000" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> 
      <readerQuotas maxDepth="64" maxStringContentLength="16384" maxArrayLength="32768" maxBytesPerRead="8192" maxNameTableCharCount="16384"/> 
      <security mode="None"> 
      <transport clientCredentialType="None" proxyCredentialType="None" realm=""/> 
      <message clientCredentialType="UserName" algorithmSuite="Default"/> 
      </security> 
     </binding> 
     <binding name="BasicHttpBinding_MyAuthenticationDomainServicesoap" closeTimeout="00:03:00" openTimeout="00:04:00" receiveTimeout="00:10:00" sendTimeout="00:05:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="50000000" maxBufferPoolSize="50000000" maxReceivedMessageSize="50000000" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> 
      <readerQuotas maxDepth="64" maxStringContentLength="16384" maxArrayLength="32768" maxBytesPerRead="8192" maxNameTableCharCount="16384"/> 
      <security mode="None"> 
      <transport clientCredentialType="None" proxyCredentialType="None" realm=""/> 
      <message clientCredentialType="UserName" algorithmSuite="Default"/> 
      </security> 
     </binding> 
     </basicHttpBinding> 
    </bindings> 

代碼:

private void FinishWebRequest(IAsyncResult result) 
     { 
      // Assign values to these objects here so that they can be referenced in the finally block 
      Stream remoteStream = null; 
      Stream localStream = null; 
      WebResponse response = null; 

      try 
      { 
       response = request.EndGetResponse(result); 

       if (response != null) 
       { 
        // Once the WebResponse object has been retrieved, get the stream object associated with the response's data 
        remoteStream = response.GetResponseStream(); 

        // Create the local file 
        string pathToSaveFile = Path.Combine(FileManager.GetFolderContent(), FileView.Filename); 
        localStream = File.Create(pathToSaveFile); 

        WinAPI.SYSTEM_INFO sysinfo = new WinAPI.SYSTEM_INFO(); 
        WinAPI.GetSystemInfo(ref sysinfo); 

        // Allocate a buffer 
        byte[] buffer = new byte[int.Parse(sysinfo.dwPageSize.ToString())];  
        int bytesRead; 

        // Simple do/while loop to read from stream until no bytes are returned 
        do 
        { 
         // Read data (up to 1k) from the stream 
         bytesRead = remoteStream.Read(buffer, 0, buffer.Length); 

         // Write the data to the local file 
         localStream.Write(buffer, 0, bytesRead); 

         // Increment total bytes processed 
         BytesProcessed += bytesRead; 
        } while (bytesRead > 0); 

        FileView.Downloaded = DateTime.Now; 

        if (BytesProcessed > 0) 
        { 
         FileView.IsSuccess = true; 
         FileView.IsDownloading = false; 
        } 
        else 
        { 
         FileView.IsSuccess = false; 
         FileView.IsDownloading = false; 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       #region Error 
       LogEntry l = new LogEntry(); 
       l.Message = string.Format("{0}", ex.Message); 
       l.Title = "FinishWebRequest() Error"; 
       l.Categories.Add(Category.General); 
       l.Priority = Priority.Highest; 

       if (ex.InnerException != null) l.ExtendedProperties.Add("InnerException", ex.InnerException.Message); 

       CustomLogger.CustomLogger.WriteErrorLog(l); 
       #endregion 
      } 
      finally 
      { 
       // Close the response and streams objects here to make sure they're closed even if an exception is thrown at some point 
       if (response != null) response.Close(); 
       if (remoteStream != null) remoteStream.Close(); 
       if (localStream != null) localStream.Close(); 
      } 
     } 
+1

你有沒有想過圍繞一個使用(Stream remoteStream = new Stream)包裝remoteStream {}類似的東西也爲你的localStream做同樣的事情..確保你沒有聲明全球其他地方的流.. as以及.. – MethodMan 2012-01-13 18:28:34

+0

@DJKRAZE Ty!我會考慮的! – 2012-01-13 18:30:01

+1

我看着你的代碼,沒有什麼突出的..想知道是否可能有一個時間,我懷疑..也當你通過代碼你是否注意到任何不尋常的東西你嘗試了https綁定以及.. – MethodMan 2012-01-13 18:32:27

回答

2

我不知道它是否適用於所有情況的解決方案。

但是當我減少並行連接數量到服務器下載大文件從3到2只有沒有錯誤。

(早期我用3個平行連接。)

1

我遇到了類似的問題,並能夠通過添加行爲部分,我的web.config它告訴數據合同串行覆蓋默認的64KB來解決它大小限制。

<behaviors> 
    <endpointBehaviors> 
    <behavior> 
     <dataContractSerializer maxItemsInObjectGraph="2147483647"/> 
    </behavior> 
    </endpointBehaviors> 
</behaviors> 

此外,在配置增加後,我開始在客戶端,這是同樣的問題,得到一個錯誤,你需要這個配置添加到您的服務和客戶端兩種。

相關問題