StreamReader.ReadToEnd() throws an exception

I have a problem that StreamReader.ReadToEnd() sometimes throws this exception

Exception System.ArgumentOutOfRangeException - CLR_E_OUT_OF_RANGE (1)

My stream is the response of a httpwebrequest.
After a lot of test and frustration I found out that the problem occur if the length of the response from System.Net.HttpWebResponse.request.GetResponse() has the length 512 or 1024
I have created a little test program, that shows the problem.

using GHI.Pins;
using GHI.Networking;
using System.Collections;

namespace EnOcean
{
    public class Application
    {
        public static void Main()
        {
            EthernetENC28J60 enc;
            enc = new GHI.Networking.EthernetENC28J60(SPI.SPI_module.SPI2, G120.P1_17, G120.P0_5, G120.P1_14);
            enc.Open();
            enc.EnableDynamicDns();
            enc.EnableDhcp();
            Thread.Sleep(3000);

            // Create an HTTP Web request.
            System.Net.HttpWebRequest request = System.Net.HttpWebRequest.Create("http://softcontroltest.azurewebsites.net/Change/GetJsonObjXX?Length=512") as System.Net.HttpWebRequest;

            // Set request.KeepAlive to use a persistent connection. 
            request.KeepAlive = false;
            request.ContentType = "application/json";
            request.Timeout = 10000;

            try
            {
                using (var response = (System.Net.HttpWebResponse)request.GetResponse())
                {
                    // Error
                    if (response.StatusCode != System.Net.HttpStatusCode.OK)
                    {
                        Debug.Print("Resonse not OK");
                    }

                    // Grab the response
                    else
                    {
                        Debug.Print("Content length is " + response.ContentLength);
                        Debug.Print("Content type is " + response.ContentType);

                        using (var responseStream = response.GetResponseStream())
                        {
                            if (responseStream != null)
                            {
                                using (var reader = new StreamReader(responseStream))
                                {
                                    var responseValue = reader.ReadToEnd();
                                    Debug.Print("String: " + responseValue);
                                }
                            }
                        }
                    }
                }
            }

            catch (Exception ex)
            {
            }
        }
    }
}

In the url the Length specifies how many bytes to request.
If Length=511 everything is ok
If Length=513 everything is ok
If Length=512 it throws an exception

I have run into this as well. In my case, it was while using .Peek() as part of a parser. On buffer boundaries, the framework code does not manage it’s internal buffers and pointers correctly. There is currently no fix for this bug, but reading blocks of data in a loop may be a viable workaround for you.

EDIT: Added to the NETMF issues as : [url]https://github.com/NETMF/netmf-interpreter/issues/505[/url], but we’ll need to try the repro in 4.4 before it will get attention. It forced me to do awkward things in my json/bson serializer, so if I can find it in the code, I will try to submit a 4.4 pull request.

:’(
I have made a work around and read each byte if len%512==0 && len > 0
This seem to work, but I am not sure about the len%512 will that cover all faults?
I could also just always read each byte, but this way is 10 times slower.
I will also try to put the bye algorith, in the catch() reached by ReadToEnd()

using (var responseStream = response.GetResponseStream())
{
    if (responseStream != null)
    {
        long len = response.ContentLength;
        if (len % 512 == 0 && len > 0)
        {
            byte[] b = new byte[len];
            for (int i = 0; i < len; i++)
            {
                b[i] = (byte)responseStream.ReadByte();
                responseValue = new string(Encoding.UTF8.GetChars(b));
            }
        }
        else
        {
            using (var reader = new StreamReader(responseStream))
            {
                responseValue = reader.ReadToEnd();
                if (responseValue == null)
                    responseValue = string.Empty;
                ServerFaultCnt = 0;
            }
        }
    }
}

Rather than read character by character, you might try reading in blocks of 512. That should work and bypass this bug. We’re just trying to avoid the error case, and reading character-at-a-time should not be necessary.