| | | 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.Runtime.Serialization; |
| | | 7 | | using CoreWCF.Description; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Dispatcher |
| | | 10 | | { |
| | | 11 | | internal class SingleBodyParameterXmlSerializerMessageFormatter : SingleBodyParameterMessageFormatter |
| | | 12 | | { |
| | | 13 | | private XmlObjectSerializer _cachedOutputSerializer; |
| | | 14 | | private Type _cachedOutputSerializerType; |
| | | 15 | | private readonly List<Type> _knownTypes; |
| | | 16 | | private readonly Type _parameterType; |
| | | 17 | | private readonly UnwrappedTypesXmlSerializerManager _serializerManager; |
| | | 18 | | private XmlObjectSerializer[] _serializers; |
| | | 19 | | private readonly object _thisLock; |
| | | 20 | | private UnwrappedTypesXmlSerializerManager.TypeSerializerPair[] _typeSerializerPairs; |
| | | 21 | | |
| | | 22 | | public SingleBodyParameterXmlSerializerMessageFormatter(OperationDescription operation, Type parameterType, bool |
| | 0 | 23 | | : base(operation, isRequestFormatter, "XmlSerializer") |
| | | 24 | | { |
| | 0 | 25 | | if (operation == null) |
| | | 26 | | { |
| | 0 | 27 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(operation)); |
| | | 28 | | } |
| | | 29 | | |
| | 0 | 30 | | if (xsob == null) |
| | | 31 | | { |
| | 0 | 32 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(xsob)); |
| | | 33 | | } |
| | | 34 | | |
| | 0 | 35 | | _serializerManager = serializerManager ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(n |
| | 0 | 36 | | _parameterType = parameterType ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(pa |
| | 0 | 37 | | List<Type> operationTypes = new List<Type>(); |
| | 0 | 38 | | operationTypes.Add(parameterType); |
| | 0 | 39 | | _knownTypes = new List<Type>(); |
| | 0 | 40 | | if (operation.KnownTypes != null) |
| | | 41 | | { |
| | 0 | 42 | | foreach (Type knownType in operation.KnownTypes) |
| | | 43 | | { |
| | 0 | 44 | | _knownTypes.Add(knownType); |
| | 0 | 45 | | operationTypes.Add(knownType); |
| | | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | 0 | 49 | | Type nullableType = SingleBodyParameterDataContractMessageFormatter.UnwrapNullableType(_parameterType); |
| | 0 | 50 | | if (nullableType != _parameterType) |
| | | 51 | | { |
| | 0 | 52 | | _knownTypes.Add(nullableType); |
| | 0 | 53 | | operationTypes.Add(nullableType); |
| | | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | _serializerManager.RegisterType(this, operationTypes); |
| | 0 | 57 | | _thisLock = new object(); |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | protected override XmlObjectSerializer[] GetInputSerializers() |
| | | 61 | | { |
| | 0 | 62 | | lock (_thisLock) |
| | | 63 | | { |
| | 0 | 64 | | EnsureSerializers(); |
| | 0 | 65 | | return _serializers; |
| | | 66 | | } |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | protected override XmlObjectSerializer GetOutputSerializer(Type type) |
| | | 70 | | { |
| | 0 | 71 | | lock (_thisLock) |
| | | 72 | | { |
| | 0 | 73 | | if (_cachedOutputSerializerType != type) |
| | | 74 | | { |
| | 0 | 75 | | Type typeForSerializer = GetTypeForSerializer(type, _parameterType, _knownTypes); |
| | 0 | 76 | | EnsureSerializers(); |
| | 0 | 77 | | bool foundSerializer = false; |
| | 0 | 78 | | if (_typeSerializerPairs != null) |
| | | 79 | | { |
| | 0 | 80 | | for (int i = 0; i < _typeSerializerPairs.Length; ++i) |
| | | 81 | | { |
| | 0 | 82 | | if (typeForSerializer == _typeSerializerPairs[i].Type) |
| | | 83 | | { |
| | 0 | 84 | | _cachedOutputSerializer = _typeSerializerPairs[i].Serializer; |
| | 0 | 85 | | _cachedOutputSerializerType = type; |
| | 0 | 86 | | foundSerializer = true; |
| | 0 | 87 | | break; |
| | | 88 | | } |
| | | 89 | | } |
| | | 90 | | } |
| | | 91 | | |
| | 0 | 92 | | if (!foundSerializer) |
| | | 93 | | { |
| | 0 | 94 | | return null; |
| | | 95 | | } |
| | | 96 | | } |
| | | 97 | | |
| | 0 | 98 | | return _cachedOutputSerializer; |
| | | 99 | | } |
| | 0 | 100 | | } |
| | | 101 | | |
| | | 102 | | // must be called under a lock |
| | | 103 | | private void EnsureSerializers() |
| | | 104 | | { |
| | 0 | 105 | | if (_typeSerializerPairs == null) |
| | | 106 | | { |
| | 0 | 107 | | _typeSerializerPairs = _serializerManager.GetOperationSerializers(this); |
| | 0 | 108 | | _serializers = new XmlObjectSerializer[_typeSerializerPairs.Length]; |
| | 0 | 109 | | for (int i = 0; i < _typeSerializerPairs.Length; ++i) |
| | | 110 | | { |
| | 0 | 111 | | _serializers[i] = _typeSerializerPairs[i].Serializer; |
| | | 112 | | } |
| | | 113 | | } |
| | 0 | 114 | | } |
| | | 115 | | } |
| | | 116 | | } |