"An existing connection was forcibly closed by the remote host" IIS 中的错误,但不是 VS 开发服务器



通过我的应用程序,我使用WebClient在线访问文件并处理它们。下面就是这样一个例子:

using (var webClient = new WebClient())
{
    webClient.Credentials = new NetworkCredential(userName, password, domain);
    var bytes = webClient.DownloadData(urlToWorkbook);
    Stream stream = new MemoryStream(bytes);
    var workbook = new Workbook(stream);
    stream.Close();
    stream.Dispose();
    return workbook;
}

当我开发应用程序并通过Visual Studio的调试器进行测试时,一切都运行良好。现在我已经部署了应用程序,它正在通过IIS运行,我在webClient.DownloadData()调用上得到以下错误:

System.Net.WebException: An exception occurred during a WebClient request. 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.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)

编辑:另一个奇怪的事情是,当我把这个代码在我的控制器的索引动作(这是一个MVC)应用程序,它执行DownloadData()调用成功的一切。只有当调用被嵌入到我的应用程序(需要它的地方)时,它才会失败。

我将我的站点本地发布到我的机器的IIS,我得到相同的异常。我觉得我已经尝试了无数的代码解决方案。这似乎是VS的web服务器和IIS之间的区别。知道我能在IIS中做什么吗?

原来这与文件流打开时间过长有关。我的应用程序从Sharepoint存储中检索文件并循环遍历,将文件下载到字节数组中。它似乎不喜欢通过IIS完成的方式,所以我进行了一些重构,以保持流在尽可能短的时间内打开。

相关内容

最新更新