/
EndpointAddress.cs
1319 lines (1153 loc) · 48.7 KB
/
EndpointAddress.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime;
using System.ServiceModel.Channels;
using System.Xml;
namespace System.ServiceModel
{
public class EndpointAddress
{
private static Uri s_anonymousUri;
private static Uri s_noneUri;
private static EndpointAddress s_anonymousAddress;
/*
Conceptually, the agnostic EndpointAddress class represents all of UNION(v200408,v10) data thusly:
- Address Uri (both versions - the Address)
- AddressHeaderCollection (both versions - RefProp&RefParam both project into here)
- PSP blob (200408 - this is PortType, ServiceName, Policy, it is not surfaced in OM)
- metadata (both versions, but weird semantics in 200408)
- identity (both versions, this is the one 'extension' that we know about)
- extensions (both versions, the "any*" stuff at the end)
When reading from 200408:
- Address is projected into Uri
- both RefProps and RefParams are projected into AddressHeaderCollection,
they (internally) remember 'which kind' they are
- PortType, ServiceName, Policy are projected into the (internal) PSP blob
- if we see a wsx:metadata element next, we project that element and that element only into the metadata reader
- we read the rest, recognizing and fishing out identity if there, projecting rest to extensions reader
When reading from 10:
- Address is projected into Uri
- RefParams are projected into AddressHeaderCollection; they (internally) remember 'which kind' they are
- nothing is projected into the (internal) PSP blob (it's empty)
- if there's a wsa10:metadata element, everything inside it projects into metadatareader
- we read the rest, recognizing and fishing out identity if there, projecting rest to extensions reader
When writing to 200408:
- Uri is written as Address
- AddressHeaderCollection is written as RefProps & RefParams, based on what they internally remember selves to be
- PSP blob is written out verbatim (will have: PortType?, ServiceName?, Policy?)
- metadata reader is written out verbatim
- identity is written out as extension
- extension reader is written out verbatim
When writing to 10:
- Uri is written as Address
- AddressHeaderCollection is all written as RefParams, regardless of what they internally remember selves to be
- PSP blob is ignored
- if metadata reader is non-empty, we write its value out verbatim inside a wsa10:metadata element
- identity is written out as extension
- extension reader is written out verbatim
EndpointAddressBuilder:
- you can set metadata to any value you like; we don't (cannot) validate because 10 allows anything
- you can set any extensions you like
Known Weirdnesses:
- PSP blob does not surface in OM - it can only roundtrip 200408wire->OM->200408wire
- RefProperty distinction does not surface in OM - it can only roundtrip 200408wire->OM->200408wire
- regardless of what metadata in reader, when you roundtrip OM->200408wire->OM, only wsx:metadata
as first element after PSP will stay in metadata, anything else gets dumped in extensions
- PSP blob is lost when doing OM->10wire->OM
- RefProps turn into RefParams when doing OM->10wire->OM
- Identity is always shuffled to front of extensions when doing anyWire->OM->anyWire
*/
private AddressingVersion _addressingVersion;
private AddressHeaderCollection _headers;
private int _extensionSection;
private int _metadataSection;
private int _pspSection;
private bool _isNone;
// these are the element name/namespace for the dummy wrapper element that wraps each buffer section
internal const string DummyName = "Dummy";
internal const string DummyNamespace = "http://Dummy";
private EndpointAddress(AddressingVersion version, Uri uri, EndpointIdentity identity, AddressHeaderCollection headers, XmlBuffer buffer, int metadataSection, int extensionSection, int pspSection)
{
Init(version, uri, identity, headers, buffer, metadataSection, extensionSection, pspSection);
}
public EndpointAddress(string uri)
{
if (uri == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(uri));
}
Uri u = new Uri(uri);
Init(u, (EndpointIdentity)null, (AddressHeaderCollection)null, null, -1, -1, -1);
}
public EndpointAddress(Uri uri, params AddressHeader[] addressHeaders)
: this(uri, (EndpointIdentity)null, addressHeaders)
{
}
public EndpointAddress(Uri uri, EndpointIdentity identity, params AddressHeader[] addressHeaders)
{
if (uri == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(uri));
}
Init(uri, identity, addressHeaders);
}
internal EndpointAddress(Uri newUri, EndpointAddress oldEndpointAddress)
{
Init(oldEndpointAddress._addressingVersion, newUri, oldEndpointAddress.Identity, oldEndpointAddress._headers, oldEndpointAddress.Buffer, oldEndpointAddress._metadataSection, oldEndpointAddress._extensionSection, oldEndpointAddress._pspSection);
}
internal EndpointAddress(Uri uri, EndpointIdentity identity, AddressHeaderCollection headers, XmlDictionaryReader metadataReader, XmlDictionaryReader extensionReader, XmlDictionaryReader pspReader)
{
if (uri == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(uri));
}
XmlBuffer buffer = null;
PossiblyPopulateBuffer(metadataReader, ref buffer, out _metadataSection);
EndpointIdentity ident2;
int extSection;
buffer = ReadExtensions(extensionReader, null, buffer, out ident2, out extSection);
if (identity != null && ident2 != null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SRP.MultipleIdentities, nameof(extensionReader)));
}
PossiblyPopulateBuffer(pspReader, ref buffer, out _pspSection);
if (buffer != null)
{
buffer.Close();
}
Init(uri, identity ?? ident2, headers, buffer, _metadataSection, extSection, _pspSection);
}
private void Init(Uri uri, EndpointIdentity identity, AddressHeader[] headers)
{
if (headers == null || headers.Length == 0)
{
Init(uri, identity, (AddressHeaderCollection)null, null, -1, -1, -1);
}
else
{
Init(uri, identity, new AddressHeaderCollection(headers), null, -1, -1, -1);
}
}
private void Init(Uri uri, EndpointIdentity identity, AddressHeaderCollection headers, XmlBuffer buffer, int metadataSection, int extensionSection, int pspSection)
{
Init(null, uri, identity, headers, buffer, metadataSection, extensionSection, pspSection);
}
private void Init(AddressingVersion version, Uri uri, EndpointIdentity identity, AddressHeaderCollection headers, XmlBuffer buffer, int metadataSection, int extensionSection, int pspSection)
{
if (!uri.IsAbsoluteUri)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(uri), SRP.UriMustBeAbsolute);
}
_addressingVersion = version;
Uri = uri;
Identity = identity;
_headers = headers;
Buffer = buffer;
_metadataSection = metadataSection;
_extensionSection = extensionSection;
_pspSection = pspSection;
if (version != null)
{
IsAnonymous = uri == version.AnonymousUri;
_isNone = uri == version.NoneUri;
}
else
{
IsAnonymous = object.ReferenceEquals(uri, AnonymousUri) || uri == AnonymousUri;
_isNone = object.ReferenceEquals(uri, NoneUri) || uri == NoneUri;
}
if (IsAnonymous)
{
Uri = AnonymousUri;
}
if (_isNone)
{
Uri = NoneUri;
}
}
internal static EndpointAddress AnonymousAddress
{
get
{
if (s_anonymousAddress == null)
{
s_anonymousAddress = new EndpointAddress(AnonymousUri);
}
return s_anonymousAddress;
}
}
public static Uri AnonymousUri
{
get
{
if (s_anonymousUri == null)
{
s_anonymousUri = new Uri(AddressingStrings.AnonymousUri);
}
return s_anonymousUri;
}
}
public static Uri NoneUri
{
get
{
if (s_noneUri == null)
{
s_noneUri = new Uri(AddressingStrings.NoneUri);
}
return s_noneUri;
}
}
internal XmlBuffer Buffer { get; private set; }
public AddressHeaderCollection Headers
{
get
{
if (_headers == null)
{
_headers = new AddressHeaderCollection();
}
return _headers;
}
}
public EndpointIdentity Identity { get; private set; }
public bool IsAnonymous { get; private set; }
public bool IsNone
{
get
{
return _isNone;
}
}
public Uri Uri { get; private set; }
public void ApplyTo(Message message)
{
if (message == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(message));
}
Uri uri = Uri;
if (IsAnonymous)
{
if (message.Version.Addressing == AddressingVersion.WSAddressing10)
{
message.Headers.To = null;
}
else if (message.Version.Addressing == AddressingVersion.WSAddressingAugust2004)
{
message.Headers.To = message.Version.Addressing.AnonymousUri;
}
else
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new ProtocolException(SRP.Format(SRP.AddressingVersionNotSupported, message.Version.Addressing)));
}
}
else if (IsNone)
{
message.Headers.To = message.Version.Addressing.NoneUri;
}
else
{
message.Headers.To = uri;
}
message.Properties.Via = message.Headers.To;
if (_headers != null)
{
_headers.AddHeadersTo(message);
}
}
// NOTE: UserInfo, Query, and Fragment are ignored when comparing Uris as addresses
// this is the WCF logic for comparing Uris that represent addresses
// this method must be kept in sync with UriGetHashCode
internal static bool UriEquals(Uri u1, Uri u2, bool ignoreCase, bool includeHostInComparison)
{
return UriEquals(u1, u2, ignoreCase, includeHostInComparison, true);
}
internal static bool UriEquals(Uri u1, Uri u2, bool ignoreCase, bool includeHostInComparison, bool includePortInComparison)
{
// PERF: Equals compares everything but UserInfo and Fragments. It's more strict than
// we are, and faster, so it is done first.
if (u1.Equals(u2))
{
return true;
}
if (u1.Scheme != u2.Scheme) // Uri.Scheme is always lowercase
{
return false;
}
if (includePortInComparison)
{
if (u1.Port != u2.Port)
{
return false;
}
}
if (includeHostInComparison)
{
if (string.Compare(u1.Host, u2.Host, StringComparison.OrdinalIgnoreCase) != 0)
{
return false;
}
}
if (string.Compare(u1.AbsolutePath, u2.AbsolutePath, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) == 0)
{
return true;
}
// Normalize for trailing slashes
string u1Path = u1.GetComponents(UriComponents.Path, UriFormat.Unescaped);
string u2Path = u2.GetComponents(UriComponents.Path, UriFormat.Unescaped);
int u1Len = (u1Path.Length > 0 && u1Path[u1Path.Length - 1] == '/') ? u1Path.Length - 1 : u1Path.Length;
int u2Len = (u2Path.Length > 0 && u2Path[u2Path.Length - 1] == '/') ? u2Path.Length - 1 : u2Path.Length;
if (u2Len != u1Len)
{
return false;
}
return string.Compare(u1Path, 0, u2Path, 0, u1Len, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) == 0;
}
// this method must be kept in sync with UriEquals
internal static int UriGetHashCode(Uri uri, bool includeHostInComparison)
{
return UriGetHashCode(uri, includeHostInComparison, true);
}
internal static int UriGetHashCode(Uri uri, bool includeHostInComparison, bool includePortInComparison)
{
UriComponents components = UriComponents.Scheme | UriComponents.Path;
if (includePortInComparison)
{
components = components | UriComponents.Port;
}
if (includeHostInComparison)
{
components = components | UriComponents.Host;
}
// Normalize for trailing slashes
string uriString = uri.GetComponents(components, UriFormat.Unescaped);
if (uriString.Length > 0 && uriString[uriString.Length - 1] != '/')
{
uriString = string.Concat(uriString, "/");
}
return StringComparer.OrdinalIgnoreCase.GetHashCode(uriString);
}
internal bool EndpointEquals(EndpointAddress endpointAddress)
{
if (endpointAddress == null)
{
return false;
}
if (object.ReferenceEquals(this, endpointAddress))
{
return true;
}
Uri thisTo = Uri;
Uri otherTo = endpointAddress.Uri;
if (!UriEquals(thisTo, otherTo, false /* ignoreCase */, true /* includeHostInComparison */))
{
return false;
}
if (Identity == null)
{
if (endpointAddress.Identity != null)
{
return false;
}
}
else if (!Identity.Equals(endpointAddress.Identity))
{
return false;
}
if (!Headers.IsEquivalent(endpointAddress.Headers))
{
return false;
}
return true;
}
public override bool Equals(object obj)
{
if (object.ReferenceEquals(obj, this))
{
return true;
}
if (obj == null)
{
return false;
}
EndpointAddress address = obj as EndpointAddress;
if (address == null)
{
return false;
}
return EndpointEquals(address);
}
public override int GetHashCode()
{
return UriGetHashCode(Uri, true /* includeHostInComparison */);
}
// returns reader without starting dummy wrapper element
internal XmlDictionaryReader GetReaderAtPsp()
{
return GetReaderAtSection(Buffer, _pspSection);
}
// returns reader without starting dummy wrapper element
public XmlDictionaryReader GetReaderAtMetadata()
{
return GetReaderAtSection(Buffer, _metadataSection);
}
// returns reader without starting dummy wrapper element
public XmlDictionaryReader GetReaderAtExtensions()
{
return GetReaderAtSection(Buffer, _extensionSection);
}
private static XmlDictionaryReader GetReaderAtSection(XmlBuffer buffer, int section)
{
if (buffer == null || section < 0)
{
return null;
}
XmlDictionaryReader reader = buffer.GetReader(section);
reader.MoveToContent();
Fx.Assert(reader.Name == DummyName, "EndpointAddress: Expected dummy element not found");
reader.Read(); // consume the dummy wrapper element
return reader;
}
private void PossiblyPopulateBuffer(XmlDictionaryReader reader, ref XmlBuffer buffer, out int section)
{
if (reader == null)
{
section = -1;
}
else
{
if (buffer == null)
{
buffer = new XmlBuffer(short.MaxValue);
}
section = buffer.SectionCount;
XmlDictionaryWriter writer = buffer.OpenSection(reader.Quotas);
writer.WriteStartElement(DummyName, DummyNamespace);
Copy(writer, reader);
buffer.CloseSection();
}
}
public static EndpointAddress ReadFrom(XmlDictionaryReader reader)
{
AddressingVersion dummyVersion;
return ReadFrom(reader, out dummyVersion);
}
internal static EndpointAddress ReadFrom(XmlDictionaryReader reader, out AddressingVersion version)
{
if (reader == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader));
}
reader.ReadFullStartElement();
reader.MoveToContent();
if (reader.IsNamespaceUri(AddressingVersion.WSAddressing10.DictionaryNamespace))
{
version = AddressingVersion.WSAddressing10;
}
else if (reader.IsNamespaceUri(AddressingVersion.WSAddressingAugust2004.DictionaryNamespace))
{
version = AddressingVersion.WSAddressingAugust2004;
}
else if (reader.NodeType != XmlNodeType.Element)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(
nameof(reader), SRP.CannotDetectAddressingVersion);
}
else
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(
nameof(reader), SRP.Format(SRP.AddressingVersionNotSupported, reader.NamespaceURI));
}
EndpointAddress ea = ReadFromDriver(version, reader);
reader.ReadEndElement();
return ea;
}
public static EndpointAddress ReadFrom(AddressingVersion addressingVersion, XmlDictionaryReader reader)
{
if (reader == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader));
}
if (addressingVersion == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(addressingVersion));
}
reader.ReadFullStartElement();
EndpointAddress ea = ReadFromDriver(addressingVersion, reader);
reader.ReadEndElement();
return ea;
}
public static EndpointAddress ReadFrom(AddressingVersion addressingVersion, XmlDictionaryReader reader, XmlDictionaryString localName, XmlDictionaryString ns)
{
if (reader == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader));
if (addressingVersion == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(addressingVersion));
reader.ReadFullStartElement(localName, ns);
EndpointAddress ea = ReadFromDriver(addressingVersion, reader);
reader.ReadEndElement();
return ea;
}
private static EndpointAddress ReadFromDriver(AddressingVersion addressingVersion, XmlDictionaryReader reader)
{
AddressHeaderCollection headers;
EndpointIdentity identity;
Uri uri;
XmlBuffer buffer;
bool isAnonymous;
int extensionSection;
int metadataSection;
int pspSection = -1;
if (addressingVersion == AddressingVersion.WSAddressing10)
{
isAnonymous = ReadContentsFrom10(reader, out uri, out headers, out identity, out buffer, out metadataSection, out extensionSection);
}
else if (addressingVersion == AddressingVersion.WSAddressingAugust2004)
{
isAnonymous = ReadContentsFrom200408(reader, out uri, out headers, out identity, out buffer, out metadataSection, out extensionSection, out pspSection);
}
else
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(addressingVersion),
SRP.Format(SRP.AddressingVersionNotSupported, addressingVersion));
}
if (isAnonymous && headers == null && identity == null && buffer == null)
{
return AnonymousAddress;
}
else
{
return new EndpointAddress(addressingVersion, uri, identity, headers, buffer, metadataSection, extensionSection, pspSection);
}
}
internal static XmlBuffer ReadExtensions(XmlDictionaryReader reader, AddressingVersion version, XmlBuffer buffer, out EndpointIdentity identity, out int section)
{
if (reader == null)
{
identity = null;
section = -1;
return buffer;
}
// EndpointIdentity and extensions
identity = null;
XmlDictionaryWriter bufferWriter = null;
reader.MoveToContent();
while (reader.IsStartElement())
{
if (reader.IsStartElement(XD.AddressingDictionary.Identity, XD.AddressingDictionary.IdentityExtensionNamespace))
{
if (identity != null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateXmlException(reader, SRP.Format(SRP.UnexpectedDuplicateElement, XD.AddressingDictionary.Identity.Value, XD.AddressingDictionary.IdentityExtensionNamespace.Value)));
}
identity = EndpointIdentity.ReadIdentity(reader);
}
else if (version != null && reader.NamespaceURI == version.Namespace)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateXmlException(reader, SRP.Format(SRP.AddressingExtensionInBadNS, reader.LocalName, reader.NamespaceURI)));
}
else
{
if (bufferWriter == null)
{
if (buffer == null)
{
buffer = new XmlBuffer(short.MaxValue);
}
bufferWriter = buffer.OpenSection(reader.Quotas);
bufferWriter.WriteStartElement(DummyName, DummyNamespace);
}
bufferWriter.WriteNode(reader, true);
}
reader.MoveToContent();
}
if (bufferWriter != null)
{
bufferWriter.WriteEndElement();
buffer.CloseSection();
section = buffer.SectionCount - 1;
}
else
{
section = -1;
}
return buffer;
}
private static bool ReadContentsFrom200408(XmlDictionaryReader reader, out Uri uri, out AddressHeaderCollection headers, out EndpointIdentity identity, out XmlBuffer buffer, out int metadataSection, out int extensionSection, out int pspSection)
{
buffer = null;
headers = null;
extensionSection = -1;
metadataSection = -1;
pspSection = -1;
// Cache address string
reader.MoveToContent();
if (!reader.IsStartElement(XD.AddressingDictionary.Address, AddressingVersion.WSAddressingAugust2004.DictionaryNamespace))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateXmlException(reader, SRP.Format(SRP.UnexpectedElementExpectingElement, reader.LocalName, reader.NamespaceURI, XD.AddressingDictionary.Address.Value, XD.Addressing200408Dictionary.Namespace.Value)));
}
string address = reader.ReadElementContentAsString();
// ReferenceProperites
reader.MoveToContent();
if (reader.IsStartElement(XD.AddressingDictionary.ReferenceProperties, AddressingVersion.WSAddressingAugust2004.DictionaryNamespace))
{
headers = AddressHeaderCollection.ReadServiceParameters(reader, true);
}
// ReferenceParameters
reader.MoveToContent();
if (reader.IsStartElement(XD.AddressingDictionary.ReferenceParameters, AddressingVersion.WSAddressingAugust2004.DictionaryNamespace))
{
if (headers != null)
{
List<AddressHeader> headerList = new List<AddressHeader>();
foreach (AddressHeader ah in headers)
{
headerList.Add(ah);
}
AddressHeaderCollection tmp = AddressHeaderCollection.ReadServiceParameters(reader);
foreach (AddressHeader ah in tmp)
{
headerList.Add(ah);
}
headers = new AddressHeaderCollection(headerList);
}
else
{
headers = AddressHeaderCollection.ReadServiceParameters(reader);
}
}
XmlDictionaryWriter bufferWriter = null;
// PortType
reader.MoveToContent();
if (reader.IsStartElement(XD.AddressingDictionary.PortType, AddressingVersion.WSAddressingAugust2004.DictionaryNamespace))
{
if (bufferWriter == null)
{
if (buffer == null)
{
buffer = new XmlBuffer(short.MaxValue);
}
bufferWriter = buffer.OpenSection(reader.Quotas);
bufferWriter.WriteStartElement(DummyName, DummyNamespace);
}
bufferWriter.WriteNode(reader, true);
}
// ServiceName
reader.MoveToContent();
if (reader.IsStartElement(XD.AddressingDictionary.ServiceName, AddressingVersion.WSAddressingAugust2004.DictionaryNamespace))
{
if (bufferWriter == null)
{
if (buffer == null)
{
buffer = new XmlBuffer(short.MaxValue);
}
bufferWriter = buffer.OpenSection(reader.Quotas);
bufferWriter.WriteStartElement(DummyName, DummyNamespace);
}
bufferWriter.WriteNode(reader, true);
}
// Policy
reader.MoveToContent();
while (reader.IsNamespaceUri(PolicyStrings.Namespace))
{
if (bufferWriter == null)
{
if (buffer == null)
{
buffer = new XmlBuffer(short.MaxValue);
}
bufferWriter = buffer.OpenSection(reader.Quotas);
bufferWriter.WriteStartElement(DummyName, DummyNamespace);
}
bufferWriter.WriteNode(reader, true);
reader.MoveToContent();
}
// Finish PSP
if (bufferWriter != null)
{
bufferWriter.WriteEndElement();
buffer.CloseSection();
pspSection = buffer.SectionCount - 1;
bufferWriter = null;
}
else
{
pspSection = -1;
}
// Metadata
if (reader.IsStartElement(System.ServiceModel.Description.MetadataStrings.MetadataExchangeStrings.Metadata,
System.ServiceModel.Description.MetadataStrings.MetadataExchangeStrings.Namespace))
{
if (bufferWriter == null)
{
if (buffer == null)
{
buffer = new XmlBuffer(short.MaxValue);
}
bufferWriter = buffer.OpenSection(reader.Quotas);
bufferWriter.WriteStartElement(DummyName, DummyNamespace);
}
bufferWriter.WriteNode(reader, true);
}
// Finish metadata
if (bufferWriter != null)
{
bufferWriter.WriteEndElement();
buffer.CloseSection();
metadataSection = buffer.SectionCount - 1;
bufferWriter = null;
}
else
{
metadataSection = -1;
}
// Extensions
reader.MoveToContent();
buffer = ReadExtensions(reader, AddressingVersion.WSAddressingAugust2004, buffer, out identity, out extensionSection);
// Finished reading
if (buffer != null)
{
buffer.Close();
}
// Process Address
if (address == Addressing200408Strings.Anonymous)
{
uri = AddressingVersion.WSAddressingAugust2004.AnonymousUri;
if (headers == null && identity == null)
{
return true;
}
}
else
{
if (!Uri.TryCreate(address, UriKind.Absolute, out uri))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SRP.Format(SRP.InvalidUriValue, address, XD.AddressingDictionary.Address.Value, AddressingVersion.WSAddressingAugust2004.Namespace)));
}
}
return false;
}
private static bool ReadContentsFrom10(XmlDictionaryReader reader, out Uri uri, out AddressHeaderCollection headers, out EndpointIdentity identity, out XmlBuffer buffer, out int metadataSection, out int extensionSection)
{
buffer = null;
extensionSection = -1;
metadataSection = -1;
// Cache address string
if (!reader.IsStartElement(XD.AddressingDictionary.Address, XD.Addressing10Dictionary.Namespace))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateXmlException(reader, SRP.Format(SRP.UnexpectedElementExpectingElement, reader.LocalName, reader.NamespaceURI, XD.AddressingDictionary.Address.Value, XD.Addressing10Dictionary.Namespace.Value)));
}
string address = reader.ReadElementContentAsString();
// Headers
if (reader.IsStartElement(XD.AddressingDictionary.ReferenceParameters, XD.Addressing10Dictionary.Namespace))
{
headers = AddressHeaderCollection.ReadServiceParameters(reader);
}
else
{
headers = null;
}
// Metadata
if (reader.IsStartElement(XD.Addressing10Dictionary.Metadata, XD.Addressing10Dictionary.Namespace))
{
reader.ReadFullStartElement(); // the wsa10:Metadata element is never stored in the buffer
buffer = new XmlBuffer(short.MaxValue);
metadataSection = 0;
XmlDictionaryWriter writer = buffer.OpenSection(reader.Quotas);
writer.WriteStartElement(DummyName, DummyNamespace);
while (reader.NodeType != XmlNodeType.EndElement && !reader.EOF)
{
writer.WriteNode(reader, true);
}
writer.Flush();
buffer.CloseSection();
reader.ReadEndElement();
}
// Extensions
buffer = ReadExtensions(reader, AddressingVersion.WSAddressing10, buffer, out identity, out extensionSection);
if (buffer != null)
{
buffer.Close();
}
// Process Address
if (address == Addressing10Strings.Anonymous)
{
uri = AddressingVersion.WSAddressing10.AnonymousUri;
if (headers == null && identity == null)
{
return true;
}
}
else if (address == Addressing10Strings.NoneAddress)
{
uri = AddressingVersion.WSAddressing10.NoneUri;
return false;
}
else
{
if (!Uri.TryCreate(address, UriKind.Absolute, out uri))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SRP.Format(SRP.InvalidUriValue, address, XD.AddressingDictionary.Address.Value, XD.Addressing10Dictionary.Namespace.Value)));
}
}
return false;
}
private static XmlException CreateXmlException(XmlDictionaryReader reader, string message)
{
IXmlLineInfo lineInfo = reader as IXmlLineInfo;
if (lineInfo != null)
{
return new XmlException(message, null, lineInfo.LineNumber, lineInfo.LinePosition);
}
return new XmlException(message);
}
// this function has a side-effect on the reader (MoveToContent)
private static bool Done(XmlDictionaryReader reader)
{
reader.MoveToContent();
return (reader.NodeType == XmlNodeType.EndElement || reader.EOF);
}
// copy all of reader to writer
static internal void Copy(XmlDictionaryWriter writer, XmlDictionaryReader reader)
{
while (!Done(reader))
{
writer.WriteNode(reader, true);
}
}
public override string ToString()
{
return Uri.ToString();
}
public void WriteContentsTo(AddressingVersion addressingVersion, XmlDictionaryWriter writer)
{
if (writer == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer));
}
if (addressingVersion == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(addressingVersion));
}
if (addressingVersion == AddressingVersion.WSAddressing10)
{
WriteContentsTo10(writer);
}
else if (addressingVersion == AddressingVersion.WSAddressingAugust2004)
{
WriteContentsTo200408(writer);
}
else if (addressingVersion == AddressingVersion.None)
{
WriteContentsToNone(writer);
}
else
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(addressingVersion),
SRP.Format(SRP.AddressingVersionNotSupported, addressingVersion));
}
}
private void WriteContentsToNone(XmlDictionaryWriter writer)
{
writer.WriteString(Uri.AbsoluteUri);
}
private void WriteContentsTo200408(XmlDictionaryWriter writer)
{
// Address
writer.WriteStartElement(XD.AddressingDictionary.Address, XD.Addressing200408Dictionary.Namespace);
if (IsAnonymous)
{
writer.WriteString(XD.Addressing200408Dictionary.Anonymous);
}
else if (IsNone)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("addressingVersion", SRP.Format(SRP.SFxNone2004));
}
else
{