0

I have a request that calls a post method. It is posting XML in the request content (but sending it as raw text). In testing, the length of the xml is 106880 characters.

In the wep api post method, I process the request body to pull out the XML and store each element/value in a dictionary using the following:

var stream = new System.IO.StreamReader(Request.Body);
XmlReaderSettings settings = new XmlReaderSettings() { Async = true };
using (XmlReader r = XmlReader.Create(stream, settings))
{
    bool rowsExist = true;
    while (rowsExist && await r.ReadAsync())
    {
        if (nodeType == r.NodeType)
        {
            var name = r.Name;

            rowsExist = await r.ReadAsync();
            if (r.NodeType == XmlNodeType.Text)
            {
                xmlDic[name] = r.Value;
            }                                    
        }
    }
}

This works fine with small XML, however when the text value is relatively large, when calling the second ReadAsync method, the data is truncated and the XmlReader throws an exception saying

"Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead."

The exception makes no sense because ReadAsync is being called, but appears to be related to the size of the data, as it wasn't doing it with a smaller set of XML.

I tested a workaround which is to read the entire request body into a string, and then run the XmlReader using the entire body. However, that does use up more memory as it is loading the entire request into memory first, something that shouldn't be necessary.

I wondered if there might be a default max size/limit that stream or XmlReader uses, and see that the XmlReader settings class has 2 properties that control the Max characters:

settings.MaxCharactersFromEntities
settings.MaxCharactersInDocument

However the first has a default set to 10000000, which is way more than I am posting, and the second is set to zero, which means no limit. As a reault, these don't appear to make any difference.

What could be causing this to fail when reading the body using a StreamReader?

Jason
  • 617
  • 9
  • 22

0 Answers0