/
MtomMessageEncodingBindingElement.cs
230 lines (203 loc) · 8.11 KB
/
MtomMessageEncodingBindingElement.cs
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.ComponentModel;
using System.Text;
using System.Xml;
namespace System.ServiceModel.Channels
{
public sealed class MtomMessageEncodingBindingElement : MessageEncodingBindingElement
{
private int _maxReadPoolSize;
private int _maxWritePoolSize;
private XmlDictionaryReaderQuotas _readerQuotas;
private int _maxBufferSize;
private Encoding _writeEncoding;
private MessageVersion _messageVersion;
public MtomMessageEncodingBindingElement() : this(MessageVersion.Default, TextEncoderDefaults.Encoding) { }
public MtomMessageEncodingBindingElement(MessageVersion messageVersion, Encoding writeEncoding)
{
if (messageVersion == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(messageVersion));
if (messageVersion == MessageVersion.None)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SRP.Format(SRP.MtomEncoderBadMessageVersion, messageVersion.ToString()), nameof(messageVersion)));
if (writeEncoding == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writeEncoding));
TextEncoderDefaults.ValidateEncoding(writeEncoding);
_maxReadPoolSize = EncoderDefaults.MaxReadPoolSize;
_maxWritePoolSize = EncoderDefaults.MaxWritePoolSize;
_readerQuotas = new XmlDictionaryReaderQuotas();
EncoderDefaults.ReaderQuotas.CopyTo(_readerQuotas);
_maxBufferSize = MtomEncoderDefaults.MaxBufferSize;
_messageVersion = messageVersion;
_writeEncoding = writeEncoding;
}
private MtomMessageEncodingBindingElement(MtomMessageEncodingBindingElement elementToBeCloned)
: base(elementToBeCloned)
{
_maxReadPoolSize = elementToBeCloned._maxReadPoolSize;
_maxWritePoolSize = elementToBeCloned._maxWritePoolSize;
_readerQuotas = new XmlDictionaryReaderQuotas();
elementToBeCloned._readerQuotas.CopyTo(_readerQuotas);
_maxBufferSize = elementToBeCloned._maxBufferSize;
_writeEncoding = elementToBeCloned._writeEncoding;
_messageVersion = elementToBeCloned._messageVersion;
}
[DefaultValue(EncoderDefaults.MaxReadPoolSize)]
public int MaxReadPoolSize
{
get
{
return _maxReadPoolSize;
}
set
{
if (value <= 0)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(value), value,
SRP.ValueMustBePositive));
}
_maxReadPoolSize = value;
}
}
[DefaultValue(EncoderDefaults.MaxWritePoolSize)]
public int MaxWritePoolSize
{
get
{
return _maxWritePoolSize;
}
set
{
if (value <= 0)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(value), value,
SRP.ValueMustBePositive));
}
_maxWritePoolSize = value;
}
}
public XmlDictionaryReaderQuotas ReaderQuotas
{
get
{
return _readerQuotas;
}
}
[DefaultValue(MtomEncoderDefaults.MaxBufferSize)]
public int MaxBufferSize
{
get
{
return _maxBufferSize;
}
set
{
if (value <= 0)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(value), value,
SRP.ValueMustBePositive));
}
_maxBufferSize = value;
}
}
public Encoding WriteEncoding
{
get
{
return _writeEncoding;
}
set
{
if (value == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value));
TextEncoderDefaults.ValidateEncoding(value);
_writeEncoding = value;
}
}
public override MessageVersion MessageVersion
{
get
{
return _messageVersion;
}
set
{
if (value == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value));
}
if (value == MessageVersion.None)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SRP.Format(SRP.MtomEncoderBadMessageVersion, value.ToString()), nameof(value)));
}
_messageVersion = value;
}
}
public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
{
return InternalBuildChannelFactory<TChannel>(context);
}
public override bool CanBuildChannelFactory<TChannel>(BindingContext context)
{
return InternalCanBuildChannelFactory<TChannel>(context);
}
public override BindingElement Clone()
{
return new MtomMessageEncodingBindingElement(this);
}
public override MessageEncoderFactory CreateMessageEncoderFactory()
{
return new MtomMessageEncoderFactory(MessageVersion, WriteEncoding, MaxReadPoolSize, MaxWritePoolSize, MaxBufferSize, ReaderQuotas);
}
public override T GetProperty<T>(BindingContext context)
{
if (context == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(context));
}
if (typeof(T) == typeof(XmlDictionaryReaderQuotas))
{
return (T)(object)_readerQuotas;
}
else
{
return base.GetProperty<T>(context);
}
}
internal override bool CheckEncodingVersion(EnvelopeVersion version)
{
return _messageVersion.Envelope == version;
}
internal override bool IsMatch(BindingElement b)
{
if (!base.IsMatch(b))
return false;
MtomMessageEncodingBindingElement mtom = b as MtomMessageEncodingBindingElement;
if (mtom == null)
return false;
if (_maxReadPoolSize != mtom.MaxReadPoolSize)
return false;
if (_maxWritePoolSize != mtom.MaxWritePoolSize)
return false;
// compare XmlDictionaryReaderQuotas
if (_readerQuotas.MaxStringContentLength != mtom.ReaderQuotas.MaxStringContentLength)
return false;
if (_readerQuotas.MaxArrayLength != mtom.ReaderQuotas.MaxArrayLength)
return false;
if (_readerQuotas.MaxBytesPerRead != mtom.ReaderQuotas.MaxBytesPerRead)
return false;
if (_readerQuotas.MaxDepth != mtom.ReaderQuotas.MaxDepth)
return false;
if (_readerQuotas.MaxNameTableCharCount != mtom.ReaderQuotas.MaxNameTableCharCount)
return false;
if (_maxBufferSize != mtom.MaxBufferSize)
return false;
if (WriteEncoding.EncodingName != mtom.WriteEncoding.EncodingName)
return false;
if (!MessageVersion.IsMatch(mtom.MessageVersion))
return false;
return true;
}
}
}