| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Globalization; |
| | | 7 | | using System.IO; |
| | | 8 | | using System.Linq; |
| | | 9 | | using System.Xml; |
| | | 10 | | using System.Xml.Schema; |
| | | 11 | | using CoreWCF.Runtime; |
| | | 12 | | using WsdlNS = System.Web.Services.Description; |
| | | 13 | | |
| | | 14 | | namespace CoreWCF.Description |
| | | 15 | | { |
| | | 16 | | internal static class WsdlHelper |
| | | 17 | | { |
| | | 18 | | public static WsdlNS.ServiceDescription GetSingleWsdl(MetadataSet metadataSet) |
| | | 19 | | { |
| | 19 | 20 | | if (metadataSet.MetadataSections.Count < 1) |
| | | 21 | | { |
| | 0 | 22 | | return null; |
| | | 23 | | } |
| | | 24 | | |
| | 19 | 25 | | List<WsdlNS.ServiceDescription> wsdls = new List<WsdlNS.ServiceDescription>(); |
| | 19 | 26 | | List<XmlSchema> xsds = new List<XmlSchema>(); |
| | | 27 | | |
| | 188 | 28 | | foreach (MetadataSection section in metadataSet.MetadataSections) |
| | | 29 | | { |
| | 75 | 30 | | if (section.Metadata is WsdlNS.ServiceDescription) |
| | | 31 | | { |
| | 19 | 32 | | wsdls.Add((WsdlNS.ServiceDescription)section.Metadata); |
| | | 33 | | } |
| | | 34 | | |
| | 75 | 35 | | if (section.Metadata is XmlSchema) |
| | | 36 | | { |
| | 56 | 37 | | xsds.Add((XmlSchema)section.Metadata); |
| | | 38 | | } |
| | | 39 | | } |
| | | 40 | | |
| | 19 | 41 | | VerifyContractNamespace(wsdls); |
| | 19 | 42 | | WsdlNS.ServiceDescription singleWsdl = GetSingleWsdl(CopyServiceDescriptionCollection(wsdls)); |
| | | 43 | | |
| | | 44 | | // Inline XML schemas |
| | 150 | 45 | | foreach (XmlSchema schema in xsds) |
| | | 46 | | { |
| | 56 | 47 | | XmlSchema newSchema = CloneXsd(schema); |
| | 56 | 48 | | RemoveSchemaLocations(newSchema); |
| | 56 | 49 | | singleWsdl.Types.Schemas.Add(newSchema); |
| | | 50 | | } |
| | | 51 | | |
| | 19 | 52 | | return singleWsdl; |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | private static void RemoveSchemaLocations(XmlSchema schema) |
| | | 56 | | { |
| | 154 | 57 | | foreach (XmlSchemaObject schemaObject in schema.Includes) |
| | | 58 | | { |
| | 21 | 59 | | XmlSchemaExternal external = schemaObject as XmlSchemaExternal; |
| | 21 | 60 | | if (external != null) |
| | | 61 | | { |
| | 21 | 62 | | external.SchemaLocation = null; |
| | | 63 | | } |
| | | 64 | | } |
| | 56 | 65 | | } |
| | | 66 | | |
| | | 67 | | private static WsdlNS.ServiceDescription GetSingleWsdl(List<WsdlNS.ServiceDescription> wsdls) |
| | | 68 | | { |
| | | 69 | | // Use WSDL that has the contracts as the base for single WSDL |
| | 38 | 70 | | WsdlNS.ServiceDescription singleWsdl = wsdls.First(wsdl => wsdl.PortTypes.Count > 0); |
| | 19 | 71 | | if (singleWsdl == null) |
| | | 72 | | { |
| | 0 | 73 | | singleWsdl = new WsdlNS.ServiceDescription(); |
| | | 74 | | } |
| | | 75 | | else |
| | | 76 | | { |
| | 19 | 77 | | singleWsdl.Types.Schemas.Clear(); |
| | 19 | 78 | | singleWsdl.Imports.Clear(); |
| | | 79 | | } |
| | | 80 | | |
| | 19 | 81 | | Dictionary<XmlQualifiedName, XmlQualifiedName> bindingReferenceChanges = new Dictionary<XmlQualifiedName, Xm |
| | 76 | 82 | | foreach (WsdlNS.ServiceDescription wsdl in wsdls) |
| | | 83 | | { |
| | 19 | 84 | | if (wsdl != singleWsdl) |
| | | 85 | | { |
| | 0 | 86 | | MergeWsdl(singleWsdl, wsdl, bindingReferenceChanges); |
| | | 87 | | } |
| | | 88 | | } |
| | | 89 | | |
| | 19 | 90 | | EnsureSingleNamespace(singleWsdl, bindingReferenceChanges); |
| | 19 | 91 | | return singleWsdl; |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | private static List<WsdlNS.ServiceDescription> CopyServiceDescriptionCollection(List<WsdlNS.ServiceDescription> |
| | | 95 | | { |
| | 19 | 96 | | List<WsdlNS.ServiceDescription> newWsdls = new List<WsdlNS.ServiceDescription>(); |
| | 76 | 97 | | foreach (WsdlNS.ServiceDescription wsdl in wsdls) |
| | | 98 | | { |
| | 19 | 99 | | newWsdls.Add(CloneWsdl(wsdl)); |
| | | 100 | | } |
| | | 101 | | |
| | 19 | 102 | | return newWsdls; |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | private static void MergeWsdl(WsdlNS.ServiceDescription singleWsdl, WsdlNS.ServiceDescription wsdl, Dictionary<X |
| | | 106 | | { |
| | 0 | 107 | | if (wsdl.Services.Count > 0) |
| | | 108 | | { |
| | 0 | 109 | | singleWsdl.Name = wsdl.Name; |
| | | 110 | | } |
| | | 111 | | |
| | 0 | 112 | | foreach (WsdlNS.Binding binding in wsdl.Bindings) |
| | | 113 | | { |
| | 0 | 114 | | string uniqueBindingName = NamingHelper.GetUniqueName(binding.Name, IsBindingNameUsed, singleWsdl.Bindin |
| | 0 | 115 | | if (binding.Name != uniqueBindingName) |
| | | 116 | | { |
| | 0 | 117 | | bindingReferenceChanges.Add( |
| | 0 | 118 | | new XmlQualifiedName(binding.Name, binding.ServiceDescription.TargetNamespace), |
| | 0 | 119 | | new XmlQualifiedName(uniqueBindingName, singleWsdl.TargetNamespace)); |
| | 0 | 120 | | UpdatePolicyKeys(binding, uniqueBindingName, wsdl); |
| | 0 | 121 | | binding.Name = uniqueBindingName; |
| | | 122 | | } |
| | | 123 | | |
| | 0 | 124 | | singleWsdl.Bindings.Add(binding); |
| | | 125 | | } |
| | | 126 | | |
| | 0 | 127 | | foreach (object extension in wsdl.Extensions) |
| | | 128 | | { |
| | 0 | 129 | | singleWsdl.Extensions.Add(extension); |
| | | 130 | | } |
| | | 131 | | |
| | 0 | 132 | | foreach (WsdlNS.Message message in wsdl.Messages) |
| | | 133 | | { |
| | 0 | 134 | | singleWsdl.Messages.Add(message); |
| | | 135 | | } |
| | | 136 | | |
| | 0 | 137 | | foreach (WsdlNS.Service service in wsdl.Services) |
| | | 138 | | { |
| | 0 | 139 | | singleWsdl.Services.Add(service); |
| | | 140 | | } |
| | | 141 | | |
| | 0 | 142 | | foreach (string warning in wsdl.ValidationWarnings) |
| | | 143 | | { |
| | 0 | 144 | | singleWsdl.ValidationWarnings.Add(warning); |
| | | 145 | | } |
| | 0 | 146 | | } |
| | | 147 | | |
| | | 148 | | private static void UpdatePolicyKeys(WsdlNS.Binding binding, string newBindingName, WsdlNS.ServiceDescription ws |
| | | 149 | | { |
| | 0 | 150 | | string oldBindingName = binding.Name; |
| | | 151 | | |
| | | 152 | | // policy |
| | 0 | 153 | | IEnumerable<XmlElement> bindingPolicies = FindAllElements(wsdl.Extensions, MetadataStrings.WSPolicy.Elements |
| | 0 | 154 | | string policyIdStringPrefixFormat = "{0}_"; |
| | 0 | 155 | | foreach (XmlElement policyElement in bindingPolicies) |
| | | 156 | | { |
| | 0 | 157 | | XmlNode policyId = policyElement.Attributes.GetNamedItem(MetadataStrings.Wsu.Attributes.Id, MetadataStri |
| | 0 | 158 | | string policyIdString = policyId?.Value; |
| | 0 | 159 | | string policyIdStringWithOldBindingName = string.Format(CultureInfo.InvariantCulture, policyIdStringPref |
| | 0 | 160 | | string policyIdStringWithNewBindingName = string.Format(CultureInfo.InvariantCulture, policyIdStringPref |
| | 0 | 161 | | if (policyId != null && policyIdString != null && policyIdString.StartsWith(policyIdStringWithOldBinding |
| | | 162 | | { |
| | 0 | 163 | | policyId.Value = policyIdStringWithNewBindingName + policyIdString.Substring(policyIdStringWithOldBi |
| | | 164 | | } |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | // policy reference |
| | 0 | 168 | | UpdatePolicyReference(binding.Extensions, oldBindingName, newBindingName); |
| | 0 | 169 | | foreach (WsdlNS.OperationBinding operationBinding in binding.Operations) |
| | | 170 | | { |
| | 0 | 171 | | UpdatePolicyReference(operationBinding.Extensions, oldBindingName, newBindingName); |
| | 0 | 172 | | if (operationBinding.Input != null) |
| | | 173 | | { |
| | 0 | 174 | | UpdatePolicyReference(operationBinding.Input.Extensions, oldBindingName, newBindingName); |
| | | 175 | | } |
| | | 176 | | |
| | 0 | 177 | | if (operationBinding.Output != null) |
| | | 178 | | { |
| | 0 | 179 | | UpdatePolicyReference(operationBinding.Output.Extensions, oldBindingName, newBindingName); |
| | | 180 | | } |
| | | 181 | | |
| | 0 | 182 | | foreach (WsdlNS.FaultBinding fault in operationBinding.Faults) |
| | | 183 | | { |
| | 0 | 184 | | UpdatePolicyReference(fault.Extensions, oldBindingName, newBindingName); |
| | | 185 | | } |
| | | 186 | | } |
| | 0 | 187 | | } |
| | | 188 | | |
| | | 189 | | private static void UpdatePolicyReference(WsdlNS.ServiceDescriptionFormatExtensionCollection extensions, string |
| | | 190 | | { |
| | 0 | 191 | | IEnumerable<XmlElement> bindingPolicyReferences = FindAllElements(extensions, MetadataStrings.WSPolicy.Eleme |
| | 0 | 192 | | string policyReferencePrefixFormat = "#{0}_"; |
| | 0 | 193 | | foreach (XmlElement policyReferenceElement in bindingPolicyReferences) |
| | | 194 | | { |
| | 0 | 195 | | XmlNode policyReference = policyReferenceElement.Attributes.GetNamedItem(MetadataStrings.WSPolicy.Attrib |
| | 0 | 196 | | string policyReferenceValue = policyReference?.Value; |
| | 0 | 197 | | string policyReferenceValueWithOldBindingName = string.Format(CultureInfo.InvariantCulture, policyRefere |
| | 0 | 198 | | string policyReferenceValueWithNewBindingName = string.Format(CultureInfo.InvariantCulture, policyRefere |
| | 0 | 199 | | if (policyReference != null && policyReferenceValue != null && policyReferenceValue.StartsWith(policyRef |
| | | 200 | | { |
| | 0 | 201 | | policyReference.Value = policyReferenceValueWithNewBindingName + policyReference.Value.Substring(pol |
| | | 202 | | } |
| | | 203 | | } |
| | 0 | 204 | | } |
| | | 205 | | |
| | | 206 | | private static IEnumerable<XmlElement> FindAllElements(WsdlNS.ServiceDescriptionFormatExtensionCollection extens |
| | | 207 | | { |
| | 0 | 208 | | List<XmlElement> policyReferences = new List<XmlElement>(); |
| | 0 | 209 | | for (int i = 0; i < extensions.Count; i++) |
| | | 210 | | { |
| | 0 | 211 | | XmlElement element = extensions[i] as XmlElement; |
| | 0 | 212 | | if (element != null && element.LocalName == elementName) |
| | | 213 | | { |
| | 0 | 214 | | policyReferences.Add(element); |
| | | 215 | | } |
| | | 216 | | } |
| | | 217 | | |
| | 0 | 218 | | return policyReferences; |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | private static void VerifyContractNamespace(List<WsdlNS.ServiceDescription> wsdls) |
| | | 222 | | { |
| | 38 | 223 | | IEnumerable<WsdlNS.ServiceDescription> contractWsdls = wsdls.Where(serviceDescription => serviceDescription. |
| | 19 | 224 | | if (contractWsdls.Count() > 1) |
| | | 225 | | { |
| | 0 | 226 | | IEnumerable<string> namespaces = contractWsdls.Select<WsdlNS.ServiceDescription, string>(wsdl => wsdl.Ta |
| | 0 | 227 | | string contractNamespaces = string.Join(", ", namespaces); |
| | 0 | 228 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Format(SR.SingleW |
| | | 229 | | } |
| | 19 | 230 | | } |
| | | 231 | | |
| | | 232 | | private static void EnsureSingleNamespace(WsdlNS.ServiceDescription wsdl, Dictionary<XmlQualifiedName, XmlQualif |
| | | 233 | | { |
| | 19 | 234 | | string targetNamespace = wsdl.TargetNamespace; |
| | 90 | 235 | | foreach (WsdlNS.Binding binding in wsdl.Bindings) |
| | | 236 | | { |
| | 26 | 237 | | if (binding.Type.Namespace != targetNamespace) |
| | | 238 | | { |
| | 0 | 239 | | binding.Type = new XmlQualifiedName(binding.Type.Name, targetNamespace); |
| | | 240 | | } |
| | | 241 | | } |
| | | 242 | | |
| | 76 | 243 | | foreach (WsdlNS.PortType portType in wsdl.PortTypes) |
| | | 244 | | { |
| | 226 | 245 | | foreach (WsdlNS.Operation operation in portType.Operations) |
| | | 246 | | { |
| | 94 | 247 | | WsdlNS.OperationInput messageInput = operation.Messages.Input; |
| | 94 | 248 | | if (messageInput != null && messageInput.Message.Namespace != targetNamespace) |
| | | 249 | | { |
| | 0 | 250 | | messageInput.Message = new XmlQualifiedName(messageInput.Message.Name, targetNamespace); |
| | | 251 | | } |
| | | 252 | | |
| | 94 | 253 | | WsdlNS.OperationOutput messageOutput = operation.Messages.Output; |
| | 94 | 254 | | if (messageOutput != null && messageOutput.Message.Namespace != targetNamespace) |
| | | 255 | | { |
| | 0 | 256 | | messageOutput.Message = new XmlQualifiedName(messageOutput.Message.Name, targetNamespace); |
| | | 257 | | } |
| | | 258 | | |
| | 188 | 259 | | foreach (WsdlNS.OperationFault fault in operation.Faults) |
| | | 260 | | { |
| | 0 | 261 | | if (fault.Message.Namespace != targetNamespace) |
| | | 262 | | { |
| | 0 | 263 | | fault.Message = new XmlQualifiedName(fault.Message.Name, targetNamespace); |
| | | 264 | | } |
| | | 265 | | } |
| | | 266 | | } |
| | | 267 | | } |
| | | 268 | | |
| | 76 | 269 | | foreach (WsdlNS.Service service in wsdl.Services) |
| | | 270 | | { |
| | 90 | 271 | | foreach (WsdlNS.Port port in service.Ports) |
| | | 272 | | { |
| | | 273 | | XmlQualifiedName newPortBinding; |
| | 26 | 274 | | if (bindingReferenceChanges.TryGetValue(port.Binding, out newPortBinding)) |
| | | 275 | | { |
| | 0 | 276 | | port.Binding = newPortBinding; |
| | | 277 | | } |
| | 26 | 278 | | else if (port.Binding.Namespace != targetNamespace) |
| | | 279 | | { |
| | 0 | 280 | | port.Binding = new XmlQualifiedName(port.Binding.Name, targetNamespace); |
| | | 281 | | } |
| | | 282 | | } |
| | | 283 | | } |
| | 19 | 284 | | } |
| | | 285 | | |
| | | 286 | | private static bool IsBindingNameUsed(string name, object collection) |
| | | 287 | | { |
| | 0 | 288 | | WsdlNS.BindingCollection bindings = (WsdlNS.BindingCollection)collection; |
| | 0 | 289 | | foreach (WsdlNS.Binding binding in bindings) |
| | | 290 | | { |
| | 0 | 291 | | if (binding.Name == name) |
| | | 292 | | { |
| | 0 | 293 | | return true; |
| | | 294 | | } |
| | | 295 | | } |
| | | 296 | | |
| | 0 | 297 | | return false; |
| | 0 | 298 | | } |
| | | 299 | | |
| | | 300 | | private static WsdlNS.ServiceDescription CloneWsdl(WsdlNS.ServiceDescription originalWsdl) |
| | | 301 | | { |
| | | 302 | | Fx.Assert(originalWsdl != null, "originalWsdl must not be null"); |
| | | 303 | | WsdlNS.ServiceDescription newWsdl; |
| | 19 | 304 | | using (MemoryStream memoryStream = new MemoryStream()) |
| | | 305 | | { |
| | 19 | 306 | | originalWsdl.Write(memoryStream); |
| | 19 | 307 | | memoryStream.Seek(0, SeekOrigin.Begin); |
| | 19 | 308 | | newWsdl = WsdlNS.ServiceDescription.Read(memoryStream); |
| | 19 | 309 | | } |
| | | 310 | | |
| | 19 | 311 | | return newWsdl; |
| | | 312 | | } |
| | | 313 | | |
| | | 314 | | private static XmlSchema CloneXsd(XmlSchema originalXsd) |
| | | 315 | | { |
| | | 316 | | Fx.Assert(originalXsd != null, "originalXsd must not be null"); |
| | | 317 | | XmlSchema newXsd; |
| | 56 | 318 | | using (MemoryStream memoryStream = new MemoryStream()) |
| | | 319 | | { |
| | 56 | 320 | | originalXsd.Write(memoryStream); |
| | 56 | 321 | | memoryStream.Seek(0, SeekOrigin.Begin); |
| | 56 | 322 | | newXsd = XmlSchema.Read(new XmlTextReader(memoryStream) { DtdProcessing = DtdProcessing.Parse }, null); |
| | 56 | 323 | | } |
| | | 324 | | |
| | 56 | 325 | | return newXsd; |
| | | 326 | | } |
| | | 327 | | } |
| | | 328 | | } |