MetadataExchangeClient.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // MetadataExchangeClient.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.ServiceModel.Description;
  35. using System.Web.Services.Description;
  36. using System.Web.Services.Discovery;
  37. using System.Web.Services.Protocols;
  38. using System.Xml;
  39. using System.Xml.Serialization;
  40. using System.IO;
  41. using System.Net;
  42. using System.Text;
  43. using SMBinding = System.ServiceModel.Channels.Binding;
  44. using SMMessage = System.ServiceModel.Channels.Message;
  45. namespace System.ServiceModel.Description
  46. {
  47. public class MetadataExchangeClient
  48. {
  49. string scheme;
  50. EndpointAddress address;
  51. SMBinding binding;
  52. MetadataExchangeClientMode mode = MetadataExchangeClientMode.MetadataExchange;
  53. // constructors
  54. [MonoTODO ("use empty configuration")]
  55. public MetadataExchangeClient ()
  56. {
  57. }
  58. public MetadataExchangeClient (SMBinding mexBinding)
  59. {
  60. binding = mexBinding;
  61. }
  62. public MetadataExchangeClient (EndpointAddress address)
  63. {
  64. this.address = address;
  65. }
  66. public MetadataExchangeClient (string endpointConfigurationName)
  67. {
  68. throw new NotImplementedException ();
  69. }
  70. [MonoTODO ("MetadataExchangeClientMode is not considered")]
  71. public MetadataExchangeClient (Uri address, MetadataExchangeClientMode mode)
  72. {
  73. this.address = new EndpointAddress (address.AbsoluteUri);
  74. this.mode = mode;
  75. }
  76. [MonoTODO]
  77. public ICredentials HttpCredentials { get; set; }
  78. [MonoTODO]
  79. public int MaximumResolvedReferences { get; set; }
  80. public TimeSpan OperationTimeout { get; set; }
  81. [MonoTODO]
  82. public bool ResolveMetadataReferences { get; set; }
  83. public ClientCredentials SoapCredentials { get; set; }
  84. [MonoTODO ("use dialect and identifier (but how?)")]
  85. protected internal virtual ChannelFactory<IMetadataExchange> GetChannelFactory (EndpointAddress metadataAddress, string dialect, string identifier)
  86. {
  87. if (metadataAddress == null)
  88. throw new ArgumentNullException ("metadataAddress");
  89. var se = new ServiceEndpoint (ContractDescription.GetContract (typeof (IMetadataExchange)), CreateBinding (metadataAddress), metadataAddress);
  90. if (SoapCredentials != null) {
  91. se.Behaviors.RemoveAll<ClientCredentials> ();
  92. se.Behaviors.Add (SoapCredentials);
  93. }
  94. return new ChannelFactory<IMetadataExchange> (se);
  95. }
  96. [MonoTODO]
  97. protected internal virtual HttpWebRequest GetWebRequest (Uri location, string dialect, string identifier)
  98. {
  99. throw new NotImplementedException ();
  100. }
  101. SMBinding CreateBinding (EndpointAddress address)
  102. {
  103. return address.Uri.Scheme == Uri.UriSchemeHttps ?
  104. MetadataExchangeBindings.CreateMexHttpsBinding () :
  105. MetadataExchangeBindings.CreateMexHttpBinding ();
  106. }
  107. // sync methods
  108. public MetadataSet GetMetadata ()
  109. {
  110. return GetMetadata (address);
  111. }
  112. public MetadataSet GetMetadata (EndpointAddress address)
  113. {
  114. //FIXME: default mode?
  115. return GetMetadataInternal (address, mode);
  116. }
  117. public MetadataSet GetMetadata (Uri address, MetadataExchangeClientMode mode)
  118. {
  119. return GetMetadataInternal (new EndpointAddress (address.AbsoluteUri), mode);
  120. }
  121. internal MetadataSet GetMetadataInternal (EndpointAddress address, MetadataExchangeClientMode mode)
  122. {
  123. // FIXME: give dialect and identifier
  124. var cf = GetChannelFactory (address, null, null);
  125. cf.Open ();
  126. var proxy = cf.CreateChannel ();
  127. var asClientChannel = proxy as IClientChannel;
  128. if (asClientChannel == null)
  129. throw new InvalidOperationException ("The channel factory must return an IClientChannel implementation");
  130. asClientChannel.OperationTimeout = OperationTimeout;
  131. asClientChannel.Open ();
  132. SMMessage msg = SMMessage.CreateMessage (
  133. MessageVersion.Soap12WSAddressing10,
  134. "http://schemas.xmlsoap.org/ws/2004/09/transfer/Get");
  135. msg.Headers.ReplyTo = new EndpointAddress (
  136. "http://www.w3.org/2005/08/addressing/anonymous");
  137. //msg.Headers.From = new EndpointAddress ("http://localhost");
  138. msg.Headers.To = address.Uri;
  139. msg.Headers.MessageId = new UniqueId ();
  140. SMMessage ret;
  141. try {
  142. ret = proxy.Get (msg);
  143. } catch (Exception e) {
  144. throw new InvalidOperationException (
  145. "Metadata contains a reference that cannot be resolved : " + address.Uri.AbsoluteUri, e);
  146. }
  147. return MetadataSet.ReadFrom (ret.GetReaderAtBodyContents ());
  148. }
  149. // async methods
  150. Func<Func<MetadataSet>,MetadataSet> getter;
  151. void PrepareGetter ()
  152. {
  153. if (getter == null)
  154. getter = new Func<Func<MetadataSet>,MetadataSet> (GetMetadata);
  155. }
  156. public MetadataSet EndGetMetadata (IAsyncResult result)
  157. {
  158. return getter.EndInvoke (result);
  159. }
  160. MetadataSet GetMetadata (Func<MetadataSet> func)
  161. {
  162. return func ();
  163. }
  164. public IAsyncResult BeginGetMetadata (AsyncCallback callback, object asyncState)
  165. {
  166. PrepareGetter ();
  167. return getter.BeginInvoke (() => GetMetadata (), callback, asyncState);
  168. }
  169. public IAsyncResult BeginGetMetadata (EndpointAddress address, AsyncCallback callback, object asyncState)
  170. {
  171. PrepareGetter ();
  172. return getter.BeginInvoke (() => GetMetadata (address), callback, asyncState);
  173. }
  174. public IAsyncResult BeginGetMetadata (Uri address, MetadataExchangeClientMode mode, AsyncCallback callback, object asyncState)
  175. {
  176. PrepareGetter ();
  177. return getter.BeginInvoke (() => GetMetadata (address, mode), callback, asyncState);
  178. }
  179. }
  180. interface IMetadataExchangeClient : IMetadataExchange, IClientChannel
  181. {
  182. }
  183. }