2 // ServiceMetadataExtension.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
6 // Ankit Jain <jankit@novell.com>
7 // Gonzalo Paniagua Javier (gonzalo@novell.com)
9 // Copyright (C) 2005 Novell, Inc. http://www.novell.com
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.Collections.ObjectModel;
34 using System.Collections.Specialized;
37 using System.ServiceModel.Channels;
38 using System.ServiceModel.Dispatcher;
40 using System.Web.Services;
41 using System.Web.Services.Description;
43 using System.Xml.Schema;
45 using WSServiceDescription = System.Web.Services.Description.ServiceDescription;
46 using WSMessage = System.Web.Services.Description.Message;
47 using SMMessage = System.ServiceModel.Channels.Message;
48 using WCFBinding = System.ServiceModel.Channels.Binding;
50 namespace System.ServiceModel.Description
52 public class ServiceMetadataExtension : IExtension<ServiceHostBase>
54 const string ServiceMetadataBehaviorHttpGetBinding = "ServiceMetadataBehaviorHttpGetBinding";
57 ServiceHostBase owner;
58 Dictionary<Uri, ChannelDispatcher> dispatchers;
61 public ServiceMetadataExtension ()
66 public MetadataSet Metadata {
68 if (metadata == null) {
69 var exporter = new WsdlExporter ();
70 exporter.ExportEndpoints (owner.Description.Endpoints, new XmlQualifiedName (owner.Description.Name, owner.Description.Namespace));
71 metadata = exporter.GetGeneratedMetadata ();
77 internal ServiceHostBase Owner {
81 internal static ServiceMetadataExtension EnsureServiceMetadataExtension (ServiceHostBase serviceHostBase) {
82 ServiceMetadataExtension sme = serviceHostBase.Extensions.Find<ServiceMetadataExtension> ();
84 sme = new ServiceMetadataExtension ();
85 serviceHostBase.Extensions.Add (sme);
90 // FIXME: distinguish HTTP and HTTPS in the Url properties.
91 // FIXME: reject such ServiceDescription that has no HTTP(S) binding.
92 // FIXME: it should not use the binding that is used in the ServiceEndpoint. For example, WSDL results have to be given as text, not binary.
93 // FIXME: if the ServiceDescription has a base address (e.g. http://localhost:8080) and HttpGetUrl is empty, it returns UnknownDestination while it is expected to return the HTTP help page.
94 internal void EnsureChannelDispatcher (bool isMex, string scheme, Uri uri, WCFBinding binding)
97 instance.WsdlUrl = uri;
99 instance.HelpUrl = uri;
101 if (dispatchers == null)
102 dispatchers = new Dictionary<Uri, ChannelDispatcher> ();
103 else if (dispatchers.ContainsKey (uri)) {
104 var info = dispatchers [uri].Listener.GetProperty<MetadataPublishingInfo> ();
106 info.SupportsMex = true;
108 info.SupportsHelp = true;
112 if (binding == null) {
115 binding = MetadataExchangeBindings.CreateMexHttpBinding ();
118 binding = MetadataExchangeBindings.CreateMexHttpsBinding ();
121 binding = MetadataExchangeBindings.CreateMexTcpBinding ();
124 binding = MetadataExchangeBindings.CreateMexNamedPipeBinding ();
129 CustomBinding cb = new CustomBinding (binding) { Name = ServiceMetadataBehaviorHttpGetBinding };
130 cb.Elements.Find<MessageEncodingBindingElement> ().MessageVersion = MessageVersion.None;
132 ServiceEndpoint se = new ServiceEndpoint (ContractDescription.GetContract (typeof (IHttpGetHelpPageAndMetadataContract)), cb, new EndpointAddress (uri))
137 var channelDispatcher = new DispatcherBuilder (Owner).BuildChannelDispatcher (owner.Description.ServiceType, se, new BindingParameterCollection ());
138 channelDispatcher.MessageVersion = MessageVersion.None; // it has no MessageVersion.
139 // add HttpGetWsdl to indicate that the ChannelDispatcher is for mex or help.
140 var listener = channelDispatcher.Listener as ChannelListenerBase;
141 if (listener != null)
142 listener.Properties.Add (new MetadataPublishingInfo () { SupportsMex = isMex, SupportsHelp = !isMex, Instance = instance });
144 throw new InvalidOperationException ("FIXME: attempt to use ServiceMetadataExtension to not-supported channel listener: " + listener.GetType ());
145 channelDispatcher.IsMex = true;
146 channelDispatcher.Endpoints [0].DispatchRuntime.InstanceContextProvider = new SingletonInstanceContextProvider (new InstanceContext (owner, instance));
148 dispatchers.Add (uri, channelDispatcher);
149 owner.ChannelDispatchers.Add (channelDispatcher);
152 void IExtension<ServiceHostBase>.Attach (ServiceHostBase owner)
155 instance = new HttpGetWsdl (this);
158 void IExtension<ServiceHostBase>.Detach (ServiceHostBase owner)
164 [ServiceContract (Namespace = "http://schemas.microsoft.com/2006/04/http/metadata")]
165 interface IHttpGetHelpPageAndMetadataContract
167 [OperationContract (Action = "*", ReplyAction = "*")]
168 SMMessage Get (SMMessage req);
171 // It is used to identify which page to serve when a channel dispatcher
172 // has a listener to an relatively empty URI (conflicting with the
173 // target service endpoint)
175 // Can't be enum as it is for GetProperty<T> ().
176 internal class MetadataPublishingInfo
178 public bool SupportsMex { get; set; }
179 public bool SupportsHelp { get; set; }
180 public HttpGetWsdl Instance { get; set; }
183 class HttpGetWsdl : IHttpGetHelpPageAndMetadataContract
185 ServiceMetadataExtension ext;
188 Dictionary <string,WSServiceDescription> wsdl_documents =
189 new Dictionary<string, WSServiceDescription> ();
190 Dictionary <string, XmlSchema> schemas =
191 new Dictionary<string, XmlSchema> ();
193 public HttpGetWsdl (ServiceMetadataExtension ext)
198 public Uri HelpUrl { get; set; }
199 public Uri WsdlUrl { get; set; }
201 void EnsureMetadata ()
209 public SMMessage Get (SMMessage req)
213 HttpRequestMessageProperty prop = (HttpRequestMessageProperty) req.Properties [HttpRequestMessageProperty.Name];
215 NameValueCollection query_string = CreateQueryString (prop.QueryString);
216 if (query_string == null || query_string.AllKeys.Length != 1) {
217 if (HelpUrl != null && Uri.Compare (req.Headers.To, HelpUrl, UriComponents.HttpRequestUrl ^ UriComponents.Query, UriFormat.UriEscaped, StringComparison.Ordinal) == 0)
218 return CreateHelpPage (req);
219 WSServiceDescription w = GetWsdl ("wsdl");
221 return CreateWsdlMessage (w);
224 if (String.Compare (query_string [null], "wsdl", StringComparison.OrdinalIgnoreCase) == 0) {
225 WSServiceDescription wsdl = GetWsdl ("wsdl");
227 return CreateWsdlMessage (wsdl);
228 } else if (query_string ["wsdl"] != null) {
229 WSServiceDescription wsdl = GetWsdl (query_string ["wsdl"]);
231 return CreateWsdlMessage (wsdl);
232 } else if (query_string ["xsd"] != null) {
233 XmlSchema schema = GetXmlSchema (query_string ["xsd"]);
234 if (schema != null) {
235 //FIXME: Is this the correct way?
236 MemoryStream ms = new MemoryStream ();
239 ms.Seek (0, SeekOrigin.Begin);
240 SMMessage ret = SMMessage.CreateMessage (MessageVersion.None, "", XmlReader.Create (ms));
246 return CreateHelpPage (req);
249 /* Code from HttpListenerRequest */
250 NameValueCollection CreateQueryString (string query)
252 NameValueCollection query_string = new NameValueCollection ();
253 if (query == null || query.Length == 0)
256 string [] components = query.Split ('&');
257 foreach (string kv in components) {
258 int pos = kv.IndexOf ('=');
260 query_string.Add (null, HttpUtility.UrlDecode (kv));
262 string key = HttpUtility.UrlDecode (kv.Substring (0, pos));
263 string val = HttpUtility.UrlDecode (kv.Substring (pos + 1));
265 query_string.Add (key, val);
272 // It is returned for ServiceDebugBehavior.Http(s)HelpPageUrl.
273 // They may be empty, and for such case the help page URL is
274 // simply the service endpoint URL (foobar.svc).
276 // Note that if there is also ServiceMetadataBehavior that
277 // lacks Http(s)GetUrl, then it is also mapped to the same
278 // URL, but it requires "?wsdl" parameter and .NET somehow
279 // differentiates those requests.
281 // If both Http(s)HelpPageUrl and Http(s)GetUrl exist, then
282 // requests to the service endpoint URL (foobar.svc) results
283 // in an xml output with empty string (non-WF XML error).
285 SMMessage CreateHelpPage (SMMessage request)
287 var helpBody = ext.Owner.Description.Behaviors.Find<ServiceMetadataBehavior> () != null ?
289 <p>To create client proxy source, run:</p>
290 <p><code>svcutil <a href='{0}'>{0}</a></code></p>
291 <!-- FIXME: add client proxy usage (that required decent ServiceContractGenerator implementation, so I leave it yet.) -->
292 ", new Uri (WsdlUrl.ToString () + "?wsdl")) : // this Uri.ctor() is nasty, but there is no other way to add "?wsdl" (!!)
294 <p>Service metadata publishing for {0} is not enabled. Service administrators can enable it by adding <serviceMetadata> element in the host configuration (web.config in ASP.NET), or ServiceMetadataBehavior object to the Behaviors collection of the service host's ServiceDescription.</p>", ext.Owner.Description.Name);
296 var html = String.Format (@"
299 <title>Service {0}</title>
304 </html>", ext.Owner.Description.Name, helpBody);
306 var m = SMMessage.CreateMessage (MessageVersion.None, "", XmlReader.Create (new StringReader (html)));
307 var rp = new HttpResponseMessageProperty ();
308 rp.Headers ["Content-Type"] = "text/html";
309 m.Properties.Add (HttpResponseMessageProperty.Name, rp);
313 SMMessage CreateWsdlMessage (WSServiceDescription wsdl)
315 MemoryStream ms = new MemoryStream ();
316 XmlWriter xw = XmlWriter.Create (ms);
318 WSServiceDescription.Serializer.Serialize (xw, wsdl);
319 ms.Seek (0, SeekOrigin.Begin);
320 return SMMessage.CreateMessage (MessageVersion.None, "", XmlReader.Create (ms));
328 MetadataSet metadata = ext.Metadata;
329 int xs_i = 0, wsdl_i = 0;
331 //Dictionary keyed by namespace
332 StringDictionary wsdl_strings = new StringDictionary ();
333 StringDictionary xsd_strings = new StringDictionary ();
335 foreach (MetadataSection section in metadata.MetadataSections) {
338 XmlSchema xs = section.Metadata as XmlSchema;
340 key = String.Format ("xsd{0}", xs_i ++);
342 xsd_strings [xs.TargetNamespace] = key;
346 WSServiceDescription wsdl = section.Metadata as WSServiceDescription;
350 //if (wsdl.TargetNamespace == "http://tempuri.org/")
351 if (wsdl.Services.Count > 0)
354 key = String.Format ("wsdl{0}", wsdl_i ++);
356 wsdl_documents [key] = wsdl;
357 wsdl_strings [wsdl.TargetNamespace] = key;
360 string base_url = WsdlUrl.ToString ();
361 foreach (WSServiceDescription wsdl in wsdl_documents.Values) {
362 foreach (Import import in wsdl.Imports) {
363 if (!String.IsNullOrEmpty (import.Location))
366 import.Location = String.Format ("{0}?wsdl={1}", base_url, wsdl_strings [import.Namespace]);
369 foreach (XmlSchema schema in wsdl.Types.Schemas) {
370 foreach (XmlSchemaObject obj in schema.Includes) {
371 XmlSchemaImport imp = obj as XmlSchemaImport;
372 if (imp == null || imp.SchemaLocation != null)
375 imp.SchemaLocation = String.Format ("{0}?xsd={1}", base_url, xsd_strings [imp.Namespace]);
382 WSServiceDescription GetWsdl (string which)
385 WSServiceDescription wsdl;
386 wsdl_documents.TryGetValue (which, out wsdl);
390 XmlSchema GetXmlSchema (string which)
394 schemas.TryGetValue (which, out schema);