| | | 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; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Collections.ObjectModel; |
| | | 8 | | using System.Linq; |
| | | 9 | | using System.Reflection; |
| | | 10 | | using System.Xml; |
| | | 11 | | using System.Xml.Serialization; |
| | | 12 | | using CoreWCF.Channels; |
| | | 13 | | using CoreWCF.Collections.Generic; |
| | | 14 | | using CoreWCF.Dispatcher; |
| | | 15 | | using CoreWCF.Runtime; |
| | | 16 | | |
| | | 17 | | namespace CoreWCF.Description |
| | | 18 | | { |
| | | 19 | | public class XmlSerializerOperationBehavior : IOperationBehavior, IWsdlExportExtension |
| | | 20 | | { |
| | | 21 | | public XmlSerializerOperationBehavior(OperationDescription operation) |
| | | 22 | | : this(operation, null) |
| | | 23 | | { |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | public XmlSerializerOperationBehavior(OperationDescription operation, XmlSerializerFormatAttribute attribute) |
| | | 27 | | { |
| | | 28 | | if (operation == null) |
| | | 29 | | { |
| | | 30 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(operation)); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | Reflector parentReflector = new Reflector(operation.DeclaringContract.Namespace, operation.DeclaringContract |
| | | 34 | | OperationReflector = parentReflector.ReflectOperation(operation, attribute ?? new XmlSerializerFormatAttribu |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | public XmlSerializerOperationBehavior(OperationDescription operation, XmlSerializerFormatAttribute attribute, st |
| | | 38 | | : this(operation, attribute) |
| | | 39 | | { |
| | | 40 | | OperationReflector = new Reflector(contractType, null).ReflectOperation(operation, attribute ?? new XmlSeria |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | private XmlSerializerOperationBehavior(Reflector.OperationReflector reflector, bool builtInOperationBehavior) |
| | | 44 | | { |
| | | 45 | | Fx.Assert(reflector != null, ""); |
| | | 46 | | OperationReflector = reflector; |
| | | 47 | | IsBuiltInOperationBehavior = builtInOperationBehavior; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | internal Reflector.OperationReflector OperationReflector { get; } |
| | | 51 | | |
| | | 52 | | internal bool IsBuiltInOperationBehavior { get; } |
| | | 53 | | |
| | | 54 | | public XmlSerializerFormatAttribute XmlSerializerFormatAttribute |
| | | 55 | | { |
| | | 56 | | get |
| | | 57 | | { |
| | | 58 | | return OperationReflector.Attribute; |
| | | 59 | | } |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | internal static XmlSerializerOperationFormatter CreateOperationFormatter(OperationDescription operation) |
| | | 63 | | { |
| | | 64 | | return new XmlSerializerOperationBehavior(operation).CreateFormatter(); |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | internal static XmlSerializerOperationFormatter CreateOperationFormatter(OperationDescription operation, XmlSeri |
| | | 68 | | { |
| | | 69 | | return new XmlSerializerOperationBehavior(operation, attr).CreateFormatter(); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | internal static void AddBehaviors(ContractDescription contract) |
| | | 73 | | { |
| | | 74 | | AddBehaviors(contract, false); |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | internal static void AddBuiltInBehaviors(ContractDescription contract) |
| | | 78 | | { |
| | | 79 | | AddBehaviors(contract, true); |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | private static void AddBehaviors(ContractDescription contract, bool builtInOperationBehavior) |
| | | 83 | | { |
| | | 84 | | Reflector reflector = new Reflector(contract.Namespace, contract.ContractType); |
| | | 85 | | |
| | | 86 | | foreach (OperationDescription operation in contract.Operations) |
| | | 87 | | { |
| | | 88 | | Reflector.OperationReflector operationReflector = reflector.ReflectOperation(operation); |
| | | 89 | | if (operationReflector != null) |
| | | 90 | | { |
| | | 91 | | bool isInherited = operation.DeclaringContract != contract; |
| | | 92 | | if (!isInherited) |
| | | 93 | | { |
| | | 94 | | operation.Behaviors.Add(new XmlSerializerOperationBehavior(operationReflector, builtInOperationB |
| | | 95 | | //operation.Behaviors.Add(new XmlSerializerOperationGenerator(new XmlSerializerImportOptions())) |
| | | 96 | | } |
| | | 97 | | } |
| | | 98 | | } |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | internal XmlSerializerOperationFormatter CreateFormatter() |
| | | 102 | | { |
| | | 103 | | return new XmlSerializerOperationFormatter(OperationReflector.Operation, OperationReflector.Attribute, Opera |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | private XmlSerializerFaultFormatter CreateFaultFormatter(SynchronizedCollection<FaultContractInfo> faultContract |
| | | 107 | | { |
| | | 108 | | return new XmlSerializerFaultFormatter(faultContractInfos, OperationReflector.XmlSerializerFaultContractInfo |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | void IOperationBehavior.Validate(OperationDescription description) |
| | | 112 | | { |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | void IOperationBehavior.AddBindingParameters(OperationDescription description, BindingParameterCollection parame |
| | | 116 | | { |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | void IOperationBehavior.ApplyDispatchBehavior(OperationDescription description, DispatchOperation dispatch) |
| | | 120 | | { |
| | | 121 | | if (description == null) |
| | | 122 | | { |
| | | 123 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(description)); |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | if (dispatch == null) |
| | | 127 | | { |
| | | 128 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dispatch)); |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | if (dispatch.Formatter == null) |
| | | 132 | | { |
| | | 133 | | dispatch.Formatter = (IDispatchMessageFormatter)CreateFormatter(); |
| | | 134 | | dispatch.DeserializeRequest = OperationReflector.RequestRequiresSerialization; |
| | | 135 | | dispatch.SerializeReply = OperationReflector.ReplyRequiresSerialization; |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | if (OperationReflector.Attribute.SupportFaults) |
| | | 139 | | { |
| | | 140 | | if (!dispatch.IsFaultFormatterSetExplicit) |
| | | 141 | | { |
| | | 142 | | dispatch.FaultFormatter = (IDispatchFaultFormatter)CreateFaultFormatter(dispatch.FaultContractInfos) |
| | | 143 | | } |
| | | 144 | | else |
| | | 145 | | { |
| | | 146 | | if (dispatch.FaultFormatter is IDispatchFaultFormatterWrapper wrapper) |
| | | 147 | | { |
| | | 148 | | wrapper.InnerFaultFormatter = (IDispatchFaultFormatter)CreateFaultFormatter(dispatch.FaultContra |
| | | 149 | | } |
| | | 150 | | } |
| | | 151 | | } |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | void IOperationBehavior.ApplyClientBehavior(OperationDescription description, ClientOperation proxy) |
| | | 155 | | { |
| | | 156 | | if (description == null) |
| | | 157 | | { |
| | | 158 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(description)); |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | if (proxy == null) |
| | | 162 | | { |
| | | 163 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(proxy)); |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | if (proxy.Formatter == null) |
| | | 167 | | { |
| | | 168 | | proxy.Formatter = (IClientMessageFormatter)CreateFormatter(); |
| | | 169 | | proxy.SerializeRequest = OperationReflector.RequestRequiresSerialization; |
| | | 170 | | proxy.DeserializeReply = OperationReflector.ReplyRequiresSerialization; |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | if (OperationReflector.Attribute.SupportFaults && !proxy.IsFaultFormatterSetExplicit) |
| | | 174 | | { |
| | | 175 | | proxy.FaultFormatter = (IClientFaultFormatter)CreateFaultFormatter(proxy.FaultContractInfos); |
| | | 176 | | } |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | void IWsdlExportExtension.ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext endpointContext) |
| | | 180 | | { |
| | | 181 | | if (exporter == null) |
| | | 182 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(exporter)); |
| | | 183 | | if (endpointContext == null) |
| | | 184 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(endpointContext)); |
| | | 185 | | |
| | | 186 | | MessageContractExporter.ExportMessageBinding(exporter, endpointContext, typeof(XmlSerializerMessageContractE |
| | | 187 | | } |
| | | 188 | | |
| | | 189 | | void IWsdlExportExtension.ExportContract(WsdlExporter exporter, WsdlContractConversionContext contractContext) |
| | | 190 | | { |
| | | 191 | | if (exporter == null) |
| | | 192 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(exporter)); |
| | | 193 | | if (contractContext == null) |
| | | 194 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contractContext)); |
| | | 195 | | |
| | | 196 | | new XmlSerializerMessageContractExporter(exporter, contractContext, OperationReflector.Operation, this).Expo |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | public Collection<XmlMapping> GetXmlMappings() |
| | | 200 | | { |
| | | 201 | | Collection<XmlMapping> mappings = new Collection<XmlMapping>(); |
| | | 202 | | if (OperationReflector.Request != null && OperationReflector.Request.HeadersMapping != null) |
| | | 203 | | { |
| | | 204 | | mappings.Add(OperationReflector.Request.HeadersMapping); |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | if (OperationReflector.Request != null && OperationReflector.Request.BodyMapping != null) |
| | | 208 | | { |
| | | 209 | | mappings.Add(OperationReflector.Request.BodyMapping); |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | if (OperationReflector.Reply != null && OperationReflector.Reply.HeadersMapping != null) |
| | | 213 | | { |
| | | 214 | | mappings.Add(OperationReflector.Reply.HeadersMapping); |
| | | 215 | | } |
| | | 216 | | |
| | | 217 | | if (OperationReflector.Reply != null && OperationReflector.Reply.BodyMapping != null) |
| | | 218 | | { |
| | | 219 | | mappings.Add(OperationReflector.Reply.BodyMapping); |
| | | 220 | | } |
| | | 221 | | |
| | | 222 | | return mappings; |
| | | 223 | | } |
| | | 224 | | |
| | | 225 | | // helper for reflecting operations |
| | | 226 | | internal class Reflector |
| | | 227 | | { |
| | | 228 | | private readonly XmlSerializerImporter _importer; |
| | | 229 | | private readonly SerializerGenerationContext _generation; |
| | | 230 | | private readonly Collection<OperationReflector> _operationReflectors = new Collection<OperationReflector>(); |
| | | 231 | | private readonly object _thisLock = new object(); |
| | | 232 | | |
| | | 233 | | internal Reflector(string defaultNs, Type type) |
| | | 234 | | { |
| | | 235 | | _importer = new XmlSerializerImporter(defaultNs); |
| | | 236 | | _generation = new SerializerGenerationContext(type); |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | internal void EnsureMessageInfos() |
| | | 240 | | { |
| | | 241 | | lock (_thisLock) |
| | | 242 | | { |
| | | 243 | | foreach (OperationReflector operationReflector in _operationReflectors) |
| | | 244 | | { |
| | | 245 | | operationReflector.EnsureMessageInfos(); |
| | | 246 | | } |
| | | 247 | | } |
| | | 248 | | } |
| | | 249 | | |
| | | 250 | | private static XmlSerializerFormatAttribute FindAttribute(OperationDescription operation) |
| | | 251 | | { |
| | | 252 | | Type contractType = operation.DeclaringContract?.ContractType; |
| | | 253 | | XmlSerializerFormatAttribute contractFormatAttribute = contractType != null ? TypeLoader.GetFormattingAt |
| | | 254 | | return TypeLoader.GetFormattingAttribute(operation.OperationMethod, contractFormatAttribute) as XmlSeria |
| | | 255 | | } |
| | | 256 | | |
| | | 257 | | // auto-reflects the operation, returning null if no attribute was found or inherited |
| | | 258 | | internal OperationReflector ReflectOperation(OperationDescription operation) |
| | | 259 | | { |
| | | 260 | | XmlSerializerFormatAttribute attr = FindAttribute(operation); |
| | | 261 | | if (attr == null) |
| | | 262 | | { |
| | | 263 | | return null; |
| | | 264 | | } |
| | | 265 | | |
| | | 266 | | return ReflectOperation(operation, attr); |
| | | 267 | | } |
| | | 268 | | |
| | | 269 | | // overrides the auto-reflection with an attribute |
| | | 270 | | internal OperationReflector ReflectOperation(OperationDescription operation, XmlSerializerFormatAttribute at |
| | | 271 | | { |
| | | 272 | | OperationReflector operationReflector = new OperationReflector(this, operation, attrOverride, true/*refl |
| | | 273 | | _operationReflectors.Add(operationReflector); |
| | | 274 | | |
| | | 275 | | return operationReflector; |
| | | 276 | | } |
| | | 277 | | |
| | | 278 | | internal class OperationReflector |
| | | 279 | | { |
| | | 280 | | private readonly Reflector _parent; |
| | | 281 | | |
| | | 282 | | internal readonly OperationDescription Operation; |
| | | 283 | | internal readonly XmlSerializerFormatAttribute Attribute; |
| | | 284 | | |
| | | 285 | | internal readonly bool IsEncoded; |
| | | 286 | | internal readonly bool IsRpc; |
| | | 287 | | internal readonly bool IsOneWay; |
| | | 288 | | internal readonly bool RequestRequiresSerialization; |
| | | 289 | | internal readonly bool ReplyRequiresSerialization; |
| | | 290 | | private readonly string _keyBase; |
| | | 291 | | private MessageInfo _request; |
| | | 292 | | private MessageInfo _reply; |
| | | 293 | | private SynchronizedCollection<XmlSerializerFaultContractInfo> _xmlSerializerFaultContractInfos; |
| | | 294 | | |
| | | 295 | | internal OperationReflector(Reflector parent, OperationDescription operation, XmlSerializerFormatAttribu |
| | | 296 | | { |
| | | 297 | | Fx.Assert(parent != null, ""); |
| | | 298 | | Fx.Assert(operation != null, ""); |
| | | 299 | | Fx.Assert(attr != null, ""); |
| | | 300 | | |
| | | 301 | | OperationFormatter.Validate(operation, attr.Style == OperationFormatStyle.Rpc, attr.IsEncoded); |
| | | 302 | | |
| | | 303 | | _parent = parent; |
| | | 304 | | |
| | | 305 | | Operation = operation; |
| | | 306 | | Attribute = attr; |
| | | 307 | | |
| | | 308 | | IsEncoded = attr.IsEncoded; |
| | | 309 | | IsRpc = (attr.Style == OperationFormatStyle.Rpc); |
| | | 310 | | IsOneWay = operation.Messages.Count == 1; |
| | | 311 | | |
| | | 312 | | RequestRequiresSerialization = !operation.Messages[0].IsUntypedMessage; |
| | | 313 | | ReplyRequiresSerialization = !IsOneWay && !operation.Messages[1].IsUntypedMessage; |
| | | 314 | | |
| | | 315 | | MethodInfo methodInfo = operation.OperationMethod; |
| | | 316 | | if (methodInfo == null) |
| | | 317 | | { |
| | | 318 | | // keyBase needs to be unique within the scope of the parent reflector |
| | | 319 | | _keyBase = string.Empty; |
| | | 320 | | if (operation.DeclaringContract != null) |
| | | 321 | | { |
| | | 322 | | _keyBase = operation.DeclaringContract.Name + "," + operation.DeclaringContract.Namespace + |
| | | 323 | | } |
| | | 324 | | _keyBase = _keyBase + operation.Name; |
| | | 325 | | } |
| | | 326 | | else |
| | | 327 | | { |
| | | 328 | | _keyBase = methodInfo.DeclaringType.FullName + ":" + methodInfo.ToString(); |
| | | 329 | | } |
| | | 330 | | |
| | | 331 | | foreach (MessageDescription message in operation.Messages) |
| | | 332 | | { |
| | | 333 | | foreach (MessageHeaderDescription header in message.Headers) |
| | | 334 | | { |
| | | 335 | | SetUnknownHeaderInDescription(header); |
| | | 336 | | } |
| | | 337 | | } |
| | | 338 | | |
| | | 339 | | if (!reflectOnDemand) |
| | | 340 | | { |
| | | 341 | | EnsureMessageInfos(); |
| | | 342 | | } |
| | | 343 | | } |
| | | 344 | | |
| | | 345 | | private void SetUnknownHeaderInDescription(MessageHeaderDescription header) |
| | | 346 | | { |
| | | 347 | | if (IsEncoded) //XmlAnyElementAttribute does not apply |
| | | 348 | | { |
| | | 349 | | return; |
| | | 350 | | } |
| | | 351 | | |
| | | 352 | | if (header.AdditionalAttributesProvider != null) |
| | | 353 | | { |
| | | 354 | | object[] attributes = header |
| | | 355 | | .AdditionalAttributesProvider |
| | | 356 | | .GetCustomAttributes(false) |
| | | 357 | | .ToArray(); |
| | | 358 | | |
| | | 359 | | bool isUnknown = false; |
| | | 360 | | bool xmlIgnore = false; |
| | | 361 | | |
| | | 362 | | foreach (object attribute in attributes) |
| | | 363 | | { |
| | | 364 | | if (attribute is XmlAnyElementAttribute elementAttribute) |
| | | 365 | | { |
| | | 366 | | if (string.IsNullOrEmpty(elementAttribute.Name)) |
| | | 367 | | { |
| | | 368 | | isUnknown = true; |
| | | 369 | | } |
| | | 370 | | } |
| | | 371 | | |
| | | 372 | | // In the original full framework code using XmlAttributes, the XmlAnyElements collection is |
| | | 373 | | // if the XmlIgnore attribute is present. |
| | | 374 | | if (attribute is XmlIgnoreAttribute) |
| | | 375 | | { |
| | | 376 | | xmlIgnore = true; |
| | | 377 | | break; // XmlIgnore overrides all so no need to continue if found. |
| | | 378 | | } |
| | | 379 | | } |
| | | 380 | | |
| | | 381 | | header.IsUnknownHeaderCollection = isUnknown && !xmlIgnore; |
| | | 382 | | } |
| | | 383 | | } |
| | | 384 | | |
| | | 385 | | private string ContractName |
| | | 386 | | { |
| | | 387 | | get { return Operation.DeclaringContract.Name; } |
| | | 388 | | } |
| | | 389 | | |
| | | 390 | | private string ContractNamespace |
| | | 391 | | { |
| | | 392 | | get { return Operation.DeclaringContract.Namespace; } |
| | | 393 | | } |
| | | 394 | | |
| | | 395 | | internal MessageInfo Request |
| | | 396 | | { |
| | | 397 | | get |
| | | 398 | | { |
| | | 399 | | _parent.EnsureMessageInfos(); |
| | | 400 | | return _request; |
| | | 401 | | } |
| | | 402 | | } |
| | | 403 | | |
| | | 404 | | internal MessageInfo Reply |
| | | 405 | | { |
| | | 406 | | get |
| | | 407 | | { |
| | | 408 | | _parent.EnsureMessageInfos(); |
| | | 409 | | return _reply; |
| | | 410 | | } |
| | | 411 | | } |
| | | 412 | | |
| | | 413 | | internal SynchronizedCollection<XmlSerializerFaultContractInfo> XmlSerializerFaultContractInfos |
| | | 414 | | { |
| | | 415 | | get |
| | | 416 | | { |
| | | 417 | | _parent.EnsureMessageInfos(); |
| | | 418 | | return _xmlSerializerFaultContractInfos; |
| | | 419 | | } |
| | | 420 | | } |
| | | 421 | | |
| | | 422 | | internal void EnsureMessageInfos() |
| | | 423 | | { |
| | | 424 | | if (_request == null) |
| | | 425 | | { |
| | | 426 | | foreach (Type knownType in Operation.KnownTypes) |
| | | 427 | | { |
| | | 428 | | if (knownType == null) |
| | | 429 | | { |
| | | 430 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | | 431 | | } |
| | | 432 | | |
| | | 433 | | _parent._importer.IncludeType(knownType, IsEncoded); |
| | | 434 | | } |
| | | 435 | | _request = CreateMessageInfo(Operation.Messages[0], ":Request"); |
| | | 436 | | if (_request != null && IsRpc && Operation.IsValidateRpcWrapperName && _request.BodyMapping.XsdE |
| | | 437 | | { |
| | | 438 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.F |
| | | 439 | | } |
| | | 440 | | |
| | | 441 | | if (!IsOneWay) |
| | | 442 | | { |
| | | 443 | | _reply = CreateMessageInfo(Operation.Messages[1], ":Response"); |
| | | 444 | | XmlName responseName = TypeLoader.GetBodyWrapperResponseName(Operation.Name); |
| | | 445 | | if (_reply != null && IsRpc && Operation.IsValidateRpcWrapperName && _reply.BodyMapping.XsdE |
| | | 446 | | { |
| | | 447 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | | 448 | | } |
| | | 449 | | } |
| | | 450 | | if (Attribute.SupportFaults) |
| | | 451 | | { |
| | | 452 | | GenerateXmlSerializerFaultContractInfos(); |
| | | 453 | | } |
| | | 454 | | } |
| | | 455 | | } |
| | | 456 | | |
| | | 457 | | private void GenerateXmlSerializerFaultContractInfos() |
| | | 458 | | { |
| | | 459 | | SynchronizedCollection<XmlSerializerFaultContractInfo> faultInfos = new SynchronizedCollection<XmlSe |
| | | 460 | | for (int i = 0; i < Operation.Faults.Count; i++) |
| | | 461 | | { |
| | | 462 | | FaultDescription fault = Operation.Faults[i]; |
| | | 463 | | FaultContractInfo faultContractInfo = new FaultContractInfo(fault.Action, fault.DetailType, faul |
| | | 464 | | |
| | | 465 | | XmlMembersMapping xmlMembersMapping = ImportFaultElement(fault, out XmlQualifiedName elementName |
| | | 466 | | |
| | | 467 | | SerializerStub serializerStub = _parent._generation.AddSerializer(xmlMembersMapping); |
| | | 468 | | faultInfos.Add(new XmlSerializerFaultContractInfo(faultContractInfo, serializerStub, elementName |
| | | 469 | | } |
| | | 470 | | _xmlSerializerFaultContractInfos = faultInfos; |
| | | 471 | | } |
| | | 472 | | |
| | | 473 | | private MessageInfo CreateMessageInfo(MessageDescription message, string key) |
| | | 474 | | { |
| | | 475 | | if (message.IsUntypedMessage) |
| | | 476 | | { |
| | | 477 | | return null; |
| | | 478 | | } |
| | | 479 | | |
| | | 480 | | MessageInfo info = new MessageInfo(); |
| | | 481 | | if (message.IsTypedMessage) |
| | | 482 | | { |
| | | 483 | | key = message.MessageType.FullName + ":" + IsEncoded + ":" + IsRpc; |
| | | 484 | | } |
| | | 485 | | |
| | | 486 | | XmlMembersMapping headersMapping = LoadHeadersMapping(message, key + ":Headers"); |
| | | 487 | | info.SetHeaders(_parent._generation.AddSerializer(headersMapping)); |
| | | 488 | | info.SetBody(_parent._generation.AddSerializer(LoadBodyMapping(message, key, out MessagePartDescript |
| | | 489 | | CreateHeaderDescriptionTable(message, info, headersMapping); |
| | | 490 | | return info; |
| | | 491 | | } |
| | | 492 | | |
| | | 493 | | private void CreateHeaderDescriptionTable(MessageDescription message, MessageInfo info, XmlMembersMappin |
| | | 494 | | { |
| | | 495 | | int headerNameIndex = 0; |
| | | 496 | | OperationFormatter.MessageHeaderDescriptionTable headerDescriptionTable = new OperationFormatter.Mes |
| | | 497 | | info.SetHeaderDescriptionTable(headerDescriptionTable); |
| | | 498 | | foreach (MessageHeaderDescription header in message.Headers) |
| | | 499 | | { |
| | | 500 | | if (header.IsUnknownHeaderCollection) |
| | | 501 | | { |
| | | 502 | | info.SetUnknownHeaderDescription(header); |
| | | 503 | | } |
| | | 504 | | else if (headersMapping != null) |
| | | 505 | | { |
| | | 506 | | XmlMemberMapping memberMapping = headersMapping[headerNameIndex++]; |
| | | 507 | | string headerName, headerNs; |
| | | 508 | | if (IsEncoded) |
| | | 509 | | { |
| | | 510 | | headerName = memberMapping.TypeName; |
| | | 511 | | headerNs = memberMapping.TypeNamespace; |
| | | 512 | | } |
| | | 513 | | else |
| | | 514 | | { |
| | | 515 | | headerName = memberMapping.XsdElementName; |
| | | 516 | | headerNs = memberMapping.Namespace; |
| | | 517 | | } |
| | | 518 | | if (headerName != header.Name) |
| | | 519 | | { |
| | | 520 | | if (message.MessageType != null) |
| | | 521 | | { |
| | | 522 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationExcept |
| | | 523 | | } |
| | | 524 | | else |
| | | 525 | | { |
| | | 526 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationExcept |
| | | 527 | | } |
| | | 528 | | } |
| | | 529 | | if (headerNs != header.Namespace) |
| | | 530 | | { |
| | | 531 | | if (message.MessageType != null) |
| | | 532 | | { |
| | | 533 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationExcept |
| | | 534 | | } |
| | | 535 | | else |
| | | 536 | | { |
| | | 537 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationExcept |
| | | 538 | | } |
| | | 539 | | } |
| | | 540 | | |
| | | 541 | | headerDescriptionTable.Add(headerName, headerNs, header); |
| | | 542 | | } |
| | | 543 | | } |
| | | 544 | | } |
| | | 545 | | |
| | | 546 | | private XmlMembersMapping LoadBodyMapping(MessageDescription message, string mappingKey, out MessagePart |
| | | 547 | | { |
| | | 548 | | MessagePartDescription returnPart; |
| | | 549 | | string wrapperName, wrapperNs; |
| | | 550 | | MessagePartDescriptionCollection bodyParts; |
| | | 551 | | if (IsEncoded && message.IsTypedMessage && message.Body.WrapperName == null) |
| | | 552 | | { |
| | | 553 | | MessagePartDescription wrapperPart = GetWrapperPart(message); |
| | | 554 | | returnPart = null; |
| | | 555 | | rpcEncodedTypedMessageBodyParts = bodyParts = GetWrappedParts(wrapperPart); |
| | | 556 | | wrapperName = wrapperPart.Name; |
| | | 557 | | wrapperNs = wrapperPart.Namespace; |
| | | 558 | | } |
| | | 559 | | else |
| | | 560 | | { |
| | | 561 | | rpcEncodedTypedMessageBodyParts = null; |
| | | 562 | | returnPart = OperationFormatter.IsValidReturnValue(message.Body.ReturnValue) ? message.Body.Retu |
| | | 563 | | bodyParts = message.Body.Parts; |
| | | 564 | | wrapperName = message.Body.WrapperName; |
| | | 565 | | wrapperNs = message.Body.WrapperNamespace; |
| | | 566 | | } |
| | | 567 | | bool isWrapped = (wrapperName != null); |
| | | 568 | | bool hasReturnValue = returnPart != null; |
| | | 569 | | int paramCount = bodyParts.Count + (hasReturnValue ? 1 : 0); |
| | | 570 | | if (paramCount == 0 && !isWrapped) // no need to create serializer |
| | | 571 | | { |
| | | 572 | | return null; |
| | | 573 | | } |
| | | 574 | | |
| | | 575 | | XmlReflectionMember[] members = new XmlReflectionMember[paramCount]; |
| | | 576 | | int paramIndex = 0; |
| | | 577 | | if (hasReturnValue) |
| | | 578 | | { |
| | | 579 | | members[paramIndex++] = XmlSerializerHelper.GetXmlReflectionMember(returnPart, IsRpc, IsEncoded, |
| | | 580 | | } |
| | | 581 | | |
| | | 582 | | for (int i = 0; i < bodyParts.Count; i++) |
| | | 583 | | { |
| | | 584 | | members[paramIndex++] = XmlSerializerHelper.GetXmlReflectionMember(bodyParts[i], IsRpc, IsEncode |
| | | 585 | | } |
| | | 586 | | |
| | | 587 | | if (!isWrapped) |
| | | 588 | | { |
| | | 589 | | wrapperNs = ContractNamespace; |
| | | 590 | | } |
| | | 591 | | |
| | | 592 | | return ImportMembersMapping(wrapperName, wrapperNs, members, isWrapped, IsRpc, mappingKey); |
| | | 593 | | } |
| | | 594 | | |
| | | 595 | | private MessagePartDescription GetWrapperPart(MessageDescription message) |
| | | 596 | | { |
| | | 597 | | if (message.Body.Parts.Count != 1) |
| | | 598 | | { |
| | | 599 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma |
| | | 600 | | } |
| | | 601 | | |
| | | 602 | | MessagePartDescription bodyPart = message.Body.Parts[0]; |
| | | 603 | | Type bodyObjectType = bodyPart.Type; |
| | | 604 | | if (bodyObjectType.GetTypeInfo().BaseType != null && bodyObjectType.GetTypeInfo().BaseType != typeof |
| | | 605 | | { |
| | | 606 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma |
| | | 607 | | } |
| | | 608 | | |
| | | 609 | | if (typeof(IEnumerable).IsAssignableFrom(bodyObjectType)) |
| | | 610 | | { |
| | | 611 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma |
| | | 612 | | } |
| | | 613 | | |
| | | 614 | | if (typeof(IXmlSerializable).IsAssignableFrom(bodyObjectType)) |
| | | 615 | | { |
| | | 616 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma |
| | | 617 | | } |
| | | 618 | | |
| | | 619 | | return bodyPart; |
| | | 620 | | } |
| | | 621 | | |
| | | 622 | | private MessagePartDescriptionCollection GetWrappedParts(MessagePartDescription bodyPart) |
| | | 623 | | { |
| | | 624 | | Type bodyObjectType = bodyPart.Type; |
| | | 625 | | MessagePartDescriptionCollection partList = new MessagePartDescriptionCollection(); |
| | | 626 | | foreach (MemberInfo member in bodyObjectType.GetMembers(BindingFlags.Instance | BindingFlags.Public) |
| | | 627 | | { |
| | | 628 | | if ((member as PropertyInfo) == null && (member as FieldInfo) == null) |
| | | 629 | | { |
| | | 630 | | continue; |
| | | 631 | | } |
| | | 632 | | |
| | | 633 | | if (member.IsDefined(typeof(SoapIgnoreAttribute), inherit: false)) |
| | | 634 | | { |
| | | 635 | | continue; |
| | | 636 | | } |
| | | 637 | | |
| | | 638 | | XmlName xmlName = new XmlName(member.Name); |
| | | 639 | | MessagePartDescription part = new MessagePartDescription(xmlName.EncodedName, string.Empty); |
| | | 640 | | part.AdditionalAttributesProvider = part.MemberInfo = member; |
| | | 641 | | part.Index = part.SerializationPosition = partList.Count; |
| | | 642 | | part.Type = (member is PropertyInfo) ? ((PropertyInfo)member).PropertyType : ((FieldInfo)member) |
| | | 643 | | partList.Add(part); |
| | | 644 | | } |
| | | 645 | | return partList; |
| | | 646 | | } |
| | | 647 | | |
| | | 648 | | private XmlMembersMapping LoadHeadersMapping(MessageDescription message, string mappingKey) |
| | | 649 | | { |
| | | 650 | | int headerCount = message.Headers.Count; |
| | | 651 | | |
| | | 652 | | if (headerCount == 0) |
| | | 653 | | { |
| | | 654 | | return null; |
| | | 655 | | } |
| | | 656 | | |
| | | 657 | | if (IsEncoded) |
| | | 658 | | { |
| | | 659 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma |
| | | 660 | | } |
| | | 661 | | |
| | | 662 | | int unknownHeaderCount = 0, headerIndex = 0; |
| | | 663 | | XmlReflectionMember[] members = new XmlReflectionMember[headerCount]; |
| | | 664 | | for (int i = 0; i < headerCount; i++) |
| | | 665 | | { |
| | | 666 | | MessageHeaderDescription header = message.Headers[i]; |
| | | 667 | | if (!header.IsUnknownHeaderCollection) |
| | | 668 | | { |
| | | 669 | | members[headerIndex++] = XmlSerializerHelper.GetXmlReflectionMember(header, false/*isRpc*/, |
| | | 670 | | } |
| | | 671 | | else |
| | | 672 | | { |
| | | 673 | | unknownHeaderCount++; |
| | | 674 | | } |
| | | 675 | | } |
| | | 676 | | |
| | | 677 | | if (unknownHeaderCount == headerCount) |
| | | 678 | | { |
| | | 679 | | return null; |
| | | 680 | | } |
| | | 681 | | |
| | | 682 | | if (unknownHeaderCount > 0) |
| | | 683 | | { |
| | | 684 | | XmlReflectionMember[] newMembers = new XmlReflectionMember[headerCount - unknownHeaderCount]; |
| | | 685 | | Array.Copy(members, newMembers, newMembers.Length); |
| | | 686 | | members = newMembers; |
| | | 687 | | } |
| | | 688 | | |
| | | 689 | | return ImportMembersMapping(ContractName, ContractNamespace, members, false /*isWrapped*/, false /*i |
| | | 690 | | } |
| | | 691 | | |
| | | 692 | | internal XmlMembersMapping ImportMembersMapping(string elementName, string ns, XmlReflectionMember[] mem |
| | | 693 | | { |
| | | 694 | | string key = mappingKey.StartsWith(":", StringComparison.Ordinal) ? _keyBase + mappingKey : mappingK |
| | | 695 | | return _parent._importer.ImportMembersMapping(new XmlName(elementName, true /*isEncoded*/), ns, memb |
| | | 696 | | } |
| | | 697 | | |
| | | 698 | | internal XmlMembersMapping ImportFaultElement(FaultDescription fault, out XmlQualifiedName elementName) |
| | | 699 | | { |
| | | 700 | | // the number of reflection members is always 1 because there is only one fault detail type |
| | | 701 | | XmlReflectionMember[] members = new XmlReflectionMember[1]; |
| | | 702 | | |
| | | 703 | | XmlName faultElementName = fault.ElementName; |
| | | 704 | | string faultNamespace = fault.Namespace; |
| | | 705 | | if (faultElementName == null) |
| | | 706 | | { |
| | | 707 | | XmlTypeMapping mapping = _parent._importer.ImportTypeMapping(fault.DetailType, IsEncoded); |
| | | 708 | | faultElementName = new XmlName(mapping.ElementName, IsEncoded); |
| | | 709 | | faultNamespace = mapping.Namespace; |
| | | 710 | | if (faultElementName == null) |
| | | 711 | | { |
| | | 712 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.F |
| | | 713 | | } |
| | | 714 | | } |
| | | 715 | | |
| | | 716 | | elementName = new XmlQualifiedName(faultElementName.DecodedName, faultNamespace); |
| | | 717 | | |
| | | 718 | | members[0] = XmlSerializerHelper.GetXmlReflectionMember(null /*memberName*/, faultElementName, fault |
| | | 719 | | null /*additionalAttributesProvider*/, false /*isMultiple*/, IsEncoded, false /*isWrapped*/); |
| | | 720 | | |
| | | 721 | | string mappingKey = "fault:" + faultElementName.DecodedName + ":" + faultNamespace; |
| | | 722 | | return ImportMembersMapping(faultElementName.EncodedName, faultNamespace, members, false /*hasWrappe |
| | | 723 | | } |
| | | 724 | | } |
| | | 725 | | |
| | | 726 | | private class XmlSerializerImporter |
| | | 727 | | { |
| | | 728 | | private readonly string _defaultNs; |
| | | 729 | | private XmlReflectionImporter _xmlImporter; |
| | | 730 | | private SoapReflectionImporter _soapImporter; |
| | | 731 | | private Dictionary<string, XmlMembersMapping> _xmlMappings; |
| | | 732 | | |
| | | 733 | | internal XmlSerializerImporter(string defaultNs) |
| | | 734 | | { |
| | | 735 | | _defaultNs = defaultNs; |
| | | 736 | | _xmlImporter = null; |
| | | 737 | | _soapImporter = null; |
| | | 738 | | } |
| | | 739 | | |
| | | 740 | | private SoapReflectionImporter SoapImporter |
| | | 741 | | { |
| | | 742 | | get |
| | | 743 | | { |
| | | 744 | | if (_soapImporter == null) |
| | | 745 | | { |
| | | 746 | | _soapImporter = new SoapReflectionImporter(NamingHelper.CombineUriStrings(_defaultNs, "encod |
| | | 747 | | } |
| | | 748 | | return _soapImporter; |
| | | 749 | | } |
| | | 750 | | } |
| | | 751 | | |
| | | 752 | | private XmlReflectionImporter XmlImporter |
| | | 753 | | { |
| | | 754 | | get |
| | | 755 | | { |
| | | 756 | | if (_xmlImporter == null) |
| | | 757 | | { |
| | | 758 | | _xmlImporter = new XmlReflectionImporter(_defaultNs); |
| | | 759 | | } |
| | | 760 | | return _xmlImporter; |
| | | 761 | | } |
| | | 762 | | } |
| | | 763 | | |
| | | 764 | | private Dictionary<string, XmlMembersMapping> XmlMappings |
| | | 765 | | { |
| | | 766 | | get |
| | | 767 | | { |
| | | 768 | | if (_xmlMappings == null) |
| | | 769 | | { |
| | | 770 | | _xmlMappings = new Dictionary<string, XmlMembersMapping>(); |
| | | 771 | | } |
| | | 772 | | return _xmlMappings; |
| | | 773 | | } |
| | | 774 | | } |
| | | 775 | | |
| | | 776 | | internal XmlMembersMapping ImportMembersMapping(XmlName elementName, string ns, XmlReflectionMember[] me |
| | | 777 | | { |
| | | 778 | | string mappingName = elementName.DecodedName; |
| | | 779 | | if (XmlMappings.TryGetValue(mappingKey, out XmlMembersMapping mapping)) |
| | | 780 | | { |
| | | 781 | | return mapping; |
| | | 782 | | } |
| | | 783 | | |
| | | 784 | | if (isEncoded) |
| | | 785 | | { |
| | | 786 | | mapping = SoapImporter.ImportMembersMapping(mappingName, ns, members, hasWrapperElement, rpc); |
| | | 787 | | } |
| | | 788 | | else |
| | | 789 | | { |
| | | 790 | | mapping = XmlImporter.ImportMembersMapping(mappingName, ns, members, hasWrapperElement, rpc); |
| | | 791 | | } |
| | | 792 | | |
| | | 793 | | mapping.SetKey(mappingKey); |
| | | 794 | | XmlMappings.Add(mappingKey, mapping); |
| | | 795 | | return mapping; |
| | | 796 | | } |
| | | 797 | | |
| | | 798 | | internal XmlTypeMapping ImportTypeMapping(Type type, bool isEncoded) |
| | | 799 | | { |
| | | 800 | | if (isEncoded) |
| | | 801 | | { |
| | | 802 | | return SoapImporter.ImportTypeMapping(type); |
| | | 803 | | } |
| | | 804 | | else |
| | | 805 | | { |
| | | 806 | | return XmlImporter.ImportTypeMapping(type); |
| | | 807 | | } |
| | | 808 | | } |
| | | 809 | | |
| | | 810 | | internal void IncludeType(Type knownType, bool isEncoded) |
| | | 811 | | { |
| | | 812 | | if (isEncoded) |
| | | 813 | | { |
| | | 814 | | SoapImporter.IncludeType(knownType); |
| | | 815 | | } |
| | | 816 | | else |
| | | 817 | | { |
| | | 818 | | XmlImporter.IncludeType(knownType); |
| | | 819 | | } |
| | | 820 | | } |
| | | 821 | | } |
| | | 822 | | |
| | | 823 | | internal class SerializerGenerationContext |
| | | 824 | | { |
| | | 825 | | private readonly List<XmlMembersMapping> _mappings = new List<XmlMembersMapping>(); |
| | | 826 | | private XmlSerializer[] _serializers = null; |
| | | 827 | | private readonly Type _type; |
| | | 828 | | private readonly object _thisLock = new object(); |
| | | 829 | | |
| | | 830 | | internal SerializerGenerationContext(Type type) |
| | | 831 | | { |
| | | 832 | | _type = type; |
| | | 833 | | } |
| | | 834 | | |
| | | 835 | | // returns a stub to a serializer |
| | | 836 | | internal SerializerStub AddSerializer(XmlMembersMapping mapping) |
| | | 837 | | { |
| | | 838 | | int handle = -1; |
| | | 839 | | if (mapping != null) |
| | | 840 | | { |
| | | 841 | | handle = ((IList)_mappings).Add(mapping); |
| | | 842 | | } |
| | | 843 | | |
| | | 844 | | return new SerializerStub(this, mapping, handle); |
| | | 845 | | } |
| | | 846 | | |
| | | 847 | | internal XmlSerializer GetSerializer(int handle) |
| | | 848 | | { |
| | | 849 | | if (handle < 0) |
| | | 850 | | { |
| | | 851 | | return null; |
| | | 852 | | } |
| | | 853 | | |
| | | 854 | | if (_serializers == null) |
| | | 855 | | { |
| | | 856 | | lock (_thisLock) |
| | | 857 | | { |
| | | 858 | | if (_serializers == null) |
| | | 859 | | { |
| | | 860 | | _serializers = GenerateSerializers(); |
| | | 861 | | } |
| | | 862 | | } |
| | | 863 | | } |
| | | 864 | | return _serializers[handle]; |
| | | 865 | | } |
| | | 866 | | |
| | | 867 | | private XmlSerializer[] GenerateSerializers() |
| | | 868 | | { |
| | | 869 | | //this.Mappings may have duplicate mappings (for e.g. same message contract is used by more than one |
| | | 870 | | //XmlSerializer.FromMappings require unique mappings. The following code uniquifies, calls FromMappi |
| | | 871 | | List<XmlMembersMapping> uniqueMappings = new List<XmlMembersMapping>(); |
| | | 872 | | int[] uniqueIndexes = new int[_mappings.Count]; |
| | | 873 | | for (int srcIndex = 0; srcIndex < _mappings.Count; srcIndex++) |
| | | 874 | | { |
| | | 875 | | XmlMembersMapping mapping = _mappings[srcIndex]; |
| | | 876 | | int uniqueIndex = uniqueMappings.IndexOf(mapping); |
| | | 877 | | if (uniqueIndex < 0) |
| | | 878 | | { |
| | | 879 | | uniqueMappings.Add(mapping); |
| | | 880 | | uniqueIndex = uniqueMappings.Count - 1; |
| | | 881 | | } |
| | | 882 | | uniqueIndexes[srcIndex] = uniqueIndex; |
| | | 883 | | } |
| | | 884 | | XmlSerializer[] uniqueSerializers = CreateSerializersFromMappings(uniqueMappings.ToArray(), _type); |
| | | 885 | | if (uniqueMappings.Count == _mappings.Count) |
| | | 886 | | { |
| | | 887 | | return uniqueSerializers; |
| | | 888 | | } |
| | | 889 | | |
| | | 890 | | XmlSerializer[] serializers = new XmlSerializer[_mappings.Count]; |
| | | 891 | | for (int i = 0; i < _mappings.Count; i++) |
| | | 892 | | { |
| | | 893 | | serializers[i] = uniqueSerializers[uniqueIndexes[i]]; |
| | | 894 | | } |
| | | 895 | | return serializers; |
| | | 896 | | } |
| | | 897 | | |
| | | 898 | | private XmlSerializer[] CreateSerializersFromMappings(XmlMapping[] mappings, Type type) |
| | | 899 | | { |
| | | 900 | | return XmlSerializer.FromMappings(mappings, type); |
| | | 901 | | } |
| | | 902 | | } |
| | | 903 | | |
| | | 904 | | internal struct SerializerStub |
| | | 905 | | { |
| | | 906 | | private readonly SerializerGenerationContext _context; |
| | | 907 | | |
| | | 908 | | internal readonly XmlMembersMapping Mapping; |
| | | 909 | | internal readonly int Handle; |
| | | 910 | | |
| | | 911 | | internal SerializerStub(SerializerGenerationContext context, XmlMembersMapping mapping, int handle) |
| | | 912 | | { |
| | | 913 | | _context = context; |
| | | 914 | | Mapping = mapping; |
| | | 915 | | Handle = handle; |
| | | 916 | | } |
| | | 917 | | |
| | | 918 | | internal XmlSerializer GetSerializer() |
| | | 919 | | { |
| | | 920 | | return _context.GetSerializer(Handle); |
| | | 921 | | } |
| | | 922 | | } |
| | | 923 | | |
| | | 924 | | internal class XmlSerializerFaultContractInfo |
| | | 925 | | { |
| | | 926 | | private readonly SerializerStub _serializerStub; |
| | | 927 | | private XmlSerializerObjectSerializer _serializer; |
| | | 928 | | |
| | | 929 | | internal XmlSerializerFaultContractInfo(FaultContractInfo faultContractInfo, SerializerStub serializerSt |
| | | 930 | | XmlQualifiedName faultContractElementName) |
| | | 931 | | { |
| | | 932 | | FaultContractInfo = faultContractInfo ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumen |
| | | 933 | | _serializerStub = serializerStub; |
| | | 934 | | FaultContractElementName = faultContractElementName ?? throw DiagnosticUtility.ExceptionUtility.Thro |
| | | 935 | | } |
| | | 936 | | |
| | | 937 | | internal FaultContractInfo FaultContractInfo { get; } |
| | | 938 | | |
| | | 939 | | internal XmlQualifiedName FaultContractElementName { get; } |
| | | 940 | | |
| | | 941 | | internal XmlSerializerObjectSerializer Serializer |
| | | 942 | | { |
| | | 943 | | get |
| | | 944 | | { |
| | | 945 | | if (_serializer == null) |
| | | 946 | | { |
| | | 947 | | _serializer = new XmlSerializerObjectSerializer(FaultContractInfo.Detail, FaultContractEleme |
| | | 948 | | } |
| | | 949 | | |
| | | 950 | | return _serializer; |
| | | 951 | | } |
| | | 952 | | } |
| | | 953 | | } |
| | | 954 | | |
| | | 955 | | internal class MessageInfo : XmlSerializerOperationFormatter.MessageInfo |
| | | 956 | | { |
| | | 957 | | private SerializerStub _headers; |
| | | 958 | | private SerializerStub _body; |
| | | 959 | | private OperationFormatter.MessageHeaderDescriptionTable _headerDescriptionTable; |
| | | 960 | | private MessageHeaderDescription _unknownHeaderDescription; |
| | | 961 | | private MessagePartDescriptionCollection _rpcEncodedTypedMessageBodyParts; |
| | | 962 | | |
| | | 963 | | internal XmlMembersMapping BodyMapping |
| | | 964 | | { |
| | | 965 | | get { return _body.Mapping; } |
| | | 966 | | } |
| | | 967 | | |
| | | 968 | | internal override XmlSerializer BodySerializer |
| | | 969 | | { |
| | | 970 | | get { return _body.GetSerializer(); } |
| | | 971 | | } |
| | | 972 | | |
| | | 973 | | internal XmlMembersMapping HeadersMapping |
| | | 974 | | { |
| | | 975 | | get { return _headers.Mapping; } |
| | | 976 | | } |
| | | 977 | | |
| | | 978 | | internal override XmlSerializer HeaderSerializer |
| | | 979 | | { |
| | | 980 | | get { return _headers.GetSerializer(); } |
| | | 981 | | } |
| | | 982 | | |
| | | 983 | | internal override OperationFormatter.MessageHeaderDescriptionTable HeaderDescriptionTable |
| | | 984 | | { |
| | | 985 | | get { return _headerDescriptionTable; } |
| | | 986 | | } |
| | | 987 | | |
| | | 988 | | internal override MessageHeaderDescription UnknownHeaderDescription |
| | | 989 | | { |
| | | 990 | | get { return _unknownHeaderDescription; } |
| | | 991 | | } |
| | | 992 | | |
| | | 993 | | internal override MessagePartDescriptionCollection RpcEncodedTypedMessageBodyParts |
| | | 994 | | { |
| | | 995 | | get { return _rpcEncodedTypedMessageBodyParts; } |
| | | 996 | | } |
| | | 997 | | |
| | | 998 | | internal void SetBody(SerializerStub body, MessagePartDescriptionCollection rpcEncodedTypedMessageBodyPa |
| | | 999 | | { |
| | | 1000 | | _body = body; |
| | | 1001 | | _rpcEncodedTypedMessageBodyParts = rpcEncodedTypedMessageBodyParts; |
| | | 1002 | | } |
| | | 1003 | | |
| | | 1004 | | internal void SetHeaders(SerializerStub headers) |
| | | 1005 | | { |
| | | 1006 | | _headers = headers; |
| | | 1007 | | } |
| | | 1008 | | |
| | | 1009 | | internal void SetHeaderDescriptionTable(OperationFormatter.MessageHeaderDescriptionTable headerDescripti |
| | | 1010 | | { |
| | | 1011 | | _headerDescriptionTable = headerDescriptionTable; |
| | | 1012 | | } |
| | | 1013 | | |
| | | 1014 | | internal void SetUnknownHeaderDescription(MessageHeaderDescription unknownHeaderDescription) |
| | | 1015 | | { |
| | | 1016 | | _unknownHeaderDescription = unknownHeaderDescription; |
| | | 1017 | | } |
| | | 1018 | | } |
| | | 1019 | | } |
| | | 1020 | | } |
| | | 1021 | | |
| | | 1022 | | internal static class XmlSerializerHelper |
| | | 1023 | | { |
| | | 1024 | | internal static XmlReflectionMember GetXmlReflectionMember(MessagePartDescription part, bool isRpc, bool isEncod |
| | | 1025 | | { |
| | 144 | 1026 | | string ns = isRpc ? null : part.Namespace; |
| | 144 | 1027 | | ICustomAttributeProvider additionalAttributesProvider = null; |
| | 144 | 1028 | | if (isEncoded || part.AdditionalAttributesProvider is MemberInfo) |
| | 52 | 1029 | | additionalAttributesProvider = part.AdditionalAttributesProvider; |
| | | 1030 | | |
| | 144 | 1031 | | XmlName memberName = string.IsNullOrEmpty(part.UniquePartName) ? null : new XmlName(part.UniquePartName, tru |
| | 144 | 1032 | | XmlName elementName = part.XmlName; |
| | 144 | 1033 | | return GetXmlReflectionMember(memberName, elementName, ns, part.Type, additionalAttributesProvider, part.Mul |
| | | 1034 | | } |
| | | 1035 | | |
| | | 1036 | | internal static XmlReflectionMember GetXmlReflectionMember(XmlName memberName, XmlName elementName, string ns, T |
| | | 1037 | | { |
| | 144 | 1038 | | if (isEncoded && isMultiple) |
| | | 1039 | | { |
| | 0 | 1040 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.SFx |
| | | 1041 | | } |
| | | 1042 | | |
| | 144 | 1043 | | XmlReflectionMember member = new XmlReflectionMember |
| | 144 | 1044 | | { |
| | 144 | 1045 | | MemberName = (memberName ?? elementName).DecodedName, |
| | 144 | 1046 | | MemberType = type |
| | 144 | 1047 | | }; |
| | 144 | 1048 | | if (member.MemberType.IsByRef) |
| | | 1049 | | { |
| | 0 | 1050 | | member.MemberType = member.MemberType.GetElementType(); |
| | | 1051 | | } |
| | | 1052 | | |
| | 144 | 1053 | | if (isMultiple) |
| | | 1054 | | { |
| | 2 | 1055 | | member.MemberType = member.MemberType.MakeArrayType(); |
| | | 1056 | | } |
| | | 1057 | | |
| | 144 | 1058 | | if (additionalAttributesProvider != null) |
| | | 1059 | | { |
| | 52 | 1060 | | if (isEncoded) |
| | | 1061 | | { |
| | 34 | 1062 | | member.SoapAttributes = new SoapAttributes(additionalAttributesProvider); |
| | | 1063 | | } |
| | | 1064 | | else |
| | | 1065 | | { |
| | 18 | 1066 | | member.XmlAttributes = new XmlAttributes(additionalAttributesProvider); |
| | | 1067 | | } |
| | | 1068 | | } |
| | | 1069 | | |
| | 144 | 1070 | | if (isEncoded) |
| | | 1071 | | { |
| | 34 | 1072 | | if (member.SoapAttributes is null) |
| | | 1073 | | { |
| | 0 | 1074 | | member.SoapAttributes = new SoapAttributes(); |
| | | 1075 | | } |
| | | 1076 | | else |
| | | 1077 | | { |
| | 34 | 1078 | | Type invalidAttributeType = null; |
| | 34 | 1079 | | if (member.SoapAttributes.SoapAttribute != null) |
| | | 1080 | | { |
| | 0 | 1081 | | invalidAttributeType = typeof(SoapAttributeAttribute); |
| | | 1082 | | } |
| | 34 | 1083 | | else if (member.SoapAttributes.SoapIgnore) |
| | | 1084 | | { |
| | 0 | 1085 | | invalidAttributeType = typeof(SoapIgnoreAttribute); |
| | | 1086 | | } |
| | 34 | 1087 | | else if (member.SoapAttributes.SoapType != null) |
| | | 1088 | | { |
| | 0 | 1089 | | invalidAttributeType = typeof(SoapTypeAttribute); |
| | | 1090 | | } |
| | | 1091 | | |
| | 34 | 1092 | | if (invalidAttributeType != null) |
| | | 1093 | | { |
| | 0 | 1094 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma |
| | | 1095 | | } |
| | | 1096 | | } |
| | | 1097 | | |
| | 34 | 1098 | | if (member.SoapAttributes.SoapElement == null) |
| | | 1099 | | { |
| | 34 | 1100 | | member.SoapAttributes.SoapElement = new SoapElementAttribute(elementName.DecodedName); |
| | | 1101 | | } |
| | | 1102 | | } |
| | | 1103 | | else |
| | | 1104 | | { |
| | 110 | 1105 | | if (member.XmlAttributes is null) |
| | | 1106 | | { |
| | 0 | 1107 | | member.XmlAttributes = new XmlAttributes(); |
| | | 1108 | | } |
| | | 1109 | | else |
| | | 1110 | | { |
| | 110 | 1111 | | Type invalidAttributeType = null; |
| | 110 | 1112 | | if (member.XmlAttributes.XmlAttribute != null) |
| | | 1113 | | { |
| | 0 | 1114 | | invalidAttributeType = typeof(XmlAttributeAttribute); |
| | | 1115 | | } |
| | 110 | 1116 | | else if (member.XmlAttributes.XmlAnyAttribute != null && !isWrapped) |
| | | 1117 | | { |
| | 0 | 1118 | | invalidAttributeType = typeof(XmlAnyAttributeAttribute); |
| | | 1119 | | } |
| | 110 | 1120 | | else if (member.XmlAttributes.XmlChoiceIdentifier != null) |
| | | 1121 | | { |
| | 0 | 1122 | | invalidAttributeType = typeof(XmlChoiceIdentifierAttribute); |
| | | 1123 | | } |
| | 110 | 1124 | | else if (member.XmlAttributes.XmlIgnore) |
| | | 1125 | | { |
| | 0 | 1126 | | invalidAttributeType = typeof(XmlIgnoreAttribute); |
| | | 1127 | | } |
| | 110 | 1128 | | else if (member.XmlAttributes.Xmlns) |
| | | 1129 | | { |
| | 0 | 1130 | | invalidAttributeType = typeof(XmlNamespaceDeclarationsAttribute); |
| | | 1131 | | } |
| | 110 | 1132 | | else if (member.XmlAttributes.XmlText != null) |
| | | 1133 | | { |
| | 0 | 1134 | | invalidAttributeType = typeof(XmlTextAttribute); |
| | | 1135 | | } |
| | 110 | 1136 | | else if (member.XmlAttributes.XmlEnum != null) |
| | | 1137 | | { |
| | 0 | 1138 | | invalidAttributeType = typeof(XmlEnumAttribute); |
| | | 1139 | | } |
| | | 1140 | | |
| | 110 | 1141 | | if (invalidAttributeType != null) |
| | | 1142 | | { |
| | 0 | 1143 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma |
| | | 1144 | | } |
| | | 1145 | | |
| | 110 | 1146 | | if (member.XmlAttributes.XmlArray != null && isMultiple) |
| | | 1147 | | { |
| | 0 | 1148 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma |
| | | 1149 | | } |
| | | 1150 | | } |
| | | 1151 | | |
| | 110 | 1152 | | bool isArray = member.MemberType.IsArray; |
| | 110 | 1153 | | if ((isArray && !isMultiple && member.MemberType != typeof(byte[])) || |
| | 110 | 1154 | | (!isArray && typeof(IEnumerable).IsAssignableFrom(member.MemberType) && member.MemberType != typeof(s |
| | | 1155 | | { |
| | 0 | 1156 | | if (member.XmlAttributes.XmlArray != null) |
| | | 1157 | | { |
| | 0 | 1158 | | if (member.XmlAttributes.XmlArray.ElementName == string.Empty) |
| | | 1159 | | { |
| | 0 | 1160 | | member.XmlAttributes.XmlArray.ElementName = elementName.DecodedName; |
| | | 1161 | | } |
| | | 1162 | | |
| | 0 | 1163 | | if (member.XmlAttributes.XmlArray.Namespace == null) |
| | | 1164 | | { |
| | 0 | 1165 | | member.XmlAttributes.XmlArray.Namespace = ns; |
| | | 1166 | | } |
| | | 1167 | | } |
| | 0 | 1168 | | else if (HasNoXmlParameterAttributes(member.XmlAttributes)) |
| | | 1169 | | { |
| | 0 | 1170 | | member.XmlAttributes.XmlArray = new XmlArrayAttribute |
| | 0 | 1171 | | { |
| | 0 | 1172 | | ElementName = elementName.DecodedName, |
| | 0 | 1173 | | Namespace = ns |
| | 0 | 1174 | | }; |
| | | 1175 | | } |
| | | 1176 | | } |
| | | 1177 | | else |
| | | 1178 | | { |
| | 110 | 1179 | | if (member.XmlAttributes.XmlElements == null || member.XmlAttributes.XmlElements.Count == 0) |
| | | 1180 | | { |
| | 110 | 1181 | | if (HasNoXmlParameterAttributes(member.XmlAttributes)) |
| | | 1182 | | { |
| | 110 | 1183 | | XmlElementAttribute elementAttribute = new XmlElementAttribute |
| | 110 | 1184 | | { |
| | 110 | 1185 | | ElementName = elementName.DecodedName, |
| | 110 | 1186 | | Namespace = ns |
| | 110 | 1187 | | }; |
| | 110 | 1188 | | member.XmlAttributes.XmlElements.Add(elementAttribute); |
| | | 1189 | | } |
| | | 1190 | | } |
| | | 1191 | | else |
| | | 1192 | | { |
| | 0 | 1193 | | foreach (XmlElementAttribute elementAttribute in member.XmlAttributes.XmlElements) |
| | | 1194 | | { |
| | 0 | 1195 | | if (elementAttribute.ElementName == string.Empty) |
| | | 1196 | | { |
| | 0 | 1197 | | elementAttribute.ElementName = elementName.DecodedName; |
| | | 1198 | | } |
| | | 1199 | | |
| | 0 | 1200 | | if (elementAttribute.Namespace == null) |
| | | 1201 | | { |
| | 0 | 1202 | | elementAttribute.Namespace = ns; |
| | | 1203 | | } |
| | | 1204 | | } |
| | | 1205 | | } |
| | | 1206 | | } |
| | | 1207 | | } |
| | | 1208 | | |
| | 144 | 1209 | | return member; |
| | | 1210 | | } |
| | | 1211 | | |
| | | 1212 | | private static bool HasNoXmlParameterAttributes(XmlAttributes xmlAttributes) |
| | | 1213 | | { |
| | 110 | 1214 | | return xmlAttributes.XmlAnyAttribute == null && |
| | 110 | 1215 | | (xmlAttributes.XmlAnyElements == null || xmlAttributes.XmlAnyElements.Count == 0) && |
| | 110 | 1216 | | xmlAttributes.XmlArray == null && |
| | 110 | 1217 | | xmlAttributes.XmlAttribute == null && |
| | 110 | 1218 | | !xmlAttributes.XmlIgnore && |
| | 110 | 1219 | | xmlAttributes.XmlText == null && |
| | 110 | 1220 | | xmlAttributes.XmlChoiceIdentifier == null && |
| | 110 | 1221 | | (xmlAttributes.XmlElements == null || xmlAttributes.XmlElements.Count == 0) && |
| | 110 | 1222 | | !xmlAttributes.Xmlns; |
| | | 1223 | | } |
| | | 1224 | | } |
| | | 1225 | | } |