NullReference exception when sending XMLWrite output to httpContext.Response.OutputStream

I have an application where I get a strange error from time to time. This is a piece of code:

Dim XMLWriter As New System.Xml.XmlTextWriter(Me.Context.Response.OutputStream, Encoding.UTF8)
XMLWriter.WriteStartDocument()
XMLWriter.WriteStartElement("Status")

Message.SerializeToXML(XMLWriter)

XMLWriter.WriteEndElement()
XMLWriter.WriteEndDocument()
XMLWriter.Flush()
XMLWriter.Close()

      

The error I receive is: Message: Object reference not set to object instance.

on line XMLWriter.Flush ();

To make things more fun, this is completely unreproducible. It happens from time to time ...

Since this happens when clearing the XML, I am assuming that the object that is now null should be a Response.OutputStream.

This is an important part of the stack trace:

Description:
An unhandled exception occurred and the process was terminated.

Exception: System.NullReferenceException

Message: Object reference not set to an instance of an object.

StackTrace:    at System.Web.HttpWriter.BufferData(Byte[] data, Int32 offset, Int32 size, Boolean needToCopyData)
   at System.Web.HttpWriter.WriteFromStream(Byte[] data, Int32 offset, Int32 size)
   at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
   at System.Xml.XmlTextWriter.Flush()
   at RequestData.CompleteRequest(MessageBase Message) in RequestData.vb:line 142

      

My question is, in what cases can this happen? This server is a long polling server, so the client asks for something and I cannot respond within 30 seconds ... Is it possible that this stream will become Null if the client disconnects (i.e. closes the browser window)?

Any other ideas? (any pointers are appreciated)

+1


source to share


3 answers


The reflector gives the following:

private void BufferData(byte[] data, int offset, int size, bool needToCopyData)
{
    int num;
    if (this._lastBuffer != null)
    {
        num = this._lastBuffer.Append(data, offset, size);
        size -= num;
        offset += num;
    }
    else if ((!needToCopyData && (offset == 0)) && !this._responseBufferingOn)
    {
        this._buffers.Add(new HttpResponseBufferElement(data, size));
        return;
    }
    while (size > 0)
    {
        this._lastBuffer = this.CreateNewMemoryBufferElement();
        this._buffers.Add(this._lastBuffer);
        num = this._lastBuffer.Append(data, offset, size);
        offset += num;
        size -= num;
    }
}

      



The only object that is not null checked, initialized, or referenced by another method (which will show up in the stack trace) is this._buffers. The only place where null is set in this class is in RecycleBufferElements (), which, if you dig deeper, can occur when the client disconnects.

+1


source


No, if this happens when you call Flush, that path Context.Response.OutputStream

is actually referenced later than the only time . The value is called when the constructor is called XmlTextWriter

and then not looked up again.



Do you have more information from the stack trace?

0


source


Calling Flush is what will cause everything cached in memory to be written to the stream and ultimately the client, yes it could be a problem.

You mentioned that the request is expected to take a long time, so it might be possible that ASP.Net or IIS crashes too early. I would like to suggest looking at the executeTimeout property in the web.config file and similar settings.

0


source







All Articles