1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
//
// ServiceMetadataExtension.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
// Ankit Jain <jankit@novell.com>
// Gonzalo Paniagua Javier (gonzalo@novell.com)
//
// Copyright (C) 2005 Novell, Inc. http://www.novell.com
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.Web;
using System.Web.Services;
using System.Web.Services.Description;
using System.Xml;
using System.Xml.Schema;
using WSServiceDescription = System.Web.Services.Description.ServiceDescription;
using WSMessage = System.Web.Services.Description.Message;
using SMMessage = System.ServiceModel.Channels.Message;
using WCFBinding = System.ServiceModel.Channels.Binding;
namespace System.ServiceModel.Description
{
public class ServiceMetadataExtension : IExtension<ServiceHostBase>
{
const string ServiceMetadataBehaviorHttpGetBinding = "ServiceMetadataBehaviorHttpGetBinding";
MetadataSet metadata;
ServiceHostBase owner;
Dictionary<Uri, ChannelDispatcher> dispatchers;
HttpGetWsdl instance;
public ServiceMetadataExtension ()
{
}
internal HttpGetWsdl Instance {
get { return instance; }
}
[MonoTODO]
public MetadataSet Metadata {
get {
if (metadata == null) {
var exporter = new WsdlExporter ();
exporter.ExportEndpoints (owner.Description.Endpoints, new XmlQualifiedName (owner.Description.Name, owner.Description.Namespace));
metadata = exporter.GetGeneratedMetadata ();
}
return metadata;
}
}
internal ServiceHostBase Owner {
get { return owner; }
}
internal static ServiceMetadataExtension EnsureServiceMetadataExtension (ServiceHostBase serviceHostBase) {
ServiceMetadataExtension sme = serviceHostBase.Extensions.Find<ServiceMetadataExtension> ();
if (sme == null) {
sme = new ServiceMetadataExtension ();
serviceHostBase.Extensions.Add (sme);
}
return sme;
}
// 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.
internal void EnsureChannelDispatcher (bool isMex, string scheme, Uri uri, WCFBinding binding)
{
if (isMex)
instance.WsdlUrl = uri;
else
instance.HelpUrl = uri;
if (dispatchers == null)
dispatchers = new Dictionary<Uri, ChannelDispatcher> ();
else if (dispatchers.ContainsKey (uri))
return; // already exists (e.g. reached here for wsdl while help is already filled on the same URI.)
if (binding == null) {
switch (scheme) {
case "http":
binding = MetadataExchangeBindings.CreateMexHttpBinding ();
break;
case "https":
binding = MetadataExchangeBindings.CreateMexHttpsBinding ();
break;
case "net.tcp":
binding = MetadataExchangeBindings.CreateMexTcpBinding ();
break;
case "net.pipe":
binding = MetadataExchangeBindings.CreateMexNamedPipeBinding ();
break;
}
}
CustomBinding cb = new CustomBinding (binding) { Name = ServiceMetadataBehaviorHttpGetBinding };
cb.Elements.Find<MessageEncodingBindingElement> ().MessageVersion = MessageVersion.None;
ServiceEndpoint se = new ServiceEndpoint (ContractDescription.GetContract (typeof (IHttpGetHelpPageAndMetadataContract)), cb, new EndpointAddress (uri))
{
ListenUri = uri,
};
var channelDispatcher = new DispatcherBuilder (Owner).BuildChannelDispatcher (owner.Description.ServiceType, se, new BindingParameterCollection ());
channelDispatcher.MessageVersion = MessageVersion.None; // it has no MessageVersion.
channelDispatcher.IsMex = true;
channelDispatcher.Endpoints [0].DispatchRuntime.InstanceContextProvider = new SingletonInstanceContextProvider (new InstanceContext (owner, instance));
dispatchers.Add (uri, channelDispatcher);
owner.ChannelDispatchers.Add (channelDispatcher);
}
void IExtension<ServiceHostBase>.Attach (ServiceHostBase owner)
{
this.owner = owner;
instance = new HttpGetWsdl (this);
}
void IExtension<ServiceHostBase>.Detach (ServiceHostBase owner)
{
this.owner = null;
}
}
[ServiceContract (Namespace = "http://schemas.microsoft.com/2006/04/http/metadata")]
interface IHttpGetHelpPageAndMetadataContract
{
[OperationContract (Action = "*", ReplyAction = "*")]
SMMessage Get (SMMessage req);
}
class HttpGetWsdl : IHttpGetHelpPageAndMetadataContract
{
ServiceMetadataExtension ext;
bool initialized;
Dictionary <string,WSServiceDescription> wsdl_documents =
new Dictionary<string, WSServiceDescription> ();
Dictionary <string, XmlSchema> schemas =
new Dictionary<string, XmlSchema> ();
public HttpGetWsdl (ServiceMetadataExtension ext)
{
this.ext = ext;
}
public Uri HelpUrl { get; set; }
public Uri WsdlUrl { get; set; }
void EnsureMetadata ()
{
if (!initialized) {
GetMetadata ();
initialized = true;
}
}
public SMMessage Get (SMMessage req)
{
EnsureMetadata ();
HttpRequestMessageProperty prop = (HttpRequestMessageProperty) req.Properties [HttpRequestMessageProperty.Name];
NameValueCollection query_string = CreateQueryString (prop.QueryString);
if (query_string == null || query_string.AllKeys.Length != 1) {
if (HelpUrl != null && Uri.Compare (req.Headers.To, HelpUrl, UriComponents.HttpRequestUrl ^ UriComponents.Query, UriFormat.UriEscaped, StringComparison.Ordinal) == 0)
return CreateHelpPage (req);
WSServiceDescription w = GetWsdl ("wsdl");
if (w != null)
return CreateWsdlMessage (w);
}
if (String.Compare (query_string [null], "wsdl", StringComparison.OrdinalIgnoreCase) == 0) {
WSServiceDescription wsdl = GetWsdl ("wsdl");
if (wsdl != null)
return CreateWsdlMessage (wsdl);
} else if (query_string ["wsdl"] != null) {
WSServiceDescription wsdl = GetWsdl (query_string ["wsdl"]);
if (wsdl != null)
return CreateWsdlMessage (wsdl);
} else if (query_string ["xsd"] != null) {
XmlSchema schema = GetXmlSchema (query_string ["xsd"]);
if (schema != null) {
//FIXME: Is this the correct way?
MemoryStream ms = new MemoryStream ();
schema.Write (ms);
ms.Seek (0, SeekOrigin.Begin);
SMMessage ret = SMMessage.CreateMessage (MessageVersion.None, "", XmlReader.Create (ms));
return ret;
}
}
return CreateHelpPage (req);
}
/* Code from HttpListenerRequest */
NameValueCollection CreateQueryString (string query)
{
NameValueCollection query_string = new NameValueCollection ();
if (query == null || query.Length == 0)
return null;
string [] components = query.Split ('&');
foreach (string kv in components) {
int pos = kv.IndexOf ('=');
if (pos == -1) {
query_string.Add (null, HttpUtility.UrlDecode (kv));
} else {
string key = HttpUtility.UrlDecode (kv.Substring (0, pos));
string val = HttpUtility.UrlDecode (kv.Substring (pos + 1));
query_string.Add (key, val);
}
}
return query_string;
}
// It is returned for ServiceDebugBehavior.Http(s)HelpPageUrl.
// They may be empty, and for such case the help page URL is
// simply the service endpoint URL (foobar.svc).
//
// Note that if there is also ServiceMetadataBehavior that
// lacks Http(s)GetUrl, then it is also mapped to the same
// URL, but it requires "?wsdl" parameter and .NET somehow
// differentiates those requests.
//
// If both Http(s)HelpPageUrl and Http(s)GetUrl exist, then
// requests to the service endpoint URL (foobar.svc) results
// in an xml output with empty string (non-WF XML error).
SMMessage CreateHelpPage (SMMessage request)
{
var helpBody = ext.Owner.Description.Behaviors.Find<ServiceMetadataBehavior> () != null ?
String.Format (@"
<p>To create client proxy source, run:</p>
<p><code>svcutil <a href='{0}'>{0}</a></code></p>
<!-- FIXME: add client proxy usage (that required decent ServiceContractGenerator implementation, so I leave it yet.) -->
", new Uri (WsdlUrl.ToString () + "?wsdl")) : // this Uri.ctor() is nasty, but there is no other way to add "?wsdl" (!!)
String.Format (@"
<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);
var html = String.Format (@"
<html>
<head>
<title>Service {0}</title>
</head>
<body>
{1}
</body>
</html>", ext.Owner.Description.Name, helpBody);
var m = SMMessage.CreateMessage (MessageVersion.None, "", XmlReader.Create (new StringReader (html)));
var rp = new HttpResponseMessageProperty ();
rp.Headers ["Content-Type"] = "text/html";
m.Properties.Add (HttpResponseMessageProperty.Name, rp);
return m;
}
SMMessage CreateWsdlMessage (WSServiceDescription wsdl)
{
MemoryStream ms = new MemoryStream ();
XmlWriter xw = XmlWriter.Create (ms);
WSServiceDescription.Serializer.Serialize (xw, wsdl);
ms.Seek (0, SeekOrigin.Begin);
return SMMessage.CreateMessage (MessageVersion.None, "", XmlReader.Create (ms));
}
void GetMetadata ()
{
if (WsdlUrl == null)
return;
MetadataSet metadata = ext.Metadata;
int xs_i = 0, wsdl_i = 0;
//Dictionary keyed by namespace
StringDictionary wsdl_strings = new StringDictionary ();
StringDictionary xsd_strings = new StringDictionary ();
foreach (MetadataSection section in metadata.MetadataSections) {
string key;
XmlSchema xs = section.Metadata as XmlSchema;
if (xs != null) {
key = String.Format ("xsd{0}", xs_i ++);
schemas [key] = xs;
xsd_strings [xs.TargetNamespace] = key;
continue;
}
WSServiceDescription wsdl = section.Metadata as WSServiceDescription;
if (wsdl == null)
continue;
//if (wsdl.TargetNamespace == "http://tempuri.org/")
if (wsdl.Services.Count > 0)
key = "wsdl";
else
key = String.Format ("wsdl{0}", wsdl_i ++);
wsdl_documents [key] = wsdl;
wsdl_strings [wsdl.TargetNamespace] = key;
}
string base_url = WsdlUrl.ToString ();
foreach (WSServiceDescription wsdl in wsdl_documents.Values) {
foreach (Import import in wsdl.Imports) {
if (!String.IsNullOrEmpty (import.Location))
continue;
import.Location = String.Format ("{0}?wsdl={1}", base_url, wsdl_strings [import.Namespace]);
}
foreach (XmlSchema schema in wsdl.Types.Schemas) {
foreach (XmlSchemaObject obj in schema.Includes) {
XmlSchemaImport imp = obj as XmlSchemaImport;
if (imp == null || imp.SchemaLocation != null)
continue;
imp.SchemaLocation = String.Format ("{0}?xsd={1}", base_url, xsd_strings [imp.Namespace]);
}
}
}
}
WSServiceDescription GetWsdl (string which)
{
EnsureMetadata ();
WSServiceDescription wsdl;
wsdl_documents.TryGetValue (which, out wsdl);
return wsdl;
}
XmlSchema GetXmlSchema (string which)
{
EnsureMetadata ();
XmlSchema schema;
schemas.TryGetValue (which, out schema);
return schema;
}
}
}
|