3
votes

Through my application, I'm using WebClient to access files online and processing them. Here is one such example:

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;
}

When I was developing the application and testing through Visual Studio's debugger, everything worked fine. Now that I've deployed the application and it's running through IIS, I get the following error on the webClient.DownloadData() call:

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)

EDIT: Another strange thing is that when I put this code on my controller's Index action (this is an MVC) app, it executes the DownloadData() call successfully everything. It is only when the call is embedded in my application (where it's needed) that it fails.

I published my site locally to my machine's IIS and I get the same exception. I feel like I've tried a million code solutions. It would seem it's a difference between VS's web server and IIS. Any idea of what I could do in IIS?

1
I remember I used to get this exception while trying to connect to Oracle database after certain period of idle time. Is there any database connection involved in your WebClient communication process?Dennis R
There is no database in my application at all.im1dermike
Just a suggestion, you could try sniffing the data between your webserver and the remote machine to see if the request looks correct (if that's possible). It sounds to me as if the remote host refuses the connection for any reason (which could possibly be seen in the network packets).Markus Safar
@MarkusSafar: See my edit.im1dermike
Hm... what do you mean "embedded in my application", do you mean that you call some action and somwhere else (in an instance of another instance or so) there is your code?Markus Safar

1 Answers

1
votes

It turns out it had to do with leaving file streams open too long. My app retrieves files from a Sharepoint store and loops through them, downloading the files into byte arrays. It didn't seem to like the way it was being done when going through IIS so I refactored a bit to keep the stream open for the shortest possible time. Seems to work now.