将 XMLWrite 输出发送到 httpContext.Response.OutputStream 时出现 NullReferenceException
我有一个应用程序,时不时地会收到一个奇怪的错误。 这是一段代码:
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()
我得到的错误是: 消息:未将对象引用设置为对象的实例。
在线 XMLWriter.Flush();
为了让事情变得更有趣,这是绝对不可复制的。 它只是时不时地发生......
因为它是在刷新 XML 时发生的,我猜测现在为 null 的对象必须是 Response.OutputStream。
这是堆栈跟踪的相关部分:
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
我的问题是,在什么情况下会发生这种情况? 这个服务器是一个长轮询服务器,所以客户端询问一些东西,我可能30秒都不会回答...... 如果客户端断开连接(即关闭浏览器窗口),该 Stream 是否有可能变为 Null?
还有其他想法吗? (任何指示赞赏)
I have an application where every now and then I'm getting a strange error.
This is the 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'm getting is:
Message: Object reference not set to an instance of an object.
on line XMLWriter.Flush();
To make things more fun, this is absolutely non-reproducible. It just happens every now and then....
Since it's happening when flushing the XML i'm guessing the Object that is now null has to be the Response.OutputStream.
This is the relevant 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 could this be happening?
This server is a long-polling server, so the client asks for something, and I may not answer for 30 seconds...
Is it possible that this Stream will become Null if the client disconnects (ie. closes the browser window)?
Any other ideas? (any pointers appreciated)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Reflector给出了这个:
唯一没有通过另一个方法(将显示在堆栈跟踪中)进行空检查、初始化或引用的对象是this._buffers。 在该类中唯一将其设置为 null 的地方是在 RecycleBufferElements() 中,如果您深入研究,当客户端断开连接时可能会发生这种情况。
Reflector gives this:
The only object that is not null checked, initialized or referenced through another method(which would show in the stack trace) is this._buffers. The only place it is set to null in that class is in RecycleBufferElements() which if you dig deeper can occur when the client disconnects.
对 Flush 的调用会导致内存中缓存的任何内容被写入流并最终写入客户端,所以是的,这可能是问题所在。
您提到该请求预计需要很长时间才能执行,因此 ASP.Net 或 IIS 可能会过早地超时。 我建议查看 web.config 中的 executionTimeout 属性 和类似的设置。
The call to Flush is what will cause anything cached in memory to be written out to the stream and ultimately the client so yes, it may be the problem.
You mentioned that the request is expected to take a long time to execute so it may be possible that ASP.Net or IIS are timing you out too early. I would suggest having a look at the executionTimeout property in the web.config and similar settings.
不,如果在调用 Flush 时发生这种情况,则比实际引用 Context.Response.OutputStream 的唯一时间晚得多。 该值是在调用
XmlTextWriter
构造函数时获取的,然后不再查看。您从堆栈跟踪中获得更多信息吗?
No, if it happens when you call Flush, that's way later than the only time
Context.Response.OutputStream
is actually referenced. The value is fetched in the call to theXmlTextWriter
constructor, and then not looked at again.Do you have any more information from the stack trace?