EndpointAddress.cs 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.ComponentModel;
  9. using System.Runtime;
  10. using System.ServiceModel.Channels;
  11. using System.Xml;
  12. public class EndpointAddress
  13. {
  14. static Uri anonymousUri;
  15. static Uri noneUri;
  16. static EndpointAddress anonymousAddress;
  17. /*
  18. Conceptually, the agnostic EndpointAddress class represents all of UNION(v200408,v10) data thusly:
  19. - Address Uri (both versions - the Address)
  20. - AddressHeaderCollection (both versions - RefProp&RefParam both project into here)
  21. - PSP blob (200408 - this is PortType, ServiceName, Policy, it is not surfaced in OM)
  22. - metadata (both versions, but weird semantics in 200408)
  23. - identity (both versions, this is the one 'extension' that we know about)
  24. - extensions (both versions, the "any*" stuff at the end)
  25. When reading from 200408:
  26. - Address is projected into Uri
  27. - both RefProps and RefParams are projected into AddressHeaderCollection,
  28. they (internally) remember 'which kind' they are
  29. - PortType, ServiceName, Policy are projected into the (internal) PSP blob
  30. - if we see a wsx:metadata element next, we project that element and that element only into the metadata reader
  31. - we read the rest, recognizing and fishing out identity if there, projecting rest to extensions reader
  32. When reading from 10:
  33. - Address is projected into Uri
  34. - RefParams are projected into AddressHeaderCollection; they (internally) remember 'which kind' they are
  35. - nothing is projected into the (internal) PSP blob (it's empty)
  36. - if there's a wsa10:metadata element, everything inside it projects into metadatareader
  37. - we read the rest, recognizing and fishing out identity if there, projecting rest to extensions reader
  38. When writing to 200408:
  39. - Uri is written as Address
  40. - AddressHeaderCollection is written as RefProps & RefParams, based on what they internally remember selves to be
  41. - PSP blob is written out verbatim (will have: PortType?, ServiceName?, Policy?)
  42. - metadata reader is written out verbatim
  43. - identity is written out as extension
  44. - extension reader is written out verbatim
  45. When writing to 10:
  46. - Uri is written as Address
  47. - AddressHeaderCollection is all written as RefParams, regardless of what they internally remember selves to be
  48. - PSP blob is ignored
  49. - if metadata reader is non-empty, we write its value out verbatim inside a wsa10:metadata element
  50. - identity is written out as extension
  51. - extension reader is written out verbatim
  52. EndpointAddressBuilder:
  53. - you can set metadata to any value you like; we don't (cannot) validate because 10 allows anything
  54. - you can set any extensions you like
  55. Known Weirdnesses:
  56. - PSP blob does not surface in OM - it can only roundtrip 200408wire->OM->200408wire
  57. - RefProperty distinction does not surface in OM - it can only roundtrip 200408wire->OM->200408wire
  58. - regardless of what metadata in reader, when you roundtrip OM->200408wire->OM, only wsx:metadata
  59. as first element after PSP will stay in metadata, anything else gets dumped in extensions
  60. - PSP blob is lost when doing OM->10wire->OM
  61. - RefProps turn into RefParams when doing OM->10wire->OM
  62. - Identity is always shuffled to front of extensions when doing anyWire->OM->anyWire
  63. */
  64. AddressingVersion addressingVersion;
  65. AddressHeaderCollection headers;
  66. EndpointIdentity identity;
  67. Uri uri;
  68. XmlBuffer buffer; // invariant: each section in the buffer will start with a dummy wrapper element
  69. int extensionSection;
  70. int metadataSection;
  71. int pspSection;
  72. bool isAnonymous;
  73. bool isNone;
  74. // these are the element name/namespace for the dummy wrapper element that wraps each buffer section
  75. internal const string DummyName = "Dummy";
  76. internal const string DummyNamespace = "http://Dummy";
  77. EndpointAddress(AddressingVersion version, Uri uri, EndpointIdentity identity, AddressHeaderCollection headers, XmlBuffer buffer, int metadataSection, int extensionSection, int pspSection)
  78. {
  79. Init(version, uri, identity, headers, buffer, metadataSection, extensionSection, pspSection);
  80. }
  81. public EndpointAddress(string uri)
  82. {
  83. if (uri == null)
  84. {
  85. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("uri");
  86. }
  87. Uri u = new Uri(uri);
  88. Init(u, (EndpointIdentity)null, (AddressHeaderCollection)null, null, -1, -1, -1);
  89. }
  90. public EndpointAddress(Uri uri, params AddressHeader[] addressHeaders)
  91. : this(uri, (EndpointIdentity)null, addressHeaders)
  92. {
  93. }
  94. public EndpointAddress(Uri uri, EndpointIdentity identity, params AddressHeader[] addressHeaders)
  95. {
  96. if (uri == null)
  97. {
  98. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("uri");
  99. }
  100. Init(uri, identity, addressHeaders);
  101. }
  102. public EndpointAddress(Uri uri, EndpointIdentity identity, AddressHeaderCollection headers)
  103. {
  104. if (uri == null)
  105. {
  106. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("uri");
  107. }
  108. Init(uri, identity, headers, null, -1, -1, -1);
  109. }
  110. internal EndpointAddress(Uri newUri, EndpointAddress oldEndpointAddress)
  111. {
  112. Init(oldEndpointAddress.addressingVersion, newUri, oldEndpointAddress.identity, oldEndpointAddress.headers, oldEndpointAddress.buffer, oldEndpointAddress.metadataSection, oldEndpointAddress.extensionSection, oldEndpointAddress.pspSection);
  113. }
  114. internal EndpointAddress(Uri uri, EndpointIdentity identity, AddressHeaderCollection headers, XmlDictionaryReader metadataReader, XmlDictionaryReader extensionReader, XmlDictionaryReader pspReader)
  115. {
  116. if (uri == null)
  117. {
  118. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("uri");
  119. }
  120. XmlBuffer buffer = null;
  121. PossiblyPopulateBuffer(metadataReader, ref buffer, out metadataSection);
  122. EndpointIdentity ident2;
  123. int extSection;
  124. buffer = ReadExtensions(extensionReader, null, buffer, out ident2, out extSection);
  125. if (identity != null && ident2 != null)
  126. {
  127. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.MultipleIdentities), "extensionReader"));
  128. }
  129. PossiblyPopulateBuffer(pspReader, ref buffer, out pspSection);
  130. if (buffer != null)
  131. {
  132. buffer.Close();
  133. }
  134. Init(uri, identity ?? ident2, headers, buffer, metadataSection, extSection, pspSection);
  135. }
  136. // metadataReader and extensionReader are assumed to not have a starting dummy wrapper element
  137. public EndpointAddress(Uri uri, EndpointIdentity identity, AddressHeaderCollection headers, XmlDictionaryReader metadataReader, XmlDictionaryReader extensionReader)
  138. : this(uri, identity, headers, metadataReader, extensionReader, null)
  139. {
  140. }
  141. void Init(Uri uri, EndpointIdentity identity, AddressHeader[] headers)
  142. {
  143. if (headers == null || headers.Length == 0)
  144. {
  145. Init(uri, identity, (AddressHeaderCollection)null, null, -1, -1, -1);
  146. }
  147. else
  148. {
  149. Init(uri, identity, new AddressHeaderCollection(headers), null, -1, -1, -1);
  150. }
  151. }
  152. void Init(Uri uri, EndpointIdentity identity, AddressHeaderCollection headers, XmlBuffer buffer, int metadataSection, int extensionSection, int pspSection)
  153. {
  154. Init(null, uri, identity, headers, buffer, metadataSection, extensionSection, pspSection);
  155. }
  156. void Init(AddressingVersion version, Uri uri, EndpointIdentity identity, AddressHeaderCollection headers, XmlBuffer buffer, int metadataSection, int extensionSection, int pspSection)
  157. {
  158. if (!uri.IsAbsoluteUri)
  159. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("uri", SR.GetString(SR.UriMustBeAbsolute));
  160. this.addressingVersion = version;
  161. this.uri = uri;
  162. this.identity = identity;
  163. this.headers = headers;
  164. this.buffer = buffer;
  165. this.metadataSection = metadataSection;
  166. this.extensionSection = extensionSection;
  167. this.pspSection = pspSection;
  168. if (version != null)
  169. {
  170. this.isAnonymous = uri == version.AnonymousUri;
  171. this.isNone = uri == version.NoneUri;
  172. }
  173. else
  174. {
  175. this.isAnonymous = object.ReferenceEquals(uri, AnonymousUri) || uri == AnonymousUri;
  176. this.isNone = object.ReferenceEquals(uri, NoneUri) || uri == NoneUri;
  177. }
  178. if (this.isAnonymous)
  179. {
  180. this.uri = AnonymousUri;
  181. }
  182. if (this.isNone)
  183. {
  184. this.uri = NoneUri;
  185. }
  186. }
  187. internal static EndpointAddress AnonymousAddress
  188. {
  189. get
  190. {
  191. if (anonymousAddress == null)
  192. anonymousAddress = new EndpointAddress(AnonymousUri);
  193. return anonymousAddress;
  194. }
  195. }
  196. public static Uri AnonymousUri
  197. {
  198. get
  199. {
  200. if (anonymousUri == null)
  201. anonymousUri = new Uri(AddressingStrings.AnonymousUri);
  202. return anonymousUri;
  203. }
  204. }
  205. public static Uri NoneUri
  206. {
  207. get
  208. {
  209. if (noneUri == null)
  210. noneUri = new Uri(AddressingStrings.NoneUri);
  211. return noneUri;
  212. }
  213. }
  214. internal XmlBuffer Buffer
  215. {
  216. get
  217. {
  218. return this.buffer;
  219. }
  220. }
  221. public AddressHeaderCollection Headers
  222. {
  223. get
  224. {
  225. if (this.headers == null)
  226. {
  227. this.headers = new AddressHeaderCollection();
  228. }
  229. return this.headers;
  230. }
  231. }
  232. public EndpointIdentity Identity
  233. {
  234. get
  235. {
  236. return this.identity;
  237. }
  238. }
  239. public bool IsAnonymous
  240. {
  241. get
  242. {
  243. return this.isAnonymous;
  244. }
  245. }
  246. public bool IsNone
  247. {
  248. get
  249. {
  250. return this.isNone;
  251. }
  252. }
  253. [TypeConverter(typeof(UriTypeConverter))]
  254. public Uri Uri
  255. {
  256. get
  257. {
  258. return uri;
  259. }
  260. }
  261. public void ApplyTo(Message message)
  262. {
  263. if (message == null)
  264. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("message");
  265. Uri uri = this.Uri;
  266. if (IsAnonymous)
  267. {
  268. #pragma warning suppress 56506
  269. if (message.Version.Addressing == AddressingVersion.WSAddressing10)
  270. {
  271. message.Headers.To = null;
  272. }
  273. else if (message.Version.Addressing == AddressingVersion.WSAddressingAugust2004)
  274. {
  275. #pragma warning suppress 56506
  276. message.Headers.To = message.Version.Addressing.AnonymousUri;
  277. }
  278. else
  279. {
  280. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  281. new ProtocolException(SR.GetString(SR.AddressingVersionNotSupported, message.Version.Addressing)));
  282. }
  283. }
  284. else if (IsNone)
  285. {
  286. message.Headers.To = message.Version.Addressing.NoneUri;
  287. }
  288. else
  289. {
  290. message.Headers.To = uri;
  291. }
  292. message.Properties.Via = message.Headers.To;
  293. if (this.headers != null)
  294. {
  295. this.headers.AddHeadersTo(message);
  296. }
  297. }
  298. // NOTE: UserInfo, Query, and Fragment are ignored when comparing Uris as addresses
  299. // this is the WCF logic for comparing Uris that represent addresses
  300. // this method must be kept in [....] with UriGetHashCode
  301. internal static bool UriEquals(Uri u1, Uri u2, bool ignoreCase, bool includeHostInComparison)
  302. {
  303. return UriEquals(u1, u2, ignoreCase, includeHostInComparison, true);
  304. }
  305. internal static bool UriEquals(Uri u1, Uri u2, bool ignoreCase, bool includeHostInComparison, bool includePortInComparison)
  306. {
  307. // PERF: Equals compares everything but UserInfo and Fragments. It's more strict than
  308. // we are, and faster, so it is done first.
  309. if (u1.Equals(u2))
  310. {
  311. return true;
  312. }
  313. if (u1.Scheme != u2.Scheme) // Uri.Scheme is always lowercase
  314. {
  315. return false;
  316. }
  317. if (includePortInComparison)
  318. {
  319. if (u1.Port != u2.Port)
  320. {
  321. return false;
  322. }
  323. }
  324. if (includeHostInComparison)
  325. {
  326. if (string.Compare(u1.Host, u2.Host, StringComparison.OrdinalIgnoreCase) != 0)
  327. {
  328. return false;
  329. }
  330. }
  331. if (string.Compare(u1.AbsolutePath, u2.AbsolutePath, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) == 0)
  332. {
  333. return true;
  334. }
  335. // Normalize for trailing slashes
  336. string u1Path = u1.GetComponents(UriComponents.Path, UriFormat.Unescaped);
  337. string u2Path = u2.GetComponents(UriComponents.Path, UriFormat.Unescaped);
  338. int u1Len = (u1Path.Length > 0 && u1Path[u1Path.Length - 1] == '/') ? u1Path.Length - 1 : u1Path.Length;
  339. int u2Len = (u2Path.Length > 0 && u2Path[u2Path.Length - 1] == '/') ? u2Path.Length - 1 : u2Path.Length;
  340. if (u2Len != u1Len)
  341. {
  342. return false;
  343. }
  344. return string.Compare(u1Path, 0, u2Path, 0, u1Len, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) == 0;
  345. }
  346. // this method must be kept in [....] with UriEquals
  347. internal static int UriGetHashCode(Uri uri, bool includeHostInComparison)
  348. {
  349. return UriGetHashCode(uri, includeHostInComparison, true);
  350. }
  351. internal static int UriGetHashCode(Uri uri, bool includeHostInComparison, bool includePortInComparison)
  352. {
  353. UriComponents components = UriComponents.Scheme | UriComponents.Path;
  354. if (includePortInComparison)
  355. {
  356. components = components | UriComponents.Port;
  357. }
  358. if (includeHostInComparison)
  359. {
  360. components = components | UriComponents.Host;
  361. }
  362. // Normalize for trailing slashes
  363. string uriString = uri.GetComponents(components, UriFormat.Unescaped);
  364. if (uriString.Length > 0 && uriString[uriString.Length - 1] != '/')
  365. uriString = string.Concat(uriString, "/");
  366. return StringComparer.OrdinalIgnoreCase.GetHashCode(uriString);
  367. }
  368. internal bool EndpointEquals(EndpointAddress endpointAddress)
  369. {
  370. if (endpointAddress == null)
  371. {
  372. return false;
  373. }
  374. if (object.ReferenceEquals(this, endpointAddress))
  375. {
  376. return true;
  377. }
  378. Uri thisTo = this.Uri;
  379. Uri otherTo = endpointAddress.Uri;
  380. if (!UriEquals(thisTo, otherTo, false /* ignoreCase */, true /* includeHostInComparison */))
  381. {
  382. return false;
  383. }
  384. if (this.Identity == null)
  385. {
  386. if (endpointAddress.Identity != null)
  387. {
  388. return false;
  389. }
  390. }
  391. else if (!this.Identity.Equals(endpointAddress.Identity))
  392. {
  393. return false;
  394. }
  395. if (!this.Headers.IsEquivalent(endpointAddress.Headers))
  396. {
  397. return false;
  398. }
  399. return true;
  400. }
  401. public override bool Equals(object obj)
  402. {
  403. if (object.ReferenceEquals(obj, this))
  404. {
  405. return true;
  406. }
  407. if (obj == null)
  408. {
  409. return false;
  410. }
  411. EndpointAddress address = obj as EndpointAddress;
  412. if (address == null)
  413. {
  414. return false;
  415. }
  416. return EndpointEquals(address);
  417. }
  418. public override int GetHashCode()
  419. {
  420. return UriGetHashCode(this.uri, true /* includeHostInComparison */);
  421. }
  422. // returns reader without starting dummy wrapper element
  423. internal XmlDictionaryReader GetReaderAtPsp()
  424. {
  425. return GetReaderAtSection(this.buffer, this.pspSection);
  426. }
  427. // returns reader without starting dummy wrapper element
  428. public XmlDictionaryReader GetReaderAtMetadata()
  429. {
  430. return GetReaderAtSection(this.buffer, this.metadataSection);
  431. }
  432. // returns reader without starting dummy wrapper element
  433. public XmlDictionaryReader GetReaderAtExtensions()
  434. {
  435. return GetReaderAtSection(this.buffer, this.extensionSection);
  436. }
  437. static XmlDictionaryReader GetReaderAtSection(XmlBuffer buffer, int section)
  438. {
  439. if (buffer == null || section < 0)
  440. return null;
  441. XmlDictionaryReader reader = buffer.GetReader(section);
  442. reader.MoveToContent();
  443. Fx.Assert(reader.Name == DummyName, "EndpointAddress: Expected dummy element not found");
  444. reader.Read(); // consume the dummy wrapper element
  445. return reader;
  446. }
  447. void PossiblyPopulateBuffer(XmlDictionaryReader reader, ref XmlBuffer buffer, out int section)
  448. {
  449. if (reader == null)
  450. {
  451. section = -1;
  452. }
  453. else
  454. {
  455. if (buffer == null)
  456. {
  457. buffer = new XmlBuffer(short.MaxValue);
  458. }
  459. section = buffer.SectionCount;
  460. XmlDictionaryWriter writer = buffer.OpenSection(reader.Quotas);
  461. writer.WriteStartElement(DummyName, DummyNamespace);
  462. Copy(writer, reader);
  463. buffer.CloseSection();
  464. }
  465. }
  466. public static EndpointAddress ReadFrom(XmlDictionaryReader reader)
  467. {
  468. AddressingVersion dummyVersion;
  469. return ReadFrom(reader, out dummyVersion);
  470. }
  471. internal static EndpointAddress ReadFrom(XmlDictionaryReader reader, out AddressingVersion version)
  472. {
  473. if (reader == null)
  474. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader");
  475. reader.ReadFullStartElement();
  476. reader.MoveToContent();
  477. if (reader.IsNamespaceUri(AddressingVersion.WSAddressing10.DictionaryNamespace))
  478. {
  479. version = AddressingVersion.WSAddressing10;
  480. }
  481. else if (reader.IsNamespaceUri(AddressingVersion.WSAddressingAugust2004.DictionaryNamespace))
  482. {
  483. version = AddressingVersion.WSAddressingAugust2004;
  484. }
  485. else if (reader.NodeType != XmlNodeType.Element)
  486. {
  487. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(
  488. "reader", SR.GetString(SR.CannotDetectAddressingVersion));
  489. }
  490. else
  491. {
  492. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(
  493. "reader", SR.GetString(SR.AddressingVersionNotSupported, reader.NamespaceURI));
  494. }
  495. EndpointAddress ea = ReadFromDriver(version, reader);
  496. reader.ReadEndElement();
  497. return ea;
  498. }
  499. public static EndpointAddress ReadFrom(XmlDictionaryReader reader, XmlDictionaryString localName, XmlDictionaryString ns)
  500. {
  501. AddressingVersion version;
  502. return ReadFrom(reader, localName, ns, out version);
  503. }
  504. internal static EndpointAddress ReadFrom(XmlDictionaryReader reader, XmlDictionaryString localName, XmlDictionaryString ns, out AddressingVersion version)
  505. {
  506. if (reader == null)
  507. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader");
  508. reader.ReadFullStartElement(localName, ns);
  509. reader.MoveToContent();
  510. if (reader.IsNamespaceUri(AddressingVersion.WSAddressing10.DictionaryNamespace))
  511. {
  512. version = AddressingVersion.WSAddressing10;
  513. }
  514. else if (reader.IsNamespaceUri(AddressingVersion.WSAddressingAugust2004.DictionaryNamespace))
  515. {
  516. version = AddressingVersion.WSAddressingAugust2004;
  517. }
  518. else if (reader.NodeType != XmlNodeType.Element)
  519. {
  520. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(
  521. "reader", SR.GetString(SR.CannotDetectAddressingVersion));
  522. }
  523. else
  524. {
  525. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(
  526. "reader", SR.GetString(SR.AddressingVersionNotSupported, reader.NamespaceURI));
  527. }
  528. EndpointAddress ea = ReadFromDriver(version, reader);
  529. reader.ReadEndElement();
  530. return ea;
  531. }
  532. public static EndpointAddress ReadFrom(AddressingVersion addressingVersion, XmlReader reader)
  533. {
  534. return ReadFrom(addressingVersion, XmlDictionaryReader.CreateDictionaryReader(reader));
  535. }
  536. public static EndpointAddress ReadFrom(AddressingVersion addressingVersion, XmlReader reader, string localName, string ns)
  537. {
  538. if (reader == null)
  539. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader");
  540. if (addressingVersion == null)
  541. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("addressingVersion");
  542. XmlDictionaryReader dictReader = XmlDictionaryReader.CreateDictionaryReader(reader);
  543. dictReader.ReadFullStartElement(localName, ns);
  544. EndpointAddress ea = ReadFromDriver(addressingVersion, dictReader);
  545. reader.ReadEndElement();
  546. return ea;
  547. }
  548. public static EndpointAddress ReadFrom(AddressingVersion addressingVersion, XmlDictionaryReader reader)
  549. {
  550. if (reader == null)
  551. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader");
  552. if (addressingVersion == null)
  553. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("addressingVersion");
  554. reader.ReadFullStartElement();
  555. EndpointAddress ea = ReadFromDriver(addressingVersion, reader);
  556. reader.ReadEndElement();
  557. return ea;
  558. }
  559. public static EndpointAddress ReadFrom(AddressingVersion addressingVersion, XmlDictionaryReader reader, XmlDictionaryString localName, XmlDictionaryString ns)
  560. {
  561. if (reader == null)
  562. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader");
  563. if (addressingVersion == null)
  564. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("addressingVersion");
  565. reader.ReadFullStartElement(localName, ns);
  566. EndpointAddress ea = ReadFromDriver(addressingVersion, reader);
  567. reader.ReadEndElement();
  568. return ea;
  569. }
  570. static EndpointAddress ReadFromDriver(AddressingVersion addressingVersion, XmlDictionaryReader reader)
  571. {
  572. AddressHeaderCollection headers;
  573. EndpointIdentity identity;
  574. Uri uri;
  575. XmlBuffer buffer;
  576. bool isAnonymous;
  577. int extensionSection;
  578. int metadataSection;
  579. int pspSection = -1;
  580. if (addressingVersion == AddressingVersion.WSAddressing10)
  581. {
  582. isAnonymous = ReadContentsFrom10(reader, out uri, out headers, out identity, out buffer, out metadataSection, out extensionSection);
  583. }
  584. else if (addressingVersion == AddressingVersion.WSAddressingAugust2004)
  585. {
  586. isAnonymous = ReadContentsFrom200408(reader, out uri, out headers, out identity, out buffer, out metadataSection, out extensionSection, out pspSection);
  587. }
  588. else
  589. {
  590. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("addressingVersion",
  591. SR.GetString(SR.AddressingVersionNotSupported, addressingVersion));
  592. }
  593. if (isAnonymous && headers == null && identity == null && buffer == null)
  594. {
  595. return AnonymousAddress;
  596. }
  597. else
  598. {
  599. return new EndpointAddress(addressingVersion, uri, identity, headers, buffer, metadataSection, extensionSection, pspSection);
  600. }
  601. }
  602. internal static XmlBuffer ReadExtensions(XmlDictionaryReader reader, AddressingVersion version, XmlBuffer buffer, out EndpointIdentity identity, out int section)
  603. {
  604. if (reader == null)
  605. {
  606. identity = null;
  607. section = -1;
  608. return buffer;
  609. }
  610. // EndpointIdentity and extensions
  611. identity = null;
  612. XmlDictionaryWriter bufferWriter = null;
  613. reader.MoveToContent();
  614. while (reader.IsStartElement())
  615. {
  616. if (reader.IsStartElement(XD.AddressingDictionary.Identity, XD.AddressingDictionary.IdentityExtensionNamespace))
  617. {
  618. if (identity != null)
  619. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateXmlException(reader, SR.GetString(SR.UnexpectedDuplicateElement, XD.AddressingDictionary.Identity.Value, XD.AddressingDictionary.IdentityExtensionNamespace.Value)));
  620. identity = EndpointIdentity.ReadIdentity(reader);
  621. }
  622. else if (version != null && reader.NamespaceURI == version.Namespace)
  623. {
  624. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateXmlException(reader, SR.GetString(SR.AddressingExtensionInBadNS, reader.LocalName, reader.NamespaceURI)));
  625. }
  626. else
  627. {
  628. if (bufferWriter == null)
  629. {
  630. if (buffer == null)
  631. buffer = new XmlBuffer(short.MaxValue);
  632. bufferWriter = buffer.OpenSection(reader.Quotas);
  633. bufferWriter.WriteStartElement(DummyName, DummyNamespace);
  634. }
  635. bufferWriter.WriteNode(reader, true);
  636. }
  637. reader.MoveToContent();
  638. }
  639. if (bufferWriter != null)
  640. {
  641. bufferWriter.WriteEndElement();
  642. buffer.CloseSection();
  643. section = buffer.SectionCount - 1;
  644. }
  645. else
  646. {
  647. section = -1;
  648. }
  649. return buffer;
  650. }
  651. 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)
  652. {
  653. buffer = null;
  654. headers = null;
  655. extensionSection = -1;
  656. metadataSection = -1;
  657. pspSection = -1;
  658. // Cache address string
  659. reader.MoveToContent();
  660. if (!reader.IsStartElement(XD.AddressingDictionary.Address, AddressingVersion.WSAddressingAugust2004.DictionaryNamespace))
  661. {
  662. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateXmlException(reader, SR.GetString(SR.UnexpectedElementExpectingElement, reader.LocalName, reader.NamespaceURI, XD.AddressingDictionary.Address.Value, XD.Addressing200408Dictionary.Namespace.Value)));
  663. }
  664. string address = reader.ReadElementContentAsString();
  665. // ReferenceProperites
  666. reader.MoveToContent();
  667. if (reader.IsStartElement(XD.AddressingDictionary.ReferenceProperties, AddressingVersion.WSAddressingAugust2004.DictionaryNamespace))
  668. {
  669. headers = AddressHeaderCollection.ReadServiceParameters(reader, true);
  670. }
  671. // ReferenceParameters
  672. reader.MoveToContent();
  673. if (reader.IsStartElement(XD.AddressingDictionary.ReferenceParameters, AddressingVersion.WSAddressingAugust2004.DictionaryNamespace))
  674. {
  675. if (headers != null)
  676. {
  677. List<AddressHeader> headerList = new List<AddressHeader>();
  678. foreach (AddressHeader ah in headers)
  679. {
  680. headerList.Add(ah);
  681. }
  682. AddressHeaderCollection tmp = AddressHeaderCollection.ReadServiceParameters(reader);
  683. foreach (AddressHeader ah in tmp)
  684. {
  685. headerList.Add(ah);
  686. }
  687. headers = new AddressHeaderCollection(headerList);
  688. }
  689. else
  690. {
  691. headers = AddressHeaderCollection.ReadServiceParameters(reader);
  692. }
  693. }
  694. XmlDictionaryWriter bufferWriter = null;
  695. // PortType
  696. reader.MoveToContent();
  697. if (reader.IsStartElement(XD.AddressingDictionary.PortType, AddressingVersion.WSAddressingAugust2004.DictionaryNamespace))
  698. {
  699. if (bufferWriter == null)
  700. {
  701. if (buffer == null)
  702. buffer = new XmlBuffer(short.MaxValue);
  703. bufferWriter = buffer.OpenSection(reader.Quotas);
  704. bufferWriter.WriteStartElement(DummyName, DummyNamespace);
  705. }
  706. bufferWriter.WriteNode(reader, true);
  707. }
  708. // ServiceName
  709. reader.MoveToContent();
  710. if (reader.IsStartElement(XD.AddressingDictionary.ServiceName, AddressingVersion.WSAddressingAugust2004.DictionaryNamespace))
  711. {
  712. if (bufferWriter == null)
  713. {
  714. if (buffer == null)
  715. buffer = new XmlBuffer(short.MaxValue);
  716. bufferWriter = buffer.OpenSection(reader.Quotas);
  717. bufferWriter.WriteStartElement(DummyName, DummyNamespace);
  718. }
  719. bufferWriter.WriteNode(reader, true);
  720. }
  721. // Policy
  722. reader.MoveToContent();
  723. while (reader.IsNamespaceUri(XD.PolicyDictionary.Namespace))
  724. {
  725. if (bufferWriter == null)
  726. {
  727. if (buffer == null)
  728. buffer = new XmlBuffer(short.MaxValue);
  729. bufferWriter = buffer.OpenSection(reader.Quotas);
  730. bufferWriter.WriteStartElement(DummyName, DummyNamespace);
  731. }
  732. bufferWriter.WriteNode(reader, true);
  733. reader.MoveToContent();
  734. }
  735. // Finish PSP
  736. if (bufferWriter != null)
  737. {
  738. bufferWriter.WriteEndElement();
  739. buffer.CloseSection();
  740. pspSection = buffer.SectionCount - 1;
  741. bufferWriter = null;
  742. }
  743. else
  744. {
  745. pspSection = -1;
  746. }
  747. // Metadata
  748. if (reader.IsStartElement(System.ServiceModel.Description.MetadataStrings.MetadataExchangeStrings.Metadata,
  749. System.ServiceModel.Description.MetadataStrings.MetadataExchangeStrings.Namespace))
  750. {
  751. if (bufferWriter == null)
  752. {
  753. if (buffer == null)
  754. buffer = new XmlBuffer(short.MaxValue);
  755. bufferWriter = buffer.OpenSection(reader.Quotas);
  756. bufferWriter.WriteStartElement(DummyName, DummyNamespace);
  757. }
  758. bufferWriter.WriteNode(reader, true);
  759. }
  760. // Finish metadata
  761. if (bufferWriter != null)
  762. {
  763. bufferWriter.WriteEndElement();
  764. buffer.CloseSection();
  765. metadataSection = buffer.SectionCount - 1;
  766. bufferWriter = null;
  767. }
  768. else
  769. {
  770. metadataSection = -1;
  771. }
  772. // Extensions
  773. reader.MoveToContent();
  774. buffer = ReadExtensions(reader, AddressingVersion.WSAddressingAugust2004, buffer, out identity, out extensionSection);
  775. // Finished reading
  776. if (buffer != null)
  777. buffer.Close();
  778. // Process Address
  779. if (address == Addressing200408Strings.Anonymous)
  780. {
  781. uri = AddressingVersion.WSAddressingAugust2004.AnonymousUri;
  782. if (headers == null && identity == null)
  783. return true;
  784. }
  785. else
  786. {
  787. if (!Uri.TryCreate(address, UriKind.Absolute, out uri))
  788. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.InvalidUriValue, address, XD.AddressingDictionary.Address.Value, AddressingVersion.WSAddressingAugust2004.Namespace)));
  789. }
  790. return false;
  791. }
  792. static bool ReadContentsFrom10(XmlDictionaryReader reader, out Uri uri, out AddressHeaderCollection headers, out EndpointIdentity identity, out XmlBuffer buffer, out int metadataSection, out int extensionSection)
  793. {
  794. buffer = null;
  795. extensionSection = -1;
  796. metadataSection = -1;
  797. // Cache address string
  798. if (!reader.IsStartElement(XD.AddressingDictionary.Address, XD.Addressing10Dictionary.Namespace))
  799. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateXmlException(reader, SR.GetString(SR.UnexpectedElementExpectingElement, reader.LocalName, reader.NamespaceURI, XD.AddressingDictionary.Address.Value, XD.Addressing10Dictionary.Namespace.Value)));
  800. string address = reader.ReadElementContentAsString();
  801. // Headers
  802. if (reader.IsStartElement(XD.AddressingDictionary.ReferenceParameters, XD.Addressing10Dictionary.Namespace))
  803. {
  804. headers = AddressHeaderCollection.ReadServiceParameters(reader);
  805. }
  806. else
  807. {
  808. headers = null;
  809. }
  810. // Metadata
  811. if (reader.IsStartElement(XD.Addressing10Dictionary.Metadata, XD.Addressing10Dictionary.Namespace))
  812. {
  813. reader.ReadFullStartElement(); // the wsa10:Metadata element is never stored in the buffer
  814. buffer = new XmlBuffer(short.MaxValue);
  815. metadataSection = 0;
  816. XmlDictionaryWriter writer = buffer.OpenSection(reader.Quotas);
  817. writer.WriteStartElement(DummyName, DummyNamespace);
  818. while (reader.NodeType != XmlNodeType.EndElement && !reader.EOF)
  819. {
  820. writer.WriteNode(reader, true);
  821. }
  822. writer.Flush();
  823. buffer.CloseSection();
  824. reader.ReadEndElement();
  825. }
  826. // Extensions
  827. buffer = ReadExtensions(reader, AddressingVersion.WSAddressing10, buffer, out identity, out extensionSection);
  828. if (buffer != null)
  829. {
  830. buffer.Close();
  831. }
  832. // Process Address
  833. if (address == Addressing10Strings.Anonymous)
  834. {
  835. uri = AddressingVersion.WSAddressing10.AnonymousUri;
  836. if (headers == null && identity == null)
  837. {
  838. return true;
  839. }
  840. }
  841. else if (address == Addressing10Strings.NoneAddress)
  842. {
  843. uri = AddressingVersion.WSAddressing10.NoneUri;
  844. return false;
  845. }
  846. else
  847. {
  848. if (!Uri.TryCreate(address, UriKind.Absolute, out uri))
  849. {
  850. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.InvalidUriValue, address, XD.AddressingDictionary.Address.Value, XD.Addressing10Dictionary.Namespace.Value)));
  851. }
  852. }
  853. return false;
  854. }
  855. static XmlException CreateXmlException(XmlDictionaryReader reader, string message)
  856. {
  857. IXmlLineInfo lineInfo = reader as IXmlLineInfo;
  858. if (lineInfo != null)
  859. {
  860. return new XmlException(message, null, lineInfo.LineNumber, lineInfo.LinePosition);
  861. }
  862. return new XmlException(message);
  863. }
  864. // this function has a side-effect on the reader (MoveToContent)
  865. static bool Done(XmlDictionaryReader reader)
  866. {
  867. reader.MoveToContent();
  868. return (reader.NodeType == XmlNodeType.EndElement || reader.EOF);
  869. }
  870. // copy all of reader to writer
  871. static internal void Copy(XmlDictionaryWriter writer, XmlDictionaryReader reader)
  872. {
  873. while (!Done(reader))
  874. {
  875. writer.WriteNode(reader, true);
  876. }
  877. }
  878. public override string ToString()
  879. {
  880. return uri.ToString();
  881. }
  882. public void WriteContentsTo(AddressingVersion addressingVersion, XmlDictionaryWriter writer)
  883. {
  884. if (writer == null)
  885. {
  886. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("writer");
  887. }
  888. if (addressingVersion == null)
  889. {
  890. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("addressingVersion");
  891. }
  892. if (addressingVersion == AddressingVersion.WSAddressing10)
  893. {
  894. WriteContentsTo10(writer);
  895. }
  896. else if (addressingVersion == AddressingVersion.WSAddressingAugust2004)
  897. {
  898. WriteContentsTo200408(writer);
  899. }
  900. else if (addressingVersion == AddressingVersion.None)
  901. {
  902. WriteContentsToNone(writer);
  903. }
  904. else
  905. {
  906. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("addressingVersion",
  907. SR.GetString(SR.AddressingVersionNotSupported, addressingVersion));
  908. }
  909. }
  910. void WriteContentsToNone(XmlDictionaryWriter writer)
  911. {
  912. writer.WriteString(this.Uri.AbsoluteUri);
  913. }
  914. void WriteContentsTo200408(XmlDictionaryWriter writer)
  915. {
  916. // Address
  917. writer.WriteStartElement(XD.AddressingDictionary.Address, XD.Addressing200408Dictionary.Namespace);
  918. if (isAnonymous)
  919. {
  920. writer.WriteString(XD.Addressing200408Dictionary.Anonymous);
  921. }
  922. else if (isNone)
  923. {
  924. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("addressingVersion", SR.GetString(SR.SFxNone2004));
  925. }
  926. else
  927. {
  928. writer.WriteString(this.Uri.AbsoluteUri);
  929. }
  930. writer.WriteEndElement();
  931. // ReferenceProperties
  932. if (this.headers != null && this.headers.HasReferenceProperties)
  933. {
  934. writer.WriteStartElement(XD.AddressingDictionary.ReferenceProperties, XD.Addressing200408Dictionary.Namespace);
  935. this.headers.WriteReferencePropertyContentsTo(writer);
  936. writer.WriteEndElement();
  937. }
  938. // ReferenceParameters
  939. if (this.headers != null && this.headers.HasNonReferenceProperties)
  940. {
  941. writer.WriteStartElement(XD.AddressingDictionary.ReferenceParameters, XD.Addressing200408Dictionary.Namespace);
  942. this.headers.WriteNonReferencePropertyContentsTo(writer);
  943. writer.WriteEndElement();
  944. }
  945. // PSP (PortType, ServiceName, Policy)
  946. XmlDictionaryReader reader = null;
  947. if (pspSection >= 0)
  948. {
  949. reader = GetReaderAtSection(buffer, pspSection);
  950. Copy(writer, reader);
  951. }
  952. // Metadata
  953. reader = null;
  954. if (metadataSection >= 0)
  955. {
  956. reader = GetReaderAtSection(buffer, metadataSection);
  957. Copy(writer, reader);
  958. }
  959. // EndpointIdentity
  960. if (this.Identity != null)
  961. {
  962. this.Identity.WriteTo(writer);
  963. }
  964. // Extensions
  965. if (this.extensionSection >= 0)
  966. {
  967. reader = GetReaderAtSection(this.buffer, extensionSection);
  968. while (reader.IsStartElement())
  969. {
  970. if (reader.NamespaceURI == AddressingVersion.WSAddressingAugust2004.Namespace)
  971. {
  972. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateXmlException(reader, SR.GetString(SR.AddressingExtensionInBadNS, reader.LocalName, reader.NamespaceURI)));
  973. }
  974. writer.WriteNode(reader, true);
  975. }
  976. }
  977. }
  978. void WriteContentsTo10(XmlDictionaryWriter writer)
  979. {
  980. // Address
  981. writer.WriteStartElement(XD.AddressingDictionary.Address, XD.Addressing10Dictionary.Namespace);
  982. if (isAnonymous)
  983. {
  984. writer.WriteString(XD.Addressing10Dictionary.Anonymous);
  985. }
  986. else if (isNone)
  987. {
  988. writer.WriteString(XD.Addressing10Dictionary.NoneAddress);
  989. }
  990. else
  991. {
  992. writer.WriteString(this.Uri.AbsoluteUri);
  993. }
  994. writer.WriteEndElement();
  995. // Headers
  996. if (this.headers != null && this.headers.Count > 0)
  997. {
  998. writer.WriteStartElement(XD.AddressingDictionary.ReferenceParameters, XD.Addressing10Dictionary.Namespace);
  999. this.headers.WriteContentsTo(writer);
  1000. writer.WriteEndElement();
  1001. }
  1002. // Metadata
  1003. if (this.metadataSection >= 0)
  1004. {
  1005. XmlDictionaryReader reader = GetReaderAtSection(this.buffer, metadataSection);
  1006. writer.WriteStartElement(XD.Addressing10Dictionary.Metadata, XD.Addressing10Dictionary.Namespace);
  1007. Copy(writer, reader);
  1008. writer.WriteEndElement();
  1009. }
  1010. // EndpointIdentity
  1011. if (this.Identity != null)
  1012. {
  1013. this.Identity.WriteTo(writer);
  1014. }
  1015. // Extensions
  1016. if (this.extensionSection >= 0)
  1017. {
  1018. XmlDictionaryReader reader = GetReaderAtSection(this.buffer, this.extensionSection);
  1019. while (reader.IsStartElement())
  1020. {
  1021. if (reader.NamespaceURI == AddressingVersion.WSAddressing10.Namespace)
  1022. {
  1023. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateXmlException(reader, SR.GetString(SR.AddressingExtensionInBadNS, reader.LocalName, reader.NamespaceURI)));
  1024. }
  1025. writer.WriteNode(reader, true);
  1026. }
  1027. }
  1028. }
  1029. public void WriteContentsTo(AddressingVersion addressingVersion, XmlWriter writer)
  1030. {
  1031. XmlDictionaryWriter dictionaryWriter = XmlDictionaryWriter.CreateDictionaryWriter(writer);
  1032. WriteContentsTo(addressingVersion, dictionaryWriter);
  1033. }
  1034. public void WriteTo(AddressingVersion addressingVersion, XmlDictionaryWriter writer)
  1035. {
  1036. WriteTo(addressingVersion, writer, XD.AddressingDictionary.EndpointReference,
  1037. addressingVersion.DictionaryNamespace);
  1038. }
  1039. public void WriteTo(AddressingVersion addressingVersion, XmlDictionaryWriter writer, XmlDictionaryString localName, XmlDictionaryString ns)
  1040. {
  1041. if (writer == null)
  1042. {
  1043. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("writer");
  1044. }
  1045. if (addressingVersion == null)
  1046. {
  1047. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("addressingVersion");
  1048. }
  1049. if (localName == null)
  1050. {
  1051. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("localName");
  1052. }
  1053. if (ns == null)
  1054. {
  1055. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("ns");
  1056. }
  1057. writer.WriteStartElement(localName, ns);
  1058. WriteContentsTo(addressingVersion, writer);
  1059. writer.WriteEndElement();
  1060. }
  1061. public void WriteTo(AddressingVersion addressingVersion, XmlWriter writer)
  1062. {
  1063. XmlDictionaryString dictionaryNamespace = addressingVersion.DictionaryNamespace;
  1064. if (dictionaryNamespace == null)
  1065. {
  1066. dictionaryNamespace = XD.AddressingDictionary.Empty;
  1067. }
  1068. WriteTo(addressingVersion, XmlDictionaryWriter.CreateDictionaryWriter(writer),
  1069. XD.AddressingDictionary.EndpointReference, dictionaryNamespace);
  1070. }
  1071. public void WriteTo(AddressingVersion addressingVersion, XmlWriter writer, string localName, string ns)
  1072. {
  1073. if (writer == null)
  1074. {
  1075. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("writer");
  1076. }
  1077. if (addressingVersion == null)
  1078. {
  1079. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("addressingVersion");
  1080. }
  1081. if (localName == null)
  1082. {
  1083. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("localName");
  1084. }
  1085. if (ns == null)
  1086. {
  1087. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("ns");
  1088. }
  1089. writer.WriteStartElement(localName, ns);
  1090. WriteContentsTo(addressingVersion, writer);
  1091. writer.WriteEndElement();
  1092. }
  1093. public static bool operator ==(EndpointAddress address1, EndpointAddress address2)
  1094. {
  1095. if (object.ReferenceEquals(address2, null))
  1096. {
  1097. return (object.ReferenceEquals(address1, null));
  1098. }
  1099. return address2.Equals(address1);
  1100. }
  1101. public static bool operator !=(EndpointAddress address1, EndpointAddress address2)
  1102. {
  1103. if (object.ReferenceEquals(address2, null))
  1104. {
  1105. return !object.ReferenceEquals(address1, null);
  1106. }
  1107. return !address2.Equals(address1);
  1108. }
  1109. }
  1110. public class EndpointAddressBuilder
  1111. {
  1112. Uri uri;
  1113. EndpointIdentity identity;
  1114. Collection<AddressHeader> headers;
  1115. XmlBuffer extensionBuffer; // this buffer is wrapped just like in EndpointAddress
  1116. XmlBuffer metadataBuffer; // this buffer is wrapped just like in EndpointAddress
  1117. bool hasExtension;
  1118. bool hasMetadata;
  1119. EndpointAddress epr;
  1120. public EndpointAddressBuilder()
  1121. {
  1122. this.headers = new Collection<AddressHeader>();
  1123. }
  1124. public EndpointAddressBuilder(EndpointAddress address)
  1125. {
  1126. if (address == null)
  1127. {
  1128. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("address");
  1129. }
  1130. this.epr = address;
  1131. this.uri = address.Uri;
  1132. this.identity = address.Identity;
  1133. this.headers = new Collection<AddressHeader>();
  1134. #pragma warning suppress 56506
  1135. for (int i = 0; i < address.Headers.Count; i++)
  1136. {
  1137. this.headers.Add(address.Headers[i]);
  1138. }
  1139. }
  1140. public Uri Uri
  1141. {
  1142. get { return this.uri; }
  1143. set { this.uri = value; }
  1144. }
  1145. public EndpointIdentity Identity
  1146. {
  1147. get { return this.identity; }
  1148. set { this.identity = value; }
  1149. }
  1150. public Collection<AddressHeader> Headers
  1151. {
  1152. get { return this.headers; }
  1153. }
  1154. public XmlDictionaryReader GetReaderAtMetadata()
  1155. {
  1156. if (!this.hasMetadata)
  1157. {
  1158. return epr == null ? null : epr.GetReaderAtMetadata();
  1159. }
  1160. if (this.metadataBuffer == null)
  1161. {
  1162. return null;
  1163. }
  1164. XmlDictionaryReader reader = this.metadataBuffer.GetReader(0);
  1165. reader.MoveToContent();
  1166. Fx.Assert(reader.Name == EndpointAddress.DummyName, "EndpointAddressBuilder: Expected dummy element not found");
  1167. reader.Read(); // consume the wrapper element
  1168. return reader;
  1169. }
  1170. public void SetMetadataReader(XmlDictionaryReader reader)
  1171. {
  1172. hasMetadata = true;
  1173. metadataBuffer = null;
  1174. if (reader != null)
  1175. {
  1176. metadataBuffer = new XmlBuffer(short.MaxValue);
  1177. XmlDictionaryWriter writer = metadataBuffer.OpenSection(reader.Quotas);
  1178. writer.WriteStartElement(EndpointAddress.DummyName, EndpointAddress.DummyNamespace);
  1179. EndpointAddress.Copy(writer, reader);
  1180. metadataBuffer.CloseSection();
  1181. metadataBuffer.Close();
  1182. }
  1183. }
  1184. public XmlDictionaryReader GetReaderAtExtensions()
  1185. {
  1186. if (!this.hasExtension)
  1187. {
  1188. return epr == null ? null : epr.GetReaderAtExtensions();
  1189. }
  1190. if (this.extensionBuffer == null)
  1191. {
  1192. return null;
  1193. }
  1194. XmlDictionaryReader reader = this.extensionBuffer.GetReader(0);
  1195. reader.MoveToContent();
  1196. Fx.Assert(reader.Name == EndpointAddress.DummyName, "EndpointAddressBuilder: Expected dummy element not found");
  1197. reader.Read(); // consume the wrapper element
  1198. return reader;
  1199. }
  1200. public void SetExtensionReader(XmlDictionaryReader reader)
  1201. {
  1202. hasExtension = true;
  1203. EndpointIdentity identity;
  1204. int tmp;
  1205. this.extensionBuffer = EndpointAddress.ReadExtensions(reader, null, null, out identity, out tmp);
  1206. if (this.extensionBuffer != null)
  1207. {
  1208. this.extensionBuffer.Close();
  1209. }
  1210. if (identity != null)
  1211. {
  1212. this.identity = identity;
  1213. }
  1214. }
  1215. public EndpointAddress ToEndpointAddress()
  1216. {
  1217. return new EndpointAddress(
  1218. this.uri,
  1219. this.identity,
  1220. new AddressHeaderCollection(this.headers),
  1221. this.GetReaderAtMetadata(),
  1222. this.GetReaderAtExtensions(),
  1223. epr == null ? null : epr.GetReaderAtPsp());
  1224. }
  1225. }
  1226. }