MetadataResolver.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. static IEnumerable<ContractDescription> ToContracts (Type contract)
  49. {
  50. if (contract == null)
  51. throw new ArgumentNullException ("contract");
  52. yield return ContractDescription.GetContract (contract);
  53. }
  54. public static IAsyncResult BeginResolve (Type contract, EndpointAddress address, AsyncCallback callback, object asyncState)
  55. {
  56. return BeginResolve (ToContracts (contract), address, callback, asyncState);
  57. }
  58. public static IAsyncResult BeginResolve (IEnumerable<ContractDescription> contracts, EndpointAddress address, AsyncCallback callback, object asyncState)
  59. {
  60. return BeginResolve (contracts, address, MetadataExchangeClientMode.MetadataExchange, callback, asyncState);
  61. }
  62. public static IAsyncResult BeginResolve (Type contract, Uri address, MetadataExchangeClientMode mode, AsyncCallback callback, object asyncState)
  63. {
  64. return BeginResolve (ToContracts (contract), new EndpointAddress (address), mode, callback, asyncState);
  65. }
  66. public static IAsyncResult BeginResolve (IEnumerable<ContractDescription> contracts, EndpointAddress address, MetadataExchangeClientMode mode, AsyncCallback callback, object asyncState)
  67. {
  68. return BeginResolve (contracts, address, mode, new MetadataExchangeClient (), callback, asyncState);
  69. }
  70. public static IAsyncResult BeginResolve (Type contract, EndpointAddress address, MetadataExchangeClient client, AsyncCallback callback, object asyncState)
  71. {
  72. return resolver.BeginInvoke (ToContracts (contract), () => client.GetMetadata (address), callback, asyncState);
  73. }
  74. public static IAsyncResult BeginResolve (IEnumerable<ContractDescription> contracts, EndpointAddress address, MetadataExchangeClientMode mode, MetadataExchangeClient client, AsyncCallback callback, object asyncState)
  75. {
  76. return resolver.BeginInvoke (contracts, () => client.GetMetadataInternal (address, mode), callback, asyncState);
  77. }
  78. delegate ServiceEndpointCollection Resolver (IEnumerable<ContractDescription> contracts, Func<MetadataSet> metadataGetter);
  79. static readonly Resolver resolver = new Resolver (ResolveContracts);
  80. public static ServiceEndpointCollection EndResolve (IAsyncResult result)
  81. {
  82. return resolver.EndInvoke (result);
  83. }
  84. public static ServiceEndpointCollection Resolve (
  85. Type contract,
  86. EndpointAddress address)
  87. {
  88. return Resolve (ToContracts (contract), address);
  89. }
  90. public static ServiceEndpointCollection Resolve (
  91. Type contract,
  92. Uri address,
  93. MetadataExchangeClientMode mode)
  94. {
  95. return Resolve (ToContracts (contract), address, mode);
  96. }
  97. public static ServiceEndpointCollection Resolve (
  98. IEnumerable<ContractDescription> contracts,
  99. EndpointAddress address)
  100. {
  101. return Resolve (contracts, address, new MetadataExchangeClient ());
  102. }
  103. public static ServiceEndpointCollection Resolve (
  104. IEnumerable<ContractDescription> contracts,
  105. Uri address,
  106. MetadataExchangeClientMode mode)
  107. {
  108. return Resolve (contracts, new EndpointAddress (address), new MetadataExchangeClient (address, mode));
  109. }
  110. public static ServiceEndpointCollection Resolve (
  111. IEnumerable<ContractDescription> contracts,
  112. EndpointAddress address,
  113. MetadataExchangeClient client)
  114. {
  115. if (client == null)
  116. throw new ArgumentNullException ("client");
  117. return ResolveContracts (contracts, () => client.GetMetadata (address));
  118. }
  119. /* FIXME: What is the mode/client used for here?
  120. * According to the tests, address is used */
  121. public static ServiceEndpointCollection Resolve (
  122. IEnumerable<ContractDescription> contracts,
  123. Uri address,
  124. MetadataExchangeClientMode mode,
  125. MetadataExchangeClient client)
  126. {
  127. if (client == null)
  128. throw new ArgumentNullException ("client");
  129. return ResolveContracts (contracts, () => new MetadataExchangeClient ().GetMetadata (address, mode));
  130. }
  131. private static ServiceEndpointCollection ResolveContracts (
  132. IEnumerable<ContractDescription> contracts,
  133. Func<MetadataSet> metadataGetter)
  134. {
  135. if (contracts == null)
  136. throw new ArgumentNullException ("contracts");
  137. List<ContractDescription> list = new List<ContractDescription> (contracts);
  138. if (list.Count == 0)
  139. throw new ArgumentException ("There must be atleast one ContractDescription", "contracts");
  140. MetadataSet metadata = metadataGetter ();
  141. WsdlImporter importer = new WsdlImporter (metadata);
  142. ServiceEndpointCollection endpoints = importer.ImportAllEndpoints ();
  143. ServiceEndpointCollection ret = new ServiceEndpointCollection ();
  144. foreach (ContractDescription contract in list) {
  145. Collection<ServiceEndpoint> colln =
  146. endpoints.FindAll (new QName (contract.Name, contract.Namespace));
  147. for (int i = 0; i < colln.Count; i ++)
  148. ret.Add (colln [i]);
  149. }
  150. return ret;
  151. }
  152. }
  153. }