SoapHelper.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. //----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Description
  5. {
  6. using System.Collections.Generic;
  7. using System.Runtime;
  8. using System.ServiceModel.Dispatcher;
  9. using System.Xml;
  10. using WsdlNS = System.Web.Services.Description;
  11. static class SoapHelper
  12. {
  13. static object SoapVersionStateKey = new object();
  14. static XmlDocument xmlDocument;
  15. static XmlDocument Document
  16. {
  17. get
  18. {
  19. if (xmlDocument == null)
  20. xmlDocument = new XmlDocument();
  21. return xmlDocument;
  22. }
  23. }
  24. static XmlAttribute CreateLocalAttribute(string name, string value)
  25. {
  26. XmlAttribute attribute = Document.CreateAttribute(name);
  27. attribute.Value = value;
  28. return attribute;
  29. }
  30. // -----------------------------------------------------------------------------------------------------------------------
  31. // Developers Note: We go through a little song an dance here to Get or Create an exsisting SoapBinding from the WSDL
  32. // Extensions for a number of reasons:
  33. // 1. Multiple Extensions may contribute to the settings in the soap binding and so to make this work without
  34. // relying on ordering, we need the GetOrCreate method.
  35. // 2. There are diffrent classes for diffrent SOAP versions and the extensions that determines the version is
  36. // also un-ordered so when we finally figure out the version we may need to recreate the BindingExtension and
  37. // clone it.
  38. internal static WsdlNS.SoapAddressBinding GetOrCreateSoapAddressBinding(WsdlNS.Binding wsdlBinding, WsdlNS.Port wsdlPort, WsdlExporter exporter)
  39. {
  40. if (GetSoapVersionState(wsdlBinding, exporter) == EnvelopeVersion.None)
  41. return null;
  42. WsdlNS.SoapAddressBinding existingSoapAddressBinding = GetSoapAddressBinding(wsdlPort);
  43. EnvelopeVersion version = GetSoapVersion(wsdlBinding);
  44. if (existingSoapAddressBinding != null)
  45. return existingSoapAddressBinding;
  46. WsdlNS.SoapAddressBinding soapAddressBinding = CreateSoapAddressBinding(version, wsdlPort);
  47. return soapAddressBinding;
  48. }
  49. internal static WsdlNS.SoapBinding GetOrCreateSoapBinding(WsdlEndpointConversionContext endpointContext, WsdlExporter exporter)
  50. {
  51. if (GetSoapVersionState(endpointContext.WsdlBinding, exporter) == EnvelopeVersion.None)
  52. return null;
  53. WsdlNS.SoapBinding existingSoapBinding = GetSoapBinding(endpointContext);
  54. if (existingSoapBinding != null)
  55. {
  56. return existingSoapBinding;
  57. }
  58. EnvelopeVersion version = GetSoapVersion(endpointContext.WsdlBinding);
  59. WsdlNS.SoapBinding soapBinding = CreateSoapBinding(version, endpointContext.WsdlBinding);
  60. return soapBinding;
  61. }
  62. internal static WsdlNS.SoapOperationBinding GetOrCreateSoapOperationBinding(WsdlEndpointConversionContext endpointContext, OperationDescription operation, WsdlExporter exporter)
  63. {
  64. if (GetSoapVersionState(endpointContext.WsdlBinding, exporter) == EnvelopeVersion.None)
  65. return null;
  66. WsdlNS.SoapOperationBinding existingSoapOperationBinding = GetSoapOperationBinding(endpointContext, operation);
  67. WsdlNS.OperationBinding wsdlOperationBinding = endpointContext.GetOperationBinding(operation);
  68. EnvelopeVersion version = GetSoapVersion(endpointContext.WsdlBinding);
  69. if (existingSoapOperationBinding != null)
  70. return existingSoapOperationBinding;
  71. WsdlNS.SoapOperationBinding soapOperationBinding = CreateSoapOperationBinding(version, wsdlOperationBinding);
  72. return soapOperationBinding;
  73. }
  74. internal static WsdlNS.SoapBodyBinding GetOrCreateSoapBodyBinding(WsdlEndpointConversionContext endpointContext, WsdlNS.MessageBinding wsdlMessageBinding, WsdlExporter exporter)
  75. {
  76. if (GetSoapVersionState(endpointContext.WsdlBinding, exporter) == EnvelopeVersion.None)
  77. return null;
  78. WsdlNS.SoapBodyBinding existingSoapBodyBinding = GetSoapBodyBinding(endpointContext, wsdlMessageBinding);
  79. EnvelopeVersion version = GetSoapVersion(endpointContext.WsdlBinding);
  80. if (existingSoapBodyBinding != null)
  81. return existingSoapBodyBinding;
  82. WsdlNS.SoapBodyBinding soapBodyBinding = CreateSoapBodyBinding(version, wsdlMessageBinding);
  83. return soapBodyBinding;
  84. }
  85. internal static WsdlNS.SoapHeaderBinding CreateSoapHeaderBinding(WsdlEndpointConversionContext endpointContext, WsdlNS.MessageBinding wsdlMessageBinding)
  86. {
  87. EnvelopeVersion version = GetSoapVersion(endpointContext.WsdlBinding);
  88. WsdlNS.SoapHeaderBinding soapHeaderBinding = CreateSoapHeaderBinding(version, wsdlMessageBinding);
  89. return soapHeaderBinding;
  90. }
  91. internal static void CreateSoapFaultBinding(string name, WsdlEndpointConversionContext endpointContext, WsdlNS.FaultBinding wsdlFaultBinding, bool isEncoded)
  92. {
  93. EnvelopeVersion version = GetSoapVersion(endpointContext.WsdlBinding);
  94. XmlElement fault = CreateSoapFaultBinding(version);
  95. fault.Attributes.Append(CreateLocalAttribute("name", name));
  96. fault.Attributes.Append(CreateLocalAttribute("use", isEncoded ? "encoded" : "literal"));
  97. wsdlFaultBinding.Extensions.Add(fault);
  98. }
  99. internal static void SetSoapVersion(WsdlEndpointConversionContext endpointContext, WsdlExporter exporter, EnvelopeVersion version)
  100. {
  101. SetSoapVersionState(endpointContext.WsdlBinding, exporter, version);
  102. //Convert all SOAP extensions to the right version.
  103. if (endpointContext.WsdlPort != null)
  104. SoapConverter.ConvertExtensions(endpointContext.WsdlPort.Extensions, version, SoapConverter.ConvertSoapAddressBinding);
  105. SoapConverter.ConvertExtensions(endpointContext.WsdlBinding.Extensions, version, SoapConverter.ConvertSoapBinding);
  106. foreach (WsdlNS.OperationBinding operationBinding in endpointContext.WsdlBinding.Operations)
  107. {
  108. SoapConverter.ConvertExtensions(operationBinding.Extensions, version, SoapConverter.ConvertSoapOperationBinding);
  109. //Messages
  110. {
  111. if (operationBinding.Input != null)
  112. SoapConverter.ConvertExtensions(operationBinding.Input.Extensions, version, SoapConverter.ConvertSoapMessageBinding);
  113. if (operationBinding.Output != null)
  114. SoapConverter.ConvertExtensions(operationBinding.Output.Extensions, version, SoapConverter.ConvertSoapMessageBinding);
  115. foreach (WsdlNS.MessageBinding faultBinding in operationBinding.Faults)
  116. SoapConverter.ConvertExtensions(faultBinding.Extensions, version, SoapConverter.ConvertSoapMessageBinding);
  117. }
  118. }
  119. }
  120. internal static EnvelopeVersion GetSoapVersion(WsdlNS.Binding wsdlBinding)
  121. {
  122. foreach (object o in wsdlBinding.Extensions)
  123. {
  124. if (o is WsdlNS.SoapBinding)
  125. return o is WsdlNS.Soap12Binding ? EnvelopeVersion.Soap12 : EnvelopeVersion.Soap11;
  126. }
  127. return EnvelopeVersion.Soap12;
  128. }
  129. private static void SetSoapVersionState(WsdlNS.Binding wsdlBinding, WsdlExporter exporter, EnvelopeVersion version)
  130. {
  131. object versions = null;
  132. if (!exporter.State.TryGetValue(SoapVersionStateKey, out versions))
  133. {
  134. versions = new Dictionary<WsdlNS.Binding, EnvelopeVersion>();
  135. exporter.State[SoapVersionStateKey] = versions;
  136. }
  137. ((Dictionary<WsdlNS.Binding, EnvelopeVersion>)versions)[wsdlBinding] = version;
  138. }
  139. private static EnvelopeVersion GetSoapVersionState(WsdlNS.Binding wsdlBinding, WsdlExporter exporter)
  140. {
  141. object versions = null;
  142. if (exporter.State.TryGetValue(SoapVersionStateKey, out versions))
  143. {
  144. if (versions != null && ((Dictionary<WsdlNS.Binding, EnvelopeVersion>)versions).ContainsKey(wsdlBinding))
  145. {
  146. return ((Dictionary<WsdlNS.Binding, EnvelopeVersion>)versions)[wsdlBinding];
  147. }
  148. }
  149. return null;
  150. }
  151. static class SoapConverter
  152. {
  153. // [....], this could be simplified if we used generics.
  154. internal static void ConvertExtensions(WsdlNS.ServiceDescriptionFormatExtensionCollection extensions, EnvelopeVersion version, ConvertExtension conversionMethod)
  155. {
  156. bool foundOne = false;
  157. for (int i = extensions.Count - 1; i >= 0; i--)
  158. {
  159. object o = extensions[i];
  160. if (conversionMethod(ref o, version))
  161. {
  162. if (o == null)
  163. extensions.Remove(extensions[i]);
  164. else
  165. extensions[i] = o;
  166. foundOne = true;
  167. }
  168. }
  169. if (!foundOne)
  170. {
  171. object o = null;
  172. conversionMethod(ref o, version);
  173. if (o != null)
  174. extensions.Add(o);
  175. }
  176. }
  177. // This is the delegate implemented by the 4 methods below. It is expected to:
  178. // If given a null reference for src, a new instance of the extension can be set.
  179. internal delegate bool ConvertExtension(ref object src, EnvelopeVersion version);
  180. internal static bool ConvertSoapBinding(ref object src, EnvelopeVersion version)
  181. {
  182. WsdlNS.SoapBinding binding = src as WsdlNS.SoapBinding;
  183. if (src != null)
  184. {
  185. if (binding == null)
  186. return false; // not a soap object
  187. else if (GetBindingVersion<WsdlNS.Soap12Binding>(src) == version)
  188. return true; // matched but same version; no change
  189. }
  190. if (version == EnvelopeVersion.None)
  191. {
  192. src = null;
  193. return true;
  194. }
  195. WsdlNS.SoapBinding dest = version == EnvelopeVersion.Soap12 ? new WsdlNS.Soap12Binding() : new WsdlNS.SoapBinding();
  196. if (binding != null)
  197. {
  198. dest.Required = binding.Required;
  199. dest.Style = binding.Style;
  200. dest.Transport = binding.Transport;
  201. }
  202. src = dest;
  203. return true;
  204. }
  205. internal static bool ConvertSoapAddressBinding(ref object src, EnvelopeVersion version)
  206. {
  207. WsdlNS.SoapAddressBinding binding = src as WsdlNS.SoapAddressBinding;
  208. if (src != null)
  209. {
  210. if (binding == null)
  211. return false; // no match
  212. else if (GetBindingVersion<WsdlNS.Soap12AddressBinding>(src) == version)
  213. return true; // matched but same version; no change
  214. }
  215. if (version == EnvelopeVersion.None)
  216. {
  217. src = null;
  218. return true;
  219. }
  220. WsdlNS.SoapAddressBinding dest = version == EnvelopeVersion.Soap12 ? new WsdlNS.Soap12AddressBinding() : new WsdlNS.SoapAddressBinding();
  221. if (binding != null)
  222. {
  223. dest.Required = binding.Required;
  224. dest.Location = binding.Location;
  225. }
  226. src = dest;
  227. return true;
  228. }
  229. // returns true if src is an expected type; updates src in place; should handle null
  230. internal static bool ConvertSoapOperationBinding(ref object src, EnvelopeVersion version)
  231. {
  232. WsdlNS.SoapOperationBinding binding = src as WsdlNS.SoapOperationBinding;
  233. if (src != null)
  234. {
  235. if (binding == null)
  236. return false; // no match
  237. else if (GetBindingVersion<WsdlNS.Soap12OperationBinding>(src) == version)
  238. return true; // matched but same version
  239. }
  240. if (version == EnvelopeVersion.None)
  241. {
  242. src = null;
  243. return true;
  244. }
  245. WsdlNS.SoapOperationBinding dest = version == EnvelopeVersion.Soap12 ? new WsdlNS.Soap12OperationBinding() : new WsdlNS.SoapOperationBinding();
  246. if (src != null)
  247. {
  248. dest.Required = binding.Required;
  249. dest.Style = binding.Style;
  250. dest.SoapAction = binding.SoapAction;
  251. }
  252. src = dest;
  253. return true;
  254. }
  255. internal static bool ConvertSoapMessageBinding(ref object src, EnvelopeVersion version)
  256. {
  257. WsdlNS.SoapBodyBinding body = src as WsdlNS.SoapBodyBinding;
  258. if (body != null)
  259. {
  260. src = ConvertSoapBodyBinding(body, version);
  261. return true;
  262. }
  263. WsdlNS.SoapHeaderBinding header = src as WsdlNS.SoapHeaderBinding;
  264. if (header != null)
  265. {
  266. src = ConvertSoapHeaderBinding(header, version);
  267. return true;
  268. }
  269. WsdlNS.SoapFaultBinding fault = src as WsdlNS.SoapFaultBinding;
  270. if (fault != null)
  271. {
  272. src = ConvertSoapFaultBinding(fault, version);
  273. return true;
  274. }
  275. XmlElement element = src as XmlElement;
  276. if (element != null)
  277. {
  278. if (IsSoapFaultBinding(element))
  279. {
  280. src = ConvertSoapFaultBinding(element, version);
  281. return true;
  282. }
  283. }
  284. return src == null; // "match" only if nothing passed in
  285. }
  286. static WsdlNS.SoapBodyBinding ConvertSoapBodyBinding(WsdlNS.SoapBodyBinding src, EnvelopeVersion version)
  287. {
  288. if (version == EnvelopeVersion.None)
  289. return null;
  290. EnvelopeVersion srcVersion = GetBindingVersion<WsdlNS.Soap12BodyBinding>(src);
  291. if (srcVersion == version)
  292. return src;
  293. WsdlNS.SoapBodyBinding dest = version == EnvelopeVersion.Soap12 ? new WsdlNS.Soap12BodyBinding() : new WsdlNS.SoapBodyBinding();
  294. if (src != null)
  295. {
  296. if (XmlSerializerOperationFormatter.GetEncoding(srcVersion) == src.Encoding)
  297. dest.Encoding = XmlSerializerOperationFormatter.GetEncoding(version);
  298. dest.Encoding = XmlSerializerOperationFormatter.GetEncoding(version);
  299. dest.Namespace = src.Namespace;
  300. dest.Parts = src.Parts;
  301. dest.PartsString = src.PartsString;
  302. dest.Use = src.Use;
  303. dest.Required = src.Required;
  304. }
  305. return dest;
  306. }
  307. static XmlElement ConvertSoapFaultBinding(XmlElement src, EnvelopeVersion version)
  308. {
  309. if (src == null)
  310. return null;
  311. if (version == EnvelopeVersion.Soap12)
  312. {
  313. if (src.NamespaceURI == WsdlNS.Soap12Binding.Namespace)
  314. return src;
  315. }
  316. else if (version == EnvelopeVersion.Soap11)
  317. {
  318. if (src.NamespaceURI == WsdlNS.SoapBinding.Namespace)
  319. return src;
  320. }
  321. else
  322. {
  323. return null;
  324. }
  325. XmlElement dest = CreateSoapFaultBinding(version);
  326. if (src.HasAttributes)
  327. {
  328. foreach (XmlAttribute attribute in src.Attributes)
  329. {
  330. dest.SetAttribute(attribute.Name, attribute.Value);
  331. }
  332. }
  333. return dest;
  334. }
  335. static WsdlNS.SoapFaultBinding ConvertSoapFaultBinding(WsdlNS.SoapFaultBinding src, EnvelopeVersion version)
  336. {
  337. if (version == EnvelopeVersion.None)
  338. return null;
  339. if (GetBindingVersion<WsdlNS.Soap12FaultBinding>(src) == version)
  340. return src;
  341. WsdlNS.SoapFaultBinding dest = version == EnvelopeVersion.Soap12 ? new WsdlNS.Soap12FaultBinding() : new WsdlNS.SoapFaultBinding();
  342. if (src != null)
  343. {
  344. dest.Encoding = src.Encoding;
  345. dest.Name = src.Name;
  346. dest.Namespace = src.Namespace;
  347. dest.Use = src.Use;
  348. dest.Required = src.Required;
  349. }
  350. return dest;
  351. }
  352. static WsdlNS.SoapHeaderBinding ConvertSoapHeaderBinding(WsdlNS.SoapHeaderBinding src, EnvelopeVersion version)
  353. {
  354. if (version == EnvelopeVersion.None)
  355. return null;
  356. if (GetBindingVersion<WsdlNS.Soap12HeaderBinding>(src) == version)
  357. return src;
  358. WsdlNS.SoapHeaderBinding dest = version == EnvelopeVersion.Soap12 ? new WsdlNS.Soap12HeaderBinding() : new WsdlNS.SoapHeaderBinding();
  359. if (src != null)
  360. {
  361. dest.Fault = src.Fault;
  362. dest.MapToProperty = src.MapToProperty;
  363. dest.Message = src.Message;
  364. dest.Part = src.Part;
  365. dest.Encoding = src.Encoding;
  366. dest.Namespace = src.Namespace;
  367. dest.Use = src.Use;
  368. dest.Required = src.Required;
  369. }
  370. return dest;
  371. }
  372. internal static EnvelopeVersion GetBindingVersion<T12>(object src)
  373. {
  374. return src is T12 ? EnvelopeVersion.Soap12 : EnvelopeVersion.Soap11;
  375. }
  376. }
  377. static WsdlNS.SoapAddressBinding CreateSoapAddressBinding(EnvelopeVersion version, WsdlNS.Port wsdlPort)
  378. {
  379. WsdlNS.SoapAddressBinding soapAddressBinding = null;
  380. if (version == EnvelopeVersion.Soap12)
  381. {
  382. soapAddressBinding = new WsdlNS.Soap12AddressBinding();
  383. }
  384. else if (version == EnvelopeVersion.Soap11)
  385. {
  386. soapAddressBinding = new WsdlNS.SoapAddressBinding();
  387. }
  388. Fx.Assert(soapAddressBinding != null, "EnvelopeVersion is not recognized. Please update the SoapHelper class");
  389. wsdlPort.Extensions.Add(soapAddressBinding);
  390. return soapAddressBinding;
  391. }
  392. //
  393. static WsdlNS.SoapBinding CreateSoapBinding(EnvelopeVersion version, WsdlNS.Binding wsdlBinding)
  394. {
  395. WsdlNS.SoapBinding soapBinding = null;
  396. if (version == EnvelopeVersion.Soap12)
  397. {
  398. soapBinding = new WsdlNS.Soap12Binding();
  399. }
  400. else if (version == EnvelopeVersion.Soap11)
  401. {
  402. soapBinding = new WsdlNS.SoapBinding();
  403. }
  404. Fx.Assert(soapBinding != null, "EnvelopeVersion is not recognized. Please update the SoapHelper class");
  405. wsdlBinding.Extensions.Add(soapBinding);
  406. return soapBinding;
  407. }
  408. static WsdlNS.SoapOperationBinding CreateSoapOperationBinding(EnvelopeVersion version, WsdlNS.OperationBinding wsdlOperationBinding)
  409. {
  410. WsdlNS.SoapOperationBinding soapOperationBinding = null;
  411. if (version == EnvelopeVersion.Soap12)
  412. {
  413. soapOperationBinding = new WsdlNS.Soap12OperationBinding();
  414. }
  415. else if (version == EnvelopeVersion.Soap11)
  416. {
  417. soapOperationBinding = new WsdlNS.SoapOperationBinding();
  418. }
  419. Fx.Assert(soapOperationBinding != null, "EnvelopeVersion is not recognized. Please update the SoapHelper class");
  420. wsdlOperationBinding.Extensions.Add(soapOperationBinding);
  421. return soapOperationBinding;
  422. }
  423. static WsdlNS.SoapBodyBinding CreateSoapBodyBinding(EnvelopeVersion version, WsdlNS.MessageBinding wsdlMessageBinding)
  424. {
  425. WsdlNS.SoapBodyBinding soapBodyBinding = null;
  426. if (version == EnvelopeVersion.Soap12)
  427. {
  428. soapBodyBinding = new WsdlNS.Soap12BodyBinding();
  429. }
  430. else if (version == EnvelopeVersion.Soap11)
  431. {
  432. soapBodyBinding = new WsdlNS.SoapBodyBinding();
  433. }
  434. Fx.Assert(soapBodyBinding != null, "EnvelopeVersion is not recognized. Please update the SoapHelper class");
  435. wsdlMessageBinding.Extensions.Add(soapBodyBinding);
  436. return soapBodyBinding;
  437. }
  438. static WsdlNS.SoapHeaderBinding CreateSoapHeaderBinding(EnvelopeVersion version, WsdlNS.MessageBinding wsdlMessageBinding)
  439. {
  440. WsdlNS.SoapHeaderBinding soapHeaderBinding = null;
  441. if (version == EnvelopeVersion.Soap12)
  442. {
  443. soapHeaderBinding = new WsdlNS.Soap12HeaderBinding();
  444. }
  445. else if (version == EnvelopeVersion.Soap11)
  446. {
  447. soapHeaderBinding = new WsdlNS.SoapHeaderBinding();
  448. }
  449. Fx.Assert(soapHeaderBinding != null, "EnvelopeVersion is not recognized. Please update the SoapHelper class");
  450. wsdlMessageBinding.Extensions.Add(soapHeaderBinding);
  451. return soapHeaderBinding;
  452. }
  453. static XmlElement CreateSoapFaultBinding(EnvelopeVersion version)
  454. {
  455. string prefix = null;
  456. string ns = null;
  457. if (version == EnvelopeVersion.Soap12)
  458. {
  459. ns = WsdlNS.Soap12Binding.Namespace;
  460. prefix = "soap12";
  461. }
  462. else if (version == EnvelopeVersion.Soap11)
  463. {
  464. ns = WsdlNS.SoapBinding.Namespace;
  465. prefix = "soap";
  466. }
  467. Fx.Assert(ns != null, "EnvelopeVersion is not recognized. Please update the SoapHelper class");
  468. return Document.CreateElement(prefix, "fault", ns);
  469. }
  470. internal static WsdlNS.SoapAddressBinding GetSoapAddressBinding(WsdlNS.Port wsdlPort)
  471. {
  472. foreach (object o in wsdlPort.Extensions)
  473. {
  474. if (o is WsdlNS.SoapAddressBinding)
  475. return (WsdlNS.SoapAddressBinding)o;
  476. }
  477. return null;
  478. }
  479. static WsdlNS.SoapBinding GetSoapBinding(WsdlEndpointConversionContext endpointContext)
  480. {
  481. foreach (object o in endpointContext.WsdlBinding.Extensions)
  482. {
  483. if (o is WsdlNS.SoapBinding)
  484. return (WsdlNS.SoapBinding)o;
  485. }
  486. return null;
  487. }
  488. static WsdlNS.SoapOperationBinding GetSoapOperationBinding(WsdlEndpointConversionContext endpointContext, OperationDescription operation)
  489. {
  490. WsdlNS.OperationBinding wsdlOperationBinding = endpointContext.GetOperationBinding(operation);
  491. foreach (object o in wsdlOperationBinding.Extensions)
  492. {
  493. if (o is WsdlNS.SoapOperationBinding)
  494. return (WsdlNS.SoapOperationBinding)o;
  495. }
  496. return null;
  497. }
  498. static WsdlNS.SoapBodyBinding GetSoapBodyBinding(WsdlEndpointConversionContext endpointContext, WsdlNS.MessageBinding wsdlMessageBinding)
  499. {
  500. foreach (object o in wsdlMessageBinding.Extensions)
  501. {
  502. if (o is WsdlNS.SoapBodyBinding)
  503. return (WsdlNS.SoapBodyBinding)o;
  504. }
  505. return null;
  506. }
  507. internal static string ReadSoapAction(WsdlNS.OperationBinding wsdlOperationBinding)
  508. {
  509. WsdlNS.SoapOperationBinding soapOperationBinding = (WsdlNS.SoapOperationBinding)wsdlOperationBinding.Extensions.Find(typeof(WsdlNS.SoapOperationBinding));
  510. return soapOperationBinding != null ? soapOperationBinding.SoapAction : null;
  511. }
  512. internal static WsdlNS.SoapBindingStyle GetStyle(WsdlNS.Binding binding)
  513. {
  514. WsdlNS.SoapBindingStyle style = WsdlNS.SoapBindingStyle.Default;
  515. if (binding != null)
  516. {
  517. WsdlNS.SoapBinding soapBinding = binding.Extensions.Find(typeof(WsdlNS.SoapBinding)) as WsdlNS.SoapBinding;
  518. if (soapBinding != null)
  519. style = soapBinding.Style;
  520. }
  521. return style;
  522. }
  523. internal static WsdlNS.SoapBindingStyle GetStyle(WsdlNS.OperationBinding operationBinding, WsdlNS.SoapBindingStyle defaultBindingStyle)
  524. {
  525. WsdlNS.SoapBindingStyle style = defaultBindingStyle;
  526. if (operationBinding != null)
  527. {
  528. WsdlNS.SoapOperationBinding soapOperationBinding = operationBinding.Extensions.Find(typeof(WsdlNS.SoapOperationBinding)) as WsdlNS.SoapOperationBinding;
  529. if (soapOperationBinding != null)
  530. {
  531. if (soapOperationBinding.Style != WsdlNS.SoapBindingStyle.Default)
  532. style = soapOperationBinding.Style;
  533. }
  534. }
  535. return style;
  536. }
  537. internal static bool IsSoapFaultBinding(XmlElement element)
  538. {
  539. return (element != null && element.LocalName == "fault" && (element.NamespaceURI == WsdlNS.Soap12Binding.Namespace || element.NamespaceURI == WsdlNS.SoapBinding.Namespace));
  540. }
  541. internal static bool IsEncoded(XmlElement element)
  542. {
  543. Fx.Assert(element != null, "");
  544. XmlAttribute attribute = element.GetAttributeNode("use");
  545. if (attribute == null)
  546. return false;
  547. return attribute.Value == "encoded";
  548. }
  549. }
  550. }