| | | 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 System.Xml; |
| | | 8 | | using CoreWCF.Channels; |
| | | 9 | | using CoreWCF.Collections.Generic; |
| | | 10 | | using CoreWCF.Description; |
| | | 11 | | using CoreWCF.Web; |
| | | 12 | | |
| | | 13 | | namespace CoreWCF.Dispatcher |
| | | 14 | | { |
| | | 15 | | internal abstract class SingleBodyParameterMessageFormatter : IDispatchMessageFormatter |
| | | 16 | | { |
| | | 17 | | private readonly bool _isRequestFormatter; |
| | | 18 | | private readonly string _serializerType; |
| | | 19 | | |
| | 272 | 20 | | protected SingleBodyParameterMessageFormatter(OperationDescription operation, bool isRequestFormatter, string se |
| | | 21 | | { |
| | 272 | 22 | | if (operation == null) |
| | | 23 | | { |
| | 0 | 24 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(operation)); |
| | | 25 | | } |
| | | 26 | | |
| | 272 | 27 | | ContractName = operation.DeclaringContract.Name; |
| | 272 | 28 | | ContractNs = operation.DeclaringContract.Namespace; |
| | 272 | 29 | | OperationName = operation.Name; |
| | 272 | 30 | | _isRequestFormatter = isRequestFormatter; |
| | 272 | 31 | | _serializerType = serializerType; |
| | 272 | 32 | | } |
| | | 33 | | |
| | 0 | 34 | | protected string ContractName { get; } |
| | | 35 | | |
| | 0 | 36 | | protected string ContractNs { get; } |
| | | 37 | | |
| | 0 | 38 | | protected string OperationName { get; } |
| | | 39 | | |
| | | 40 | | public static IDispatchMessageFormatter CreateXmlAndJsonDispatchFormatter(OperationDescription operation, Type t |
| | | 41 | | { |
| | 160 | 42 | | IDispatchMessageFormatter xmlFormatter = CreateDispatchFormatter(operation, type, isRequestFormatter, false, |
| | 160 | 43 | | if (!WebHttpBehavior.SupportsJsonFormat(operation)) |
| | | 44 | | { |
| | 0 | 45 | | return xmlFormatter; |
| | | 46 | | } |
| | | 47 | | |
| | 160 | 48 | | IDispatchMessageFormatter jsonFormatter = CreateDispatchFormatter(operation, type, isRequestFormatter, true, |
| | 160 | 49 | | Dictionary<WebContentFormat, IDispatchMessageFormatter> map = new Dictionary<WebContentFormat, IDispatchMess |
| | 160 | 50 | | { |
| | 160 | 51 | | { WebContentFormat.Xml, xmlFormatter }, |
| | 160 | 52 | | { WebContentFormat.Json, jsonFormatter } |
| | 160 | 53 | | }; |
| | | 54 | | |
| | 160 | 55 | | return new DemultiplexingDispatchMessageFormatter(map, xmlFormatter); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | public void DeserializeRequest(Message message, object[] parameters) |
| | | 59 | | { |
| | 5 | 60 | | if (!_isRequestFormatter) |
| | | 61 | | { |
| | 0 | 62 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.For |
| | | 63 | | } |
| | | 64 | | |
| | 5 | 65 | | parameters[0] = ReadObject(message); |
| | 5 | 66 | | } |
| | | 67 | | |
| | | 68 | | public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result) |
| | | 69 | | { |
| | 26 | 70 | | if (_isRequestFormatter) |
| | | 71 | | { |
| | 0 | 72 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.For |
| | | 73 | | } |
| | | 74 | | |
| | 26 | 75 | | Message message = Message.CreateMessage(messageVersion, (string)null, CreateBodyWriter(result)); |
| | 26 | 76 | | if (result == null) |
| | | 77 | | { |
| | 0 | 78 | | SuppressReplyEntityBody(message); |
| | | 79 | | } |
| | | 80 | | |
| | 26 | 81 | | AttachMessageProperties(message, false); |
| | | 82 | | |
| | 26 | 83 | | return message; |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | internal static IDispatchMessageFormatter CreateDispatchFormatter(OperationDescription operation, Type type, boo |
| | | 87 | | { |
| | 640 | 88 | | if (type == null) |
| | | 89 | | { |
| | 368 | 90 | | return new NullMessageFormatter(); |
| | | 91 | | } |
| | 272 | 92 | | else if (useJson) |
| | | 93 | | { |
| | 136 | 94 | | return CreateJsonFormatter(operation, type, isRequestFormatter); |
| | | 95 | | } |
| | | 96 | | else |
| | | 97 | | { |
| | 136 | 98 | | return CreateXmlFormatter(operation, type, isRequestFormatter, xmlSerializerManager); |
| | | 99 | | } |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | internal static void SuppressReplyEntityBody(Message message) |
| | | 103 | | { |
| | 4 | 104 | | WebOperationContext currentContext = WebOperationContext.Current; |
| | 4 | 105 | | if (currentContext != null) |
| | | 106 | | { |
| | 4 | 107 | | OutgoingWebResponseContext responseContext = currentContext.OutgoingResponse; |
| | 4 | 108 | | if (responseContext != null) |
| | | 109 | | { |
| | 4 | 110 | | responseContext.SuppressEntityBody = true; |
| | | 111 | | } |
| | | 112 | | } |
| | | 113 | | else |
| | | 114 | | { |
| | 0 | 115 | | message.Properties.TryGetValue(HttpResponseMessageProperty.Name, out object untypedProp); |
| | 0 | 116 | | if (!(untypedProp is HttpResponseMessageProperty prop)) |
| | | 117 | | { |
| | 0 | 118 | | prop = new HttpResponseMessageProperty(); |
| | 0 | 119 | | message.Properties[HttpResponseMessageProperty.Name] = prop; |
| | | 120 | | } |
| | 0 | 121 | | prop.SuppressEntityBody = true; |
| | | 122 | | } |
| | 0 | 123 | | } |
| | | 124 | | |
| | | 125 | | protected virtual void AttachMessageProperties(Message message, bool isRequest) |
| | | 126 | | { |
| | 0 | 127 | | } |
| | | 128 | | |
| | | 129 | | protected abstract XmlObjectSerializer[] GetInputSerializers(); |
| | | 130 | | |
| | | 131 | | protected abstract XmlObjectSerializer GetOutputSerializer(Type type); |
| | | 132 | | |
| | | 133 | | protected virtual void ValidateMessageFormatProperty(Message message) |
| | | 134 | | { |
| | 0 | 135 | | } |
| | | 136 | | |
| | | 137 | | protected Type GetTypeForSerializer(Type type, Type parameterType, IList<Type> knownTypes) |
| | | 138 | | { |
| | 26 | 139 | | if (type == parameterType) |
| | | 140 | | { |
| | 26 | 141 | | return type; |
| | | 142 | | } |
| | 0 | 143 | | else if (knownTypes != null) |
| | | 144 | | { |
| | 0 | 145 | | for (int i = 0; i < knownTypes.Count; ++i) |
| | | 146 | | { |
| | 0 | 147 | | if (type == knownTypes[i]) |
| | | 148 | | { |
| | 0 | 149 | | return type; |
| | | 150 | | } |
| | | 151 | | } |
| | | 152 | | } |
| | | 153 | | |
| | 0 | 154 | | return parameterType; |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | public static SingleBodyParameterMessageFormatter CreateXmlFormatter(OperationDescription operation, Type type, |
| | | 158 | | { |
| | 136 | 159 | | DataContractSerializerOperationBehavior dcsob = ((KeyedByTypeCollection<IOperationBehavior>)operation.Operat |
| | 136 | 160 | | if (dcsob != null) |
| | | 161 | | { |
| | 136 | 162 | | return new SingleBodyParameterDataContractMessageFormatter(operation, type, isRequestFormatter, false, d |
| | | 163 | | } |
| | | 164 | | |
| | 0 | 165 | | XmlSerializerOperationBehavior xsob = ((KeyedByTypeCollection<IOperationBehavior>)operation.OperationBehavio |
| | 0 | 166 | | if (xsob != null) |
| | | 167 | | { |
| | 0 | 168 | | return new SingleBodyParameterXmlSerializerMessageFormatter(operation, type, isRequestFormatter, xsob, x |
| | | 169 | | } |
| | | 170 | | |
| | 0 | 171 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Format(SR.OnlyDataCon |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | public static SingleBodyParameterMessageFormatter CreateJsonFormatter(OperationDescription operation, Type type, |
| | | 175 | | { |
| | 136 | 176 | | DataContractSerializerOperationBehavior dcsob = ((KeyedByTypeCollection<IOperationBehavior>)operation.Operat |
| | 136 | 177 | | if (dcsob == null) |
| | | 178 | | { |
| | 0 | 179 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Jso |
| | | 180 | | } |
| | | 181 | | |
| | 136 | 182 | | return new SingleBodyParameterDataContractMessageFormatter(operation, type, isRequestFormatter, true, dcsob) |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | private BodyWriter CreateBodyWriter(object body) |
| | | 186 | | { |
| | | 187 | | XmlObjectSerializer serializer; |
| | 26 | 188 | | if (body != null) |
| | | 189 | | { |
| | 26 | 190 | | serializer = GetOutputSerializer(body.GetType()); |
| | 26 | 191 | | if (serializer == null) |
| | | 192 | | { |
| | 0 | 193 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Format(SR.Can |
| | | 194 | | } |
| | | 195 | | } |
| | | 196 | | else |
| | | 197 | | { |
| | 0 | 198 | | serializer = null; |
| | | 199 | | } |
| | | 200 | | |
| | 26 | 201 | | return new SingleParameterBodyWriter(body, serializer); |
| | | 202 | | } |
| | | 203 | | |
| | | 204 | | protected virtual object ReadObject(Message message) |
| | | 205 | | { |
| | 5 | 206 | | if (HttpStreamFormatter.IsEmptyMessage(message)) |
| | | 207 | | { |
| | 0 | 208 | | return null; |
| | | 209 | | } |
| | | 210 | | |
| | 5 | 211 | | XmlObjectSerializer[] inputSerializers = GetInputSerializers(); |
| | 5 | 212 | | XmlDictionaryReader reader = message.GetReaderAtBodyContents(); |
| | 5 | 213 | | if (inputSerializers != null) |
| | | 214 | | { |
| | 10 | 215 | | for (int i = 0; i < inputSerializers.Length; ++i) |
| | | 216 | | { |
| | 5 | 217 | | if (inputSerializers[i].IsStartObject(reader)) |
| | | 218 | | { |
| | 5 | 219 | | return inputSerializers[i].ReadObject(reader, false); |
| | | 220 | | } |
| | | 221 | | } |
| | | 222 | | } |
| | | 223 | | |
| | 0 | 224 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(SR.Format(SR.CannotDese |
| | | 225 | | } |
| | | 226 | | |
| | | 227 | | internal class NullMessageFormatter : IDispatchMessageFormatter |
| | | 228 | | { |
| | | 229 | | public void DeserializeRequest(Message message, object[] parameters) |
| | | 230 | | { |
| | 0 | 231 | | } |
| | | 232 | | |
| | | 233 | | public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result) |
| | | 234 | | { |
| | 4 | 235 | | Message reply = Message.CreateMessage(messageVersion, null); |
| | 4 | 236 | | SuppressReplyEntityBody(reply); |
| | | 237 | | |
| | 4 | 238 | | return reply; |
| | | 239 | | } |
| | | 240 | | } |
| | | 241 | | |
| | | 242 | | internal class SingleParameterBodyWriter : BodyWriter |
| | | 243 | | { |
| | | 244 | | private readonly object _body; |
| | | 245 | | private readonly XmlObjectSerializer _serializer; |
| | | 246 | | |
| | | 247 | | public SingleParameterBodyWriter(object body, XmlObjectSerializer serializer) |
| | 26 | 248 | | : base(false) |
| | | 249 | | { |
| | 26 | 250 | | if (body != null && serializer == null) |
| | | 251 | | { |
| | 0 | 252 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(serializer)); |
| | | 253 | | } |
| | | 254 | | |
| | 26 | 255 | | _body = body; |
| | 26 | 256 | | _serializer = serializer; |
| | 26 | 257 | | } |
| | | 258 | | |
| | | 259 | | protected override void OnWriteBodyContents(XmlDictionaryWriter writer) |
| | | 260 | | { |
| | 26 | 261 | | if (_body != null) |
| | | 262 | | { |
| | 26 | 263 | | _serializer.WriteObject(writer, _body); |
| | | 264 | | } |
| | 26 | 265 | | } |
| | | 266 | | } |
| | | 267 | | } |
| | | 268 | | } |