我在 Windows Server 2003 机器上的 IIS 6 中托管了一个 Web 应用程序,必须处理 2 个大约 7-8mb 的大型 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()
上抛出一个错误,错误是:
Insufficient system resources exist to complete the requested service
此代码在我的开发环境中有效,但一旦我发布到我们的生产环境中,我们就会收到此错误消息。
我已经看到这个错误在搜索中出现了几次,解决方法是使用 File.Move()
但我们不能这样做,因为文件需要传递给 WCF 服务方法.
IIS6 中是否需要更改某些内容以允许在读取文件时在内存中保留 15-20mb?或者还有其他需要配置的东西吗?
有什么想法吗?
最佳答案
看这个:
Why I need to read file piece by piece to buffer?
看起来你正在读取整个文件,没有缓冲..
buffer = new byte[length];
最好的问候。
关于c# - 系统资源不足,无法完成请求的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10513722/