MetadataResolver.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // MetadataResolver.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. // Ankit Jain <[email protected]>
  7. //
  8. // Copyright (C) 2005 Novell, Inc. http://www.novell.com
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Collections.ObjectModel;
  32. using System.ServiceModel;
  33. using System.ServiceModel.Channels;
  34. using System.Web.Services.Description;
  35. using System.Web.Services.Discovery;
  36. using System.Web.Services.Protocols;
  37. using System.Xml;
  38. using System.Xml.Serialization;
  39. using System.Net;
  40. using System.IO;
  41. using System.Text;
  42. using QName = System.Xml.XmlQualifiedName;
  43. namespace System.ServiceModel.Description
  44. {
  45. [MonoTODO]
  46. public static class MetadataResolver
  47. {
  48. public static ServiceEndpointCollection Resolve (
  49. Type contract,
  50. EndpointAddress address)
  51. {
  52. if (contract == null)
  53. throw new ArgumentNullException ("contract");
  54. return ResolveContracts (
  55. new ContractDescription [] {ContractDescription.GetContract (contract)},
  56. address,
  57. MetadataExchangeClientMode.MetadataExchange);
  58. }
  59. public static ServiceEndpointCollection Resolve (
  60. Type contract,
  61. Uri address,
  62. MetadataExchangeClientMode mode)
  63. {
  64. return ResolveContracts (
  65. new ContractDescription [] {ContractDescription.GetContract (contract)},
  66. address,
  67. mode);
  68. }
  69. public static ServiceEndpointCollection Resolve (
  70. IEnumerable<ContractDescription> contracts,
  71. EndpointAddress address)
  72. {
  73. return ResolveContracts (contracts, address, MetadataExchangeClientMode.MetadataExchange);
  74. }
  75. public static ServiceEndpointCollection Resolve (
  76. IEnumerable<ContractDescription> contracts,
  77. Uri address,
  78. MetadataExchangeClientMode mode)
  79. {
  80. return ResolveContracts (contracts, address, mode);
  81. }
  82. public static ServiceEndpointCollection Resolve (
  83. IEnumerable<ContractDescription> contracts,
  84. EndpointAddress address,
  85. MetadataExchangeClient client)
  86. {
  87. if (client == null)
  88. throw new ArgumentNullException ("client");
  89. /* FIXME: client is used for what? */
  90. return ResolveContracts (contracts, address, MetadataExchangeClientMode.MetadataExchange);
  91. }
  92. /* FIXME: What is the mode/client used for here?
  93. * According to the tests, address is used */
  94. public static ServiceEndpointCollection Resolve (
  95. IEnumerable<ContractDescription> contracts,
  96. Uri address,
  97. MetadataExchangeClientMode mode,
  98. MetadataExchangeClient client)
  99. {
  100. if (client == null)
  101. throw new ArgumentNullException ("client");
  102. return ResolveContracts (contracts, address, mode);
  103. }
  104. private static ServiceEndpointCollection ResolveContracts (
  105. IEnumerable<ContractDescription> contracts,
  106. Uri address,
  107. MetadataExchangeClientMode mode)
  108. {
  109. if (address == null)
  110. throw new ArgumentNullException ("address");
  111. return ResolveContracts (contracts, new EndpointAddress (address), mode);
  112. }
  113. private static ServiceEndpointCollection ResolveContracts (
  114. IEnumerable<ContractDescription> contracts,
  115. EndpointAddress address,
  116. MetadataExchangeClientMode mode)
  117. {
  118. if (contracts == null)
  119. throw new ArgumentNullException ("contracts");
  120. List<ContractDescription> list = new List<ContractDescription> (contracts);
  121. if (list.Count == 0)
  122. throw new ArgumentException ("There must be atleast one ContractDescription", "contracts");
  123. if (address == null)
  124. throw new ArgumentNullException ("address");
  125. MetadataExchangeClient client = new MetadataExchangeClient (address);
  126. MetadataSet metadata = client.GetMetadata ();
  127. WsdlImporter importer = new WsdlImporter (metadata);
  128. ServiceEndpointCollection endpoints = importer.ImportAllEndpoints ();
  129. ServiceEndpointCollection ret = new ServiceEndpointCollection ();
  130. foreach (ContractDescription contract in list) {
  131. Collection<ServiceEndpoint> colln =
  132. endpoints.FindAll (new QName (contract.Name, contract.Namespace));
  133. for (int i = 0; i < colln.Count; i ++)
  134. ret.Add (colln [i]);
  135. }
  136. return ret;
  137. }
  138. }
  139. }