2012-05-09 52 views
2

我有一個Web應用程序託管在Windows Server 2003盒子上的IIS 6中,並且必須處理7-8mb左右的2個大型PDF文件,這些文件由網站從網絡讀取共享和傳遞給WCF服務以保存在別處的字節。系統資源不足以完成請求的服務

這裏是我用來讀取文件的代碼:

public static byte[] ReadFile(string filePath) 
{ 
    int count; 
    int sum = 0; 
    byte[] buffer; 
    FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read); 

    try 
    { 
     int length = (int)stream.Length; 
     buffer = new byte[length]; 

     while ((count = stream.Read(buffer, sum, length - sum)) > 0) 
      sum += count; 

     return buffer; 
    } 
    catch (Exception) 
    { 
     throw; 
    } 
    finally 
    { 
     stream.Close(); 
     stream.Dispose(); 
    } 
} 

錯誤被扔在stream.Read()和錯誤是:存在

系統資源不足,無法完成所要求的服務

此代碼在我的開發環境中工作,但只要我發佈到我們的生產環境中,我們就會收到此錯誤消息。 我已經看到這個錯誤已經出現了幾次搜索,並且這個錯誤是使用File.Move(),但是我們不能這樣做,因爲該文件需要傳遞給WCF服務方法。

在讀取文件時,IIS6中是否有某些內容需要更改以允許在內存中保存15-20mb?還是還有其他需要配置的東西?

任何想法?

回答

1

看到這個:

Why I need to read file piece by piece to buffer?

看來你正在閱讀的整個文件,無需緩衝..

緩衝區=新的字節[長度]

此致敬禮。

+0

while循環正在讀取文件塊是不是? –

+0

我會說你正在讀取整個文件.. int length =(int)stream.Length; buffer = new byte [length]; 從上面的代碼,你認爲「大塊」的大小是多少? ... – Oscar

+0

來自以前發佈的url: public static byte [] ReadFully(Stream stream) byte [] buffer = new byte [8192]; (MemoryStream tmpStream = new MemoryStream()) { int bytesRead; ((bytesRead = stream.Read(buffer,0,buffer.Length))> 0) { tmpStream.Write(buffer,0,bytesRead); } return tmpStream.ToArray(); } } 在這裏,塊是8192字節 – Oscar

相關問題