Unable to read data from the transport connection: The connection was closed

This is my second network error in 2 weeks. When trying to retrieve an image using a WebRequest I get the following error:

Exception: System.IO.IOException Message: Unable to read data from the transport connection: The connection was closed.
Source: System
at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size)

The code has been working fine for years, but suddently it fails. After some Googling I found some hints to why this might happen:

The trick that helped me was to quit using a WebRequest and use a HttpWebRequest instead. The HttpWebRequest allows me to play with 3 important settings:

The code that worked for me is:

private Byte[] RetrieveAsset(Uri uri, out string contentType)
    Byte[] bytes;
    HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create(uri);
    webRequest.KeepAlive = true;
    webRequest.ProtocolVersion = HttpVersion.Version10;
    webRequest.ServicePoint.ConnectionLimit = 24;
    webRequest.Headers.Add("UserAgent", "Pentia; MSI");
    using (WebResponse webResponse = webRequest.GetResponse())
      contentType = webResponse.ContentType;
      using (Stream stream = webResponse.GetResponseStream())
        using (MemoryStream memoryStream = new MemoryStream())
          // Stream the response to a MemoryStream via the byte array buffer
          Byte[] buffer = new Byte[0x1000];
          Int32 bytesRead;
          while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
            memoryStream.Write(buffer, 0, bytesRead);
          bytes = memoryStream.ToArray();
    return bytes;
  catch (Exception ex)
    throw new Exception("Failed to retrieve asset from '" + uri + "': " + ex.Message, ex);

In this case all I needed was to set the ProtocolVersion to 1.0, but other scenarios migth require you to disable KeepAlive or limiting the number of connections.

Therefore you should set the KeepAlive, ProtocolVersion and ConnectionLimit using variable settings as different scenarios require different values (see An existing connection was forcibly closed by the remote host).

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in c# and tagged , , , , , , . Bookmark the permalink.

3 Responses to Unable to read data from the transport connection: The connection was closed

  1. Why is KeepAlive set to true in your example? Based on the explanation, I would expect it to be either commented out or set to false…

  2. briancaos says:

    To be honst I have no idea why the KeepAlive must be true, but if it’s false, the scenario fails.
    I guess that’s just the magic of being a software engineer…

  3. Pingback: C# HttpClient File Download Error – Unable to read data from the transport connection

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s