Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a HttpHandler that returns a large chunk (~400k lines) of XML. For some reason the data is being corrupted on its way out. Here's a simple test case:

public class SimpleTestCase : HttpHandlerBase
{
    public override bool IsReusable
    {
        get { return false; }
    }

    protected override void HandleRequest(HttpContext context)
    {
        context.Response.ContentType = "application/xml";

        context.Response.Write("<?xml version=\"1.0\"?>" + Environment.NewLine);

        for(var i = 0; i < 400010; i++)
        {
            context.Response.Write("<amount>5</amount>" + Environment.NewLine);
        }
    }
}

When I look at the result I see about 60 of the 400,010 instances where this is written:

  <amount>5amount>

Notice that the opening portion of the closing tag (</) is missing. Why is the Response stream being corrupted? And more importantly, how do I prevent it?


Update: In case it is relevant, Visual Studio 2013, IIS Express, .NET 4.5

share|improve this question
    
mysterious. Looks like a bug in the framework. Try flushing the response every X lines (say 500 or 1000) ... Response.Flush if I remember correctly –  MatthewMartin Jun 4 at 14:22
    
@MatthewMartin - thanks, but no dice. Flushed every 200 writes, but still get this strange behavior –  Philip Pittle Jun 4 at 14:33
    
Did this bug exist earlier? (and is now gone?) Browser could be caching the old wrong version of the file. –  MatthewMartin Jun 4 at 14:37
    
Good question, but sadly no. Changed the string output to <amount>6</amount> to ensure browser wasn't caching. –  Philip Pittle Jun 4 at 14:38
1  
Interesting... I repro'd the bug on webdev, vs2010rtm, .net 4.0. I would guess TextWriter or a class that it depends on has a bug for large Streams. I took a look at the reference source code-- it does a lot of arithmetic behind the scenes in Write() –  MatthewMartin Jun 4 at 17:35

1 Answer 1

up vote 2 down vote accepted

I have replicated your code, and it seems to work fine. The issue you are seeing seems to be browser related. With your code, I get the full response in Fiddler, Firefox and Chrome, but Internet Explorer does not display the correct in response in Normal, neither in Source view. However, if you look in the network inspection tools in Internet Explorer you can see that the entire response is received.

By encapsulating the xml content in a root tag, thereby making it a valid xml document, Internet Explorer now displays the result correctly.

public class SimpleTestCase : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/xml";

        context.Response.Write("<?xml version=\"1.0\"?>" + Environment.NewLine);

        context.Response.Write("<root>");
        for (var i = 0; i < 400010; i++)
        {
            context.Response.Write("<amount>5</amount>" + Environment.NewLine);
        }
        context.Response.Write("</root>");

        context.Response.Flush();
        context.Response.End();
    }

    public bool IsReusable
    {
        get { return false; }
    }
}
share|improve this answer
    
Confirmed, this does seem to be a browser issue. I can see the correct bits going across the wire on Fiddler. Odd that the browsers aren't showing the data correctly. –  Philip Pittle Jun 6 at 8:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.