我們的站點要提供PDF資源給用戶下載,其中有一些PDF文件較大,運營一段時間出現IIS進程CPU占用率高,內存使用量增加,客戶端的表現就是輸出白屏。重啟IIS後, 才能正常對外提供服務。
原因:
當 ASP.NET 輔助進程(Aspnet_wp.exe,對於在 Internet 信息服務 6.0 [IIS] 上運行的應用程序,則為 W3wp.exe)執行文件下載請求時,向 Microsoft Internet 信 息服務進程(Inetinfo.exe 或 Dllhost.exe)發送數據。
根據計算機的配置,IIS 進程可能會處理數據,也可能會將數據緩存在內存中。如果文件太大,在這兩個進程相互通信的過程中,數據將被緩存在內存中。這可能會導致 服務器上的內存使用量增加。出現此錯誤的原因是 Web 服務器上的內存限制。
解決方法:
解決方法1:
大文件切割成小數據塊,然後逐步添加到輸出流,MSDN上給出的代碼樣例
Code
System.IO.Stream iStream = null;
// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[10000];
// Length of the file:
int length;
// Total bytes to read:
long dataToRead;
// Identify the file to download including its path.
string filepath = "DownloadFileName";
// Identify the file name.
string filename = System.IO.Path.GetFileName(filepath);
try
{
// Open the file.
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
System.IO.FileAccess.Read,System.IO.FileShare.Read);
// Total bytes to read:
dataToRead = iStream.Length;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
// Read the bytes.
while (dataToRead > 0)
{
// Verify that the client is connected.
if (Response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, 0, 10000);
// Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length);
// Flush the data to the HTML output.
Response.Flush();
buffer= new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
}
catch (Exception ex)
{
// Trap the error, if any.
Response.Write("Error : " + ex.Message);
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
}
}
解決方法2:
把站點的Web.config文件中的<compilation debug="true" batch="false">配置節修改為:<compilation debug="false" batch="false">
MSDN上的解釋:
當您在 ASP.NET 應用程序的 Web.config 文件中將編譯元素的 debug 屬性值設置為 false 時,必須針對要下載的文件大小將 Server.ScriptTimeout 屬性設置為適當 的值。默認情況下,Server.ScriptTimeout 值被設置為 90 秒。但是,當 debug 屬性被設置為 true 時,Server.ScriptTimeout 值將被設置為一個非常大的值 (30,000,000 秒)。作為一名開發人員,您必須知道這可能會對您的 ASP.NET Web 應用程序的行為造成的影響。
由於開發環境在我們建立Web應用的時候會默認將Web.config的這一配置節修改為可調試的狀態,這將降低web應用程序的性能,所以我們在部署的時候常常會忽略掉這個 配置。