-
Notifications
You must be signed in to change notification settings - Fork 4
/
2004-04-27-xpathdocument-stream-closing.html
159 lines (157 loc) · 11.3 KB
/
2004-04-27-xpathdocument-stream-closing.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
---
layout: post
title: XPathDocument Stream closing
tags: [xml]
---
<div class="Section1">
<p>
Today I encountered a really annoying documentation bug in the xml side of .net. Whenever
you create an XPathDocument using the Stream based constructor, whenever the construction
returns, your stream is closed.
</p>
<div style='border:solid windowtext 1.0pt;padding:1.0pt 4.0pt 1.0pt 4.0pt; background:#FFFF99'>
<p class="MsoNormal" style='background:#FFFF99;text-autospace:none;border:none; padding:0cm'>
<span style='font-size:10.0pt;font-family:"Courier New"'>                 
documentStream = <span style='color:blue'>new</span> MemoryStream();</span>
</p>
<p class="MsoNormal" style='background:#FFFF99;text-autospace:none;border:none; padding:0cm'>
<span style='font-size:10.0pt;font-family:"Courier New"'>                 
XslFoEngine.CopyStream(message.BodyStream,documentStream);</span>
</p>
<p class="MsoNormal" style='background:#FFFF99;text-autospace:none;border:none; padding:0cm'>
<span style='font-size:10.0pt;font-family:"Courier New"'>                 
documentStream.Position = 0;</span>
</p>
<p style='background:#FFFF99;border:none;padding:0cm'>
<span style='font-size: 10.0pt;font-family:"Courier New"'>                 </span> <span lang="FR" style='font-size:10.0pt;font-family:"Courier New"'>XPathDocument
document = <span style='color:blue'>new</span> XPathDocument(documentStream);</span><span lang="FR"> </span>
</p>
</div>
<p>
You wouldn’t expect the Stream to be closed, but it is. What is happening behind
the scene is that the following constructors wrap whatever you throw at them with
an XmlTextReader object, call the Init(XmlReader) method, which itself close the reader,
in turn closing the stream.
</p>
<div style='border:solid windowtext 1.0pt;padding:1.0pt 4.0pt 1.0pt 4.0pt; background:#CCFFCC'>
<p class="MsoNormal" style='background:#CCFFCC;border:none;padding:0cm'>
<span style='font-size:10.0pt'>Initializes a new instance of the <b>XPathDocument</b> class.</span>
</p>
<p class="MsoNormal" style='background:#CCFFCC;border:none;padding:0cm'>
<span style='font-size:10.0pt;display:none'>[Visual Basic] <a href="ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemxmlxpathxpathdocumentclassctortopic1.htm">Public
Sub New(Stream)</a></span>
</p>
<p class="MsoNormal" style='background:#CCFFCC;border:none;padding:0cm'>
<span style='font-size:10.0pt;display:none'>[C#]</span><span style='font-size:10.0pt'><a href="ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemxmlxpathxpathdocumentclassctortopic1.htm">public
XPathDocument(Stream);</a></span>
</p>
<p class="MsoNormal" style='background:#CCFFCC;border:none;padding:0cm'>
<span style='font-size:10.0pt;display:none'>[C++] <a href="ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemxmlxpathxpathdocumentclassctortopic1.htm">public:
XPathDocument(Stream*);</a></span>
</p>
<p class="MsoNormal" style='background:#CCFFCC;border:none;padding:0cm'>
<span style='font-size:10.0pt;display:none'>[JScript] <a href="ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemxmlxpathxpathdocumentclassctortopic1.htm">public
function XPathDocument(Stream);</a></span>
</p>
<p class="MsoNormal" style='background:#CCFFCC;border:none;padding:0cm'>
<span style='font-size:10.0pt'>Initializes a new instance of the <b>XPathDocument</b> class.</span>
</p>
<p class="MsoNormal" style='background:#CCFFCC;border:none;padding:0cm'>
<span style='font-size:10.0pt;display:none'>[Visual Basic] <a href="ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemxmlxpathxpathdocumentclassctortopic2.htm">Public
Sub New(String)</a></span>
</p>
<p class="MsoNormal" style='background:#CCFFCC;border:none;padding:0cm'>
<span style='font-size:10.0pt;display:none'>[C#]</span><span style='font-size:10.0pt'><a href="ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemxmlxpathxpathdocumentclassctortopic2.htm">public
XPathDocument(string);</a></span>
</p>
<p class="MsoNormal" style='background:#CCFFCC;border:none;padding:0cm'>
<span style='font-size:10.0pt;display:none'>[C++] <a href="ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemxmlxpathxpathdocumentclassctortopic2.htm">public:
XPathDocument(String*);</a></span>
</p>
<p class="MsoNormal" style='background:#CCFFCC;border:none;padding:0cm'>
<span style='font-size:10.0pt;display:none'>[JScript] <a href="ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemxmlxpathxpathdocumentclassctortopic2.htm">public
function XPathDocument(String);</a></span>
</p>
<p class="MsoNormal" style='background:#CCFFCC;border:none;padding:0cm'>
<span style='font-size:10.0pt'>Initializes a new instance of the <b>XPathDocument</b> class.</span>
</p>
<p class="MsoNormal" style='background:#CCFFCC;border:none;padding:0cm'>
<span style='font-size:10.0pt;display:none'>[Visual Basic] <a href="ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemxmlxpathxpathdocumentclassctortopic3.htm">Public
Sub New(TextReader)</a></span>
</p>
<p class="MsoNormal" style='background:#CCFFCC;border:none;padding:0cm'>
<span style='font-size:10.0pt;display:none'>[C#]</span><span style='font-size:10.0pt'><a href="ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemxmlxpathxpathdocumentclassctortopic3.htm">public
XPathDocument(TextReader);</a></span>
</p>
<p class="MsoNormal" style='background:#CCFFCC;border:none;padding:0cm'>
<span style='font-size:10.0pt;display:none'>[C++] <a href="ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemxmlxpathxpathdocumentclassctortopic3.htm">public:
XPathDocument(TextReader*);</a></span>
</p>
<p class="MsoNormal" style='background:#CCFFCC;border:none;padding:0cm'>
<span style='font-size:10.0pt;display:none'>[JScript] <a href="ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemxmlxpathxpathdocumentclassctortopic3.htm">public
function XPathDocument(TextReader);</a></span>
</p>
<p class="MsoNormal" style='background:#CCFFCC;border:none;padding:0cm'>
<span style='font-size:10.0pt'>Initializes a new instance of the <b>XPathDocument</b> class.</span>
</p>
<p class="MsoNormal" style='background:#CCFFCC;border:none;padding:0cm'>
<span style='font-size:10.0pt;display:none'>[Visual Basic] <a href="ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemxmlxpathxpathdocumentclassctortopic4.htm">Public
Sub New(XmlReader)</a></span>
</p>
<p class="MsoNormal" style='background:#CCFFCC;border:none;padding:0cm'>
<span style='font-size:10.0pt;display:none'>[C#]</span><span style='font-size:10.0pt'><a href="ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemxmlxpathxpathdocumentclassctortopic4.htm">public
XPathDocument(XmlReader);</a></span>
</p>
<p class="MsoNormal" style='background:#CCFFCC;border:none;padding:0cm'>
<span style='font-size:10.0pt;display:none'>[C++] <a href="ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemxmlxpathxpathdocumentclassctortopic4.htm">public:
XPathDocument(XmlReader*);</a></span>
</p>
<p class="MsoNormal" style='background:#CCFFCC;border:none;padding:0cm'>
<span style='font-size:10.0pt;display:none'>[JScript] <a href="ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemxmlxpathxpathdocumentclassctortopic4.htm">public
function XPathDocument(XmlReader);</a></span>
</p>
<p class="MsoNormal" style='background:#CCFFCC;border:none;padding:0cm'>
<span style='font-size:10.0pt'>Initializes a new instance of the <b>XPathDocument</b> class.</span>
</p>
<p class="MsoNormal" style='background:#CCFFCC;border:none;padding:0cm'>
<span style='font-size:10.0pt;display:none'>[Visual Basic] <a href="ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemxmlxpathxpathdocumentclassctortopic5.htm">Public
Sub New(String, XmlSpace)</a></span>
</p>
<p class="MsoNormal" style='background:#CCFFCC;border:none;padding:0cm'>
<span style='font-size:10.0pt;display:none'>[C#]</span><span style='font-size:10.0pt'><a href="ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemxmlxpathxpathdocumentclassctortopic5.htm">public
XPathDocument(string, XmlSpace);</a></span>
</p>
</div>
<p>
Only way to go around the problem, create the reader yourself, but don’t close
it.
</p>
<div style='border:solid windowtext 1.0pt;padding:1.0pt 4.0pt 1.0pt 4.0pt; background:#FFFF99'>
<p class="MsoNormal" style='background:#FFFF99;text-autospace:none;border:none; padding:0cm'>
<span style='font-size:10.0pt;font-family:"Courier New"'>                 
documentStream = <span style='color:blue'>new</span> MemoryStream();</span>
</p>
<p class="MsoNormal" style='background:#FFFF99;text-autospace:none;border:none; padding:0cm'>
<span style='font-size:10.0pt;font-family:"Courier New"'>                 
XslFoEngine.CopyStream(message.BodyStream,documentStream);</span>
</p>
<p class="MsoNormal" style='background:#FFFF99;text-autospace:none;border:none; padding:0cm'>
<span style='font-size:10.0pt;font-family:"Courier New"'>                 
documentStream.Position = 0;</span>
</p>
<p style='background:#FFFF99;border:none;padding:0cm'>
<span style='font-size: 10.0pt;font-family:"Courier New"'>                 
XPathDocument document = <span style='color:blue'>new</span> XPathDocument(<span style='color:blue'>new</span> XmlTextReader(documentStream));</span>
</p>
</div>
<p>
<i>PS: I will respond to IanG comments on my ReaderWriterLock entry with some statistics
later this week on when / how to use this pattern (high contention), as I fully agree
with him that you shouldn’t just <b>assume</b> without testing. In this specific
case, the contention is really high on a very high load long living threads environment.
More on that later.</i>
</p>
<p>
 
</p>
</div>