| | | 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.Xml; |
| | | 5 | | using System.Xml.Schema; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Collections.ObjectModel; |
| | | 8 | | using WsdlNS = System.Web.Services.Description; |
| | | 9 | | using System.Linq; |
| | | 10 | | using System; |
| | | 11 | | using CoreWCF.Runtime; |
| | | 12 | | |
| | | 13 | | namespace CoreWCF.Description |
| | | 14 | | { |
| | | 15 | | // This class is created as part of the export process and passed to |
| | | 16 | | // Wsdlmporter and WsdlExporter implementations as a utility for |
| | | 17 | | // Correlating between the WSDL OM and the WCF OM |
| | | 18 | | // in the conversion process. |
| | | 19 | | public class WsdlContractConversionContext |
| | | 20 | | { |
| | | 21 | | private readonly Dictionary<OperationDescription, WsdlNS.Operation> _wsdlOperations; |
| | | 22 | | private readonly Dictionary<WsdlNS.Operation, OperationDescription> _operationDescriptions; |
| | | 23 | | private readonly Dictionary<MessageDescription, WsdlNS.OperationMessage> _wsdlOperationMessages; |
| | | 24 | | private readonly Dictionary<FaultDescription, WsdlNS.OperationFault> _wsdlOperationFaults; |
| | | 25 | | private readonly Dictionary<WsdlNS.OperationMessage, MessageDescription> _messageDescriptions; |
| | | 26 | | private readonly Dictionary<WsdlNS.OperationFault, FaultDescription> _faultDescriptions; |
| | | 27 | | private readonly Dictionary<WsdlNS.Operation, Collection<WsdlNS.OperationBinding>> _operationBindings; |
| | | 28 | | |
| | 24 | 29 | | internal WsdlContractConversionContext(ContractDescription contract, WsdlNS.PortType wsdlPortType) |
| | | 30 | | { |
| | 24 | 31 | | Contract = contract; |
| | 24 | 32 | | WsdlPortType = wsdlPortType; |
| | | 33 | | |
| | 24 | 34 | | _wsdlOperations = new Dictionary<OperationDescription, WsdlNS.Operation>(); |
| | 24 | 35 | | _operationDescriptions = new Dictionary<WsdlNS.Operation, OperationDescription>(); |
| | 24 | 36 | | _wsdlOperationMessages = new Dictionary<MessageDescription, WsdlNS.OperationMessage>(); |
| | 24 | 37 | | _messageDescriptions = new Dictionary<WsdlNS.OperationMessage, MessageDescription>(); |
| | 24 | 38 | | _wsdlOperationFaults = new Dictionary<FaultDescription, WsdlNS.OperationFault>(); |
| | 24 | 39 | | _faultDescriptions = new Dictionary<WsdlNS.OperationFault, FaultDescription>(); |
| | 24 | 40 | | _operationBindings = new Dictionary<WsdlNS.Operation, Collection<WsdlNS.OperationBinding>>(); |
| | 24 | 41 | | } |
| | | 42 | | |
| | | 43 | | internal IEnumerable<IWsdlExportExtension> ExportExtensions |
| | | 44 | | { |
| | | 45 | | get |
| | | 46 | | { |
| | 48 | 47 | | IEnumerable<IWsdlExportExtension> result = Contract.ContractBehaviors.Where(cb => cb is IWsdlExportExten |
| | 286 | 48 | | foreach (OperationDescription operation in Contract.Operations) |
| | | 49 | | { |
| | 119 | 50 | | if (!WsdlExporter.OperationIsExportable(operation)) |
| | | 51 | | { |
| | | 52 | | continue; |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | // In 3.0SP1, the DCSOB and XSOB were moved from before to after the custom behaviors. For |
| | | 56 | | // IWsdlExportExtension compat, run them in the pre-SP1 order. |
| | 1071 | 57 | | var operationExtensions = operation.OperationBehaviors.Where(ob => ob is IWsdlExportExtension).Selec |
| | 238 | 58 | | result = result.Concat(operationExtensions.Where(oe => WsdlExporter.IsBuiltInOperationBehavior(oe))) |
| | 238 | 59 | | result = result.Concat(operationExtensions.Where(oe => !WsdlExporter.IsBuiltInOperationBehavior(oe)) |
| | | 60 | | } |
| | | 61 | | |
| | 24 | 62 | | return result; |
| | | 63 | | } |
| | | 64 | | } |
| | | 65 | | |
| | 747 | 66 | | public ContractDescription Contract { get; } |
| | 1072 | 67 | | public WsdlNS.PortType WsdlPortType { get; } |
| | 246 | 68 | | public WsdlNS.Operation GetOperation(OperationDescription operation) => _wsdlOperations[operation]; |
| | 238 | 69 | | public WsdlNS.OperationMessage GetOperationMessage(MessageDescription message) => _wsdlOperationMessages[message |
| | 0 | 70 | | public WsdlNS.OperationFault GetOperationFault(FaultDescription fault) => _wsdlOperationFaults[fault]; |
| | 0 | 71 | | public OperationDescription GetOperationDescription(WsdlNS.Operation operation) => _operationDescriptions[operat |
| | 0 | 72 | | public MessageDescription GetMessageDescription(WsdlNS.OperationMessage operationMessage) => _messageDescription |
| | 0 | 73 | | public FaultDescription GetFaultDescription(WsdlNS.OperationFault operationFault) => _faultDescriptions[operatio |
| | | 74 | | |
| | | 75 | | // -------------------------------------------------------------------------------------------------- |
| | | 76 | | |
| | | 77 | | internal void AddOperation(OperationDescription operationDescription, WsdlNS.Operation wsdlOperation) |
| | | 78 | | { |
| | 119 | 79 | | _wsdlOperations.Add(operationDescription, wsdlOperation); |
| | 119 | 80 | | _operationDescriptions.Add(wsdlOperation, operationDescription); |
| | 119 | 81 | | } |
| | | 82 | | |
| | | 83 | | internal void AddMessage(MessageDescription messageDescription, WsdlNS.OperationMessage wsdlOperationMessage) |
| | | 84 | | { |
| | 238 | 85 | | _wsdlOperationMessages.Add(messageDescription, wsdlOperationMessage); |
| | 238 | 86 | | _messageDescriptions.Add(wsdlOperationMessage, messageDescription); |
| | 238 | 87 | | } |
| | | 88 | | |
| | | 89 | | internal void AddFault(FaultDescription faultDescription, WsdlNS.OperationFault wsdlOperationFault) |
| | | 90 | | { |
| | 0 | 91 | | _wsdlOperationFaults.Add(faultDescription, wsdlOperationFault); |
| | 0 | 92 | | _faultDescriptions.Add(wsdlOperationFault, faultDescription); |
| | 0 | 93 | | } |
| | | 94 | | |
| | | 95 | | internal Collection<WsdlNS.OperationBinding> GetOperationBindings(WsdlNS.Operation operation) |
| | | 96 | | { |
| | | 97 | | Collection<WsdlNS.OperationBinding> bindings; |
| | 0 | 98 | | if (!_operationBindings.TryGetValue(operation, out bindings)) |
| | | 99 | | { |
| | 0 | 100 | | bindings = new Collection<WsdlNS.OperationBinding>(); |
| | 0 | 101 | | WsdlNS.ServiceDescriptionCollection wsdlDocuments = WsdlPortType.ServiceDescription.ServiceDescriptions; |
| | 0 | 102 | | foreach (WsdlNS.ServiceDescription wsdl in wsdlDocuments) |
| | | 103 | | { |
| | 0 | 104 | | foreach (WsdlNS.Binding wsdlBinding in wsdl.Bindings) |
| | | 105 | | { |
| | 0 | 106 | | if (wsdlBinding.Type.Name == WsdlPortType.Name && wsdlBinding.Type.Namespace == WsdlPortType.Ser |
| | | 107 | | { |
| | 0 | 108 | | foreach (WsdlNS.OperationBinding operationBinding in wsdlBinding.Operations) |
| | | 109 | | { |
| | 0 | 110 | | if (Binding2DescriptionHelper.Match(operationBinding, operation) != Binding2DescriptionH |
| | | 111 | | { |
| | 0 | 112 | | bindings.Add(operationBinding); |
| | 0 | 113 | | break; |
| | | 114 | | } |
| | | 115 | | } |
| | | 116 | | } |
| | | 117 | | } |
| | | 118 | | } |
| | | 119 | | |
| | 0 | 120 | | _operationBindings.Add(operation, bindings); |
| | | 121 | | } |
| | | 122 | | |
| | 0 | 123 | | return bindings; |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | internal static class Binding2DescriptionHelper |
| | | 127 | | { |
| | | 128 | | internal enum MatchResult |
| | | 129 | | { |
| | | 130 | | None, |
| | | 131 | | Partial, |
| | | 132 | | Exact |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | internal static MatchResult Match(WsdlNS.OperationBinding wsdlOperationBinding, WsdlNS.Operation wsdlOperati |
| | | 136 | | { |
| | | 137 | | // This method checks if there is a match based on Names, between the specified OperationBinding and Ope |
| | | 138 | | // When searching for the Operation associated with an OperationBinding, we need to return an exact matc |
| | | 139 | | // or a partial match otherwise (when some of the Names are null). |
| | | 140 | | // Bug 16833 @ CSDMain requires that partial matches are allowed, while the TFS bug 477838 requires that |
| | 0 | 141 | | if (wsdlOperationBinding.Name != wsdlOperation.Name) |
| | | 142 | | { |
| | 0 | 143 | | return MatchResult.None; |
| | | 144 | | } |
| | | 145 | | |
| | 0 | 146 | | MatchResult result = MatchResult.Exact; |
| | | 147 | | |
| | 0 | 148 | | foreach (WsdlNS.OperationMessage wsdlOperationMessage in wsdlOperation.Messages) |
| | | 149 | | { |
| | | 150 | | WsdlNS.MessageBinding wsdlMessageBinding; |
| | 0 | 151 | | if (wsdlOperationMessage is WsdlNS.OperationInput) |
| | | 152 | | { |
| | 0 | 153 | | wsdlMessageBinding = wsdlOperationBinding.Input; |
| | | 154 | | } |
| | | 155 | | else |
| | | 156 | | { |
| | 0 | 157 | | wsdlMessageBinding = wsdlOperationBinding.Output; |
| | | 158 | | } |
| | | 159 | | |
| | 0 | 160 | | if (wsdlMessageBinding == null) |
| | | 161 | | { |
| | 0 | 162 | | return MatchResult.None; |
| | | 163 | | } |
| | | 164 | | |
| | 0 | 165 | | switch (MatchOperationParameterName(wsdlMessageBinding, wsdlOperationMessage)) |
| | | 166 | | { |
| | | 167 | | case MatchResult.None: |
| | 0 | 168 | | return MatchResult.None; |
| | | 169 | | case MatchResult.Partial: |
| | 0 | 170 | | result = MatchResult.Partial; |
| | | 171 | | break; |
| | | 172 | | } |
| | | 173 | | } |
| | | 174 | | |
| | 0 | 175 | | return result; |
| | 0 | 176 | | } |
| | | 177 | | |
| | | 178 | | private static MatchResult MatchOperationParameterName(WsdlNS.MessageBinding wsdlMessageBinding, WsdlNS.Oper |
| | | 179 | | { |
| | 0 | 180 | | string wsdlOperationMessageName = wsdlOperationMessage.Name; |
| | 0 | 181 | | string wsdlMessageBindingName = wsdlMessageBinding.Name; |
| | | 182 | | |
| | 0 | 183 | | if (wsdlOperationMessageName == wsdlMessageBindingName) |
| | | 184 | | { |
| | 0 | 185 | | return MatchResult.Exact; |
| | | 186 | | } |
| | | 187 | | |
| | 0 | 188 | | string wsdlOperationMessageDecodedName = WsdlNamingHelper.GetOperationMessageName(wsdlOperationMessage); |
| | 0 | 189 | | if ((wsdlOperationMessageName == null) && (wsdlMessageBindingName == wsdlOperationMessageDecodedName)) |
| | | 190 | | { |
| | 0 | 191 | | return MatchResult.Partial; |
| | | 192 | | } |
| | 0 | 193 | | else if ((wsdlMessageBindingName == null) && (wsdlOperationMessageName == wsdlOperationMessageDecodedNam |
| | | 194 | | { |
| | 0 | 195 | | return MatchResult.Partial; |
| | | 196 | | } |
| | | 197 | | else |
| | | 198 | | { |
| | 0 | 199 | | return MatchResult.None; |
| | | 200 | | } |
| | | 201 | | } |
| | | 202 | | } |
| | | 203 | | |
| | | 204 | | private static class WsdlNamingHelper |
| | | 205 | | { |
| | | 206 | | internal static string GetOperationMessageName(WsdlNS.OperationMessage wsdlOperationMessage) |
| | | 207 | | { |
| | 0 | 208 | | string messageName = null; |
| | 0 | 209 | | if (!string.IsNullOrEmpty(wsdlOperationMessage.Name)) |
| | | 210 | | { |
| | 0 | 211 | | messageName = wsdlOperationMessage.Name; |
| | | 212 | | } |
| | 0 | 213 | | else if (wsdlOperationMessage.Operation.Messages.Count == 1) |
| | | 214 | | { |
| | 0 | 215 | | messageName = wsdlOperationMessage.Operation.Name; |
| | | 216 | | } |
| | 0 | 217 | | else if (wsdlOperationMessage.Operation.Messages.IndexOf(wsdlOperationMessage) == 0) |
| | | 218 | | { |
| | 0 | 219 | | if (wsdlOperationMessage is WsdlNS.OperationInput) |
| | | 220 | | { |
| | 0 | 221 | | messageName = wsdlOperationMessage.Operation.Name + "Request"; |
| | | 222 | | } |
| | 0 | 223 | | else if (wsdlOperationMessage is WsdlNS.OperationOutput) |
| | | 224 | | { |
| | 0 | 225 | | messageName = wsdlOperationMessage.Operation.Name + "Solicit"; |
| | | 226 | | } |
| | | 227 | | } |
| | 0 | 228 | | else if (wsdlOperationMessage.Operation.Messages.IndexOf(wsdlOperationMessage) == 1) |
| | | 229 | | { |
| | 0 | 230 | | messageName = wsdlOperationMessage.Operation.Name + "Response"; |
| | | 231 | | } |
| | | 232 | | else |
| | | 233 | | { |
| | | 234 | | Fx.Assert("Unsupported WSDL OM (More than 2 OperationMessages encountered in an Operation or WsdlOM |
| | | 235 | | } |
| | | 236 | | |
| | | 237 | | // This is the same validation that ServiceDescriptor.XmlName performs |
| | | 238 | | try |
| | | 239 | | { |
| | 0 | 240 | | if (messageName != null) |
| | | 241 | | { |
| | 0 | 242 | | XmlConvert.VerifyNCName(messageName); |
| | | 243 | | } |
| | 0 | 244 | | } |
| | 0 | 245 | | catch (XmlException e) |
| | | 246 | | { |
| | 0 | 247 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(e.Message, nameof(Ws |
| | | 248 | | } |
| | | 249 | | |
| | | 250 | | // The original code returned XmlName and the called used DecodedName, this does the equivalent |
| | 0 | 251 | | return XmlConvert.DecodeName(messageName); |
| | | 252 | | } |
| | | 253 | | } |
| | | 254 | | } |
| | | 255 | | } |