| | | 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.Linq; |
| | | 8 | | using System.Runtime.Serialization; |
| | | 9 | | using System.Runtime.Serialization.Json; |
| | | 10 | | using CoreWCF.Channels; |
| | | 11 | | using CoreWCF.Description; |
| | | 12 | | |
| | | 13 | | namespace CoreWCF.Dispatcher |
| | | 14 | | { |
| | | 15 | | internal class SingleBodyParameterDataContractMessageFormatter : SingleBodyParameterMessageFormatter |
| | | 16 | | { |
| | 1 | 17 | | private static readonly Type s_typeOfNullable = typeof(Nullable<>); |
| | 1 | 18 | | private static readonly Type[] s_collectionDataContractInterfaces = new Type[] { typeof(IEnumerable), typeof(ILi |
| | 1 | 19 | | private static readonly Type[] s_genericCollectionDataContractInterfaces = new Type[] { typeof(IEnumerable<>), t |
| | | 20 | | private XmlObjectSerializer _cachedOutputSerializer; |
| | | 21 | | private Type _cachedOutputSerializerType; |
| | | 22 | | private readonly bool _ignoreExtensionData; |
| | | 23 | | private XmlObjectSerializer[] _inputSerializers; |
| | | 24 | | private readonly IList<Type> _knownTypes; |
| | | 25 | | private readonly int _maxItemsInObjectGraph; |
| | | 26 | | private readonly Type _parameterDataContractType; |
| | | 27 | | private readonly object _thisLock; |
| | | 28 | | private readonly bool _useJsonFormat; |
| | | 29 | | private readonly bool _isParameterCollectionInterfaceDataContract; |
| | | 30 | | private readonly bool _isQueryable; |
| | | 31 | | |
| | | 32 | | public SingleBodyParameterDataContractMessageFormatter(OperationDescription operation, Type parameterType, bool |
| | 272 | 33 | | : base(operation, isRequestFormatter, "DataContractSerializer") |
| | | 34 | | { |
| | 272 | 35 | | if (operation == null) |
| | | 36 | | { |
| | 0 | 37 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(operation)); |
| | | 38 | | } |
| | | 39 | | |
| | 272 | 40 | | if (parameterType == null) |
| | | 41 | | { |
| | 0 | 42 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parameterType)); |
| | | 43 | | } |
| | | 44 | | |
| | 272 | 45 | | if (dcsob == null) |
| | | 46 | | { |
| | 0 | 47 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dcsob)); |
| | | 48 | | } |
| | | 49 | | |
| | 272 | 50 | | _parameterDataContractType = DataContractHelpers.GetSubstituteDataContractType(parameterType, out _isQueryab |
| | 272 | 51 | | _isParameterCollectionInterfaceDataContract = IsTypeCollectionInterface(_parameterDataContractType); |
| | 272 | 52 | | List<Type> tmp = new List<Type>(); |
| | 272 | 53 | | if (operation.KnownTypes != null) |
| | | 54 | | { |
| | 544 | 55 | | foreach (Type knownType in operation.KnownTypes) |
| | | 56 | | { |
| | 0 | 57 | | tmp.Add(knownType); |
| | | 58 | | } |
| | | 59 | | } |
| | | 60 | | |
| | 272 | 61 | | Type nullableType = UnwrapNullableType(_parameterDataContractType); |
| | 272 | 62 | | if (nullableType != _parameterDataContractType) |
| | | 63 | | { |
| | 0 | 64 | | tmp.Add(nullableType); |
| | | 65 | | } |
| | | 66 | | |
| | 272 | 67 | | _ignoreExtensionData = dcsob.IgnoreExtensionDataObject; |
| | 272 | 68 | | _maxItemsInObjectGraph = dcsob.MaxItemsInObjectGraph; |
| | 272 | 69 | | _knownTypes = tmp.AsReadOnly(); |
| | 272 | 70 | | ValidateType(_parameterDataContractType, _knownTypes); |
| | | 71 | | |
| | 272 | 72 | | _useJsonFormat = useJsonFormat; |
| | 272 | 73 | | CreateInputSerializers(_parameterDataContractType); |
| | | 74 | | |
| | 272 | 75 | | _thisLock = new object(); |
| | 272 | 76 | | } |
| | | 77 | | |
| | | 78 | | internal static Type UnwrapNullableType(Type type) |
| | | 79 | | { |
| | 272 | 80 | | while (type.IsGenericType && type.GetGenericTypeDefinition() == s_typeOfNullable) |
| | | 81 | | { |
| | 0 | 82 | | type = type.GetGenericArguments()[0]; |
| | | 83 | | } |
| | | 84 | | |
| | 272 | 85 | | return type; |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | // The logic of this method should be kept the same as |
| | | 89 | | // System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.PartInfo.ReadObject |
| | | 90 | | protected override object ReadObject(Message message) |
| | | 91 | | { |
| | 5 | 92 | | object val = base.ReadObject(message); |
| | 5 | 93 | | if (_isQueryable && val != null) |
| | | 94 | | { |
| | 0 | 95 | | return Queryable.AsQueryable((IEnumerable)val); |
| | | 96 | | } |
| | | 97 | | |
| | 5 | 98 | | return val; |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | protected override void AttachMessageProperties(Message message, bool isRequest) |
| | | 102 | | { |
| | 26 | 103 | | if (_useJsonFormat) |
| | | 104 | | { |
| | 24 | 105 | | message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.JsonProperty); |
| | | 106 | | } |
| | 26 | 107 | | } |
| | | 108 | | |
| | | 109 | | protected override XmlObjectSerializer[] GetInputSerializers() |
| | | 110 | | { |
| | 5 | 111 | | return _inputSerializers; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | protected override XmlObjectSerializer GetOutputSerializer(Type type) |
| | | 115 | | { |
| | 26 | 116 | | lock (_thisLock) |
| | | 117 | | { |
| | | 118 | | // if we already have a serializer for this type reuse it |
| | 26 | 119 | | if (_cachedOutputSerializerType != type) |
| | | 120 | | { |
| | | 121 | | Type typeForSerializer; |
| | 26 | 122 | | if (_isParameterCollectionInterfaceDataContract) |
| | | 123 | | { |
| | | 124 | | // if the parameterType is a collection interface, ensure the type implements it |
| | 0 | 125 | | if (!_parameterDataContractType.IsAssignableFrom(type)) |
| | | 126 | | { |
| | 0 | 127 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(SR.Form |
| | | 128 | | } |
| | 0 | 129 | | typeForSerializer = _parameterDataContractType; |
| | | 130 | | } |
| | | 131 | | else |
| | | 132 | | { |
| | 26 | 133 | | typeForSerializer = GetTypeForSerializer(type, _parameterDataContractType, _knownTypes); |
| | | 134 | | } |
| | 26 | 135 | | _cachedOutputSerializer = CreateSerializer(typeForSerializer); |
| | 26 | 136 | | _cachedOutputSerializerType = type; |
| | | 137 | | } |
| | | 138 | | |
| | 26 | 139 | | return _cachedOutputSerializer; |
| | | 140 | | } |
| | 26 | 141 | | } |
| | | 142 | | |
| | | 143 | | private static bool IsTypeCollectionInterface(Type parameterType) |
| | | 144 | | { |
| | 272 | 145 | | if (parameterType.IsGenericType && parameterType.IsInterface) |
| | | 146 | | { |
| | 0 | 147 | | Type genericTypeDef = parameterType.GetGenericTypeDefinition(); |
| | 0 | 148 | | foreach (Type type in s_genericCollectionDataContractInterfaces) |
| | | 149 | | { |
| | 0 | 150 | | if (genericTypeDef == type) |
| | | 151 | | { |
| | 0 | 152 | | return true; |
| | | 153 | | } |
| | | 154 | | } |
| | | 155 | | } |
| | | 156 | | |
| | 2720 | 157 | | foreach (Type type in s_collectionDataContractInterfaces) |
| | | 158 | | { |
| | 1088 | 159 | | if (parameterType == type) |
| | | 160 | | { |
| | 0 | 161 | | return true; |
| | | 162 | | } |
| | | 163 | | } |
| | | 164 | | |
| | 272 | 165 | | return false; |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | protected override void ValidateMessageFormatProperty(Message message) |
| | | 169 | | { |
| | 0 | 170 | | if (_useJsonFormat) |
| | | 171 | | { |
| | | 172 | | // useJsonFormat is always false in the green bits |
| | 0 | 173 | | message.Properties.TryGetValue(WebBodyFormatMessageProperty.Name, out object prop); |
| | 0 | 174 | | WebBodyFormatMessageProperty formatProperty = (prop as WebBodyFormatMessageProperty); |
| | 0 | 175 | | if (formatProperty == null) |
| | | 176 | | { |
| | 0 | 177 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format( |
| | | 178 | | } |
| | 0 | 179 | | if (formatProperty.Format != WebContentFormat.Json) |
| | | 180 | | { |
| | 0 | 181 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format( |
| | | 182 | | } |
| | | 183 | | } |
| | | 184 | | else |
| | | 185 | | { |
| | 0 | 186 | | base.ValidateMessageFormatProperty(message); |
| | | 187 | | } |
| | 0 | 188 | | } |
| | | 189 | | |
| | | 190 | | private static void ValidateType(Type parameterType, IEnumerable<Type> knownTypes) |
| | | 191 | | { |
| | 272 | 192 | | XsdDataContractExporter dataContractExporter = new XsdDataContractExporter(); |
| | 272 | 193 | | if (knownTypes != null) |
| | | 194 | | { |
| | 272 | 195 | | ExportOptions options = new ExportOptions(); |
| | | 196 | | |
| | 544 | 197 | | foreach (Type knownType in knownTypes) |
| | | 198 | | { |
| | 0 | 199 | | options.KnownTypes.Add(knownType); |
| | | 200 | | } |
| | | 201 | | |
| | 272 | 202 | | dataContractExporter.Options = options; |
| | | 203 | | } |
| | | 204 | | |
| | 272 | 205 | | dataContractExporter.GetSchemaTypeName(parameterType); // throws if parameterType is not a valid data contra |
| | 272 | 206 | | } |
| | | 207 | | |
| | | 208 | | private void CreateInputSerializers(Type type) |
| | | 209 | | { |
| | 272 | 210 | | List<XmlObjectSerializer> tmp = new List<XmlObjectSerializer>(); |
| | 272 | 211 | | tmp.Add(CreateSerializer(type)); |
| | 544 | 212 | | foreach (Type knownType in _knownTypes) |
| | | 213 | | { |
| | 0 | 214 | | tmp.Add(CreateSerializer(knownType)); |
| | | 215 | | } |
| | | 216 | | |
| | 272 | 217 | | _inputSerializers = tmp.ToArray(); |
| | 272 | 218 | | } |
| | | 219 | | |
| | | 220 | | private XmlObjectSerializer CreateSerializer(Type type) |
| | | 221 | | { |
| | 298 | 222 | | if (_useJsonFormat) |
| | | 223 | | { |
| | 160 | 224 | | return new DataContractJsonSerializer(type, new DataContractJsonSerializerSettings |
| | 160 | 225 | | { |
| | 160 | 226 | | KnownTypes = _knownTypes, |
| | 160 | 227 | | MaxItemsInObjectGraph = _maxItemsInObjectGraph, |
| | 160 | 228 | | IgnoreExtensionDataObject = _ignoreExtensionData, |
| | 160 | 229 | | EmitTypeInformation = EmitTypeInformation.AsNeeded |
| | 160 | 230 | | }); |
| | | 231 | | } |
| | | 232 | | else |
| | | 233 | | { |
| | 138 | 234 | | return new DataContractSerializer(type, new DataContractSerializerSettings |
| | 138 | 235 | | { |
| | 138 | 236 | | KnownTypes = _knownTypes, |
| | 138 | 237 | | MaxItemsInObjectGraph = _maxItemsInObjectGraph, |
| | 138 | 238 | | IgnoreExtensionDataObject = _ignoreExtensionData, |
| | 138 | 239 | | PreserveObjectReferences = false |
| | 138 | 240 | | }); |
| | | 241 | | } |
| | | 242 | | } |
| | | 243 | | } |
| | | 244 | | } |