| | | 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.Reflection; |
| | | 7 | | using System.Runtime.Serialization; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using System.Xml; |
| | | 10 | | using CoreWCF.Channels; |
| | | 11 | | using CoreWCF.Description; |
| | | 12 | | using CoreWCF.Diagnostics; |
| | | 13 | | |
| | | 14 | | namespace CoreWCF.Dispatcher |
| | | 15 | | { |
| | | 16 | | public abstract class OperationFormatter : IClientMessageFormatter, IDispatchMessageFormatter |
| | | 17 | | { |
| | | 18 | | private readonly XmlDictionaryString _action; |
| | | 19 | | private readonly XmlDictionaryString _replyAction; |
| | | 20 | | internal StreamFormatter requestStreamFormatter, replyStreamFormatter; |
| | | 21 | | |
| | 867 | 22 | | public OperationFormatter(OperationDescription description, bool isRpc, bool isEncoded) |
| | | 23 | | { |
| | 867 | 24 | | Validate(description, isRpc, isEncoded); |
| | 867 | 25 | | RequestDescription = description.Messages[0]; |
| | 867 | 26 | | if (description.Messages.Count == 2) |
| | | 27 | | { |
| | 863 | 28 | | ReplyDescription = description.Messages[1]; |
| | | 29 | | } |
| | | 30 | | |
| | 867 | 31 | | int stringCount = 3 + RequestDescription.Body.Parts.Count; |
| | 867 | 32 | | if (ReplyDescription != null) |
| | | 33 | | { |
| | 863 | 34 | | stringCount += 2 + ReplyDescription.Body.Parts.Count; |
| | | 35 | | } |
| | | 36 | | |
| | 867 | 37 | | Dictionary = new XmlDictionary(stringCount * 2); |
| | 867 | 38 | | GetActions(description, Dictionary, out _action, out _replyAction); |
| | 867 | 39 | | OperationName = description.Name; |
| | 867 | 40 | | requestStreamFormatter = StreamFormatter.Create(RequestDescription, OperationName, true/*isRequest*/); |
| | 867 | 41 | | if (ReplyDescription != null) |
| | | 42 | | { |
| | 863 | 43 | | replyStreamFormatter = StreamFormatter.Create(ReplyDescription, OperationName, false/*isResponse*/); |
| | | 44 | | } |
| | 867 | 45 | | } |
| | | 46 | | |
| | | 47 | | protected abstract void AddHeadersToMessage(Message message, MessageDescription messageDescription, object[] par |
| | | 48 | | protected abstract void SerializeBody(XmlDictionaryWriter writer, MessageVersion version, string action, Message |
| | | 49 | | protected abstract void GetHeadersFromMessage(Message message, MessageDescription messageDescription, object[] p |
| | | 50 | | protected abstract object DeserializeBody(XmlDictionaryReader reader, MessageVersion version, string action, Mes |
| | | 51 | | |
| | | 52 | | protected virtual void WriteBodyAttributes(XmlDictionaryWriter writer, MessageVersion messageVersion) |
| | | 53 | | { |
| | 146 | 54 | | } |
| | | 55 | | |
| | | 56 | | internal string RequestAction |
| | | 57 | | { |
| | | 58 | | get |
| | | 59 | | { |
| | 312 | 60 | | if (_action != null) |
| | | 61 | | { |
| | 312 | 62 | | return _action.Value; |
| | | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | return null; |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | internal string ReplyAction |
| | | 69 | | { |
| | | 70 | | get |
| | | 71 | | { |
| | 0 | 72 | | if (_replyAction != null) |
| | | 73 | | { |
| | 0 | 74 | | return _replyAction.Value; |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | return null; |
| | | 78 | | } |
| | | 79 | | } |
| | | 80 | | |
| | 7819 | 81 | | protected XmlDictionary Dictionary { get; } |
| | | 82 | | |
| | 1730 | 83 | | protected string OperationName { get; } |
| | | 84 | | |
| | 5771 | 85 | | protected MessageDescription ReplyDescription { get; } |
| | | 86 | | |
| | 3285 | 87 | | protected MessageDescription RequestDescription { get; } |
| | | 88 | | |
| | | 89 | | protected XmlDictionaryString AddToDictionary(string s) |
| | | 90 | | { |
| | 6952 | 91 | | return AddToDictionary(Dictionary, s); |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | public object DeserializeReply(Message message, object[] parameters) |
| | | 95 | | { |
| | 0 | 96 | | if (message == null) |
| | | 97 | | { |
| | 0 | 98 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(message)); |
| | | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | if (parameters == null) |
| | | 102 | | { |
| | 0 | 103 | | throw TraceUtility.ThrowHelperError(new ArgumentNullException(nameof(parameters)), message); |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | try |
| | | 107 | | { |
| | 0 | 108 | | object result = null; |
| | 0 | 109 | | if (ReplyDescription.IsTypedMessage) |
| | | 110 | | { |
| | 0 | 111 | | object typeMessageInstance = CreateTypedMessageInstance(ReplyDescription.MessageType); |
| | 0 | 112 | | TypedMessageParts typedMessageParts = new TypedMessageParts(typeMessageInstance, ReplyDescription); |
| | 0 | 113 | | object[] parts = new object[typedMessageParts.Count]; |
| | | 114 | | |
| | 0 | 115 | | GetPropertiesFromMessage(message, ReplyDescription, parts); |
| | 0 | 116 | | GetHeadersFromMessage(message, ReplyDescription, parts, false/*isRequest*/); |
| | 0 | 117 | | DeserializeBodyContents(message, parts, false/*isRequest*/); |
| | | 118 | | |
| | | 119 | | // copy values into the actual field/properties |
| | 0 | 120 | | typedMessageParts.SetTypedMessageParts(parts); |
| | | 121 | | |
| | 0 | 122 | | result = typeMessageInstance; |
| | | 123 | | } |
| | | 124 | | else |
| | | 125 | | { |
| | 0 | 126 | | GetPropertiesFromMessage(message, ReplyDescription, parameters); |
| | 0 | 127 | | GetHeadersFromMessage(message, ReplyDescription, parameters, false/*isRequest*/); |
| | 0 | 128 | | result = DeserializeBodyContents(message, parameters, false/*isRequest*/); |
| | | 129 | | } |
| | 0 | 130 | | return result; |
| | | 131 | | } |
| | 0 | 132 | | catch (XmlException xe) |
| | | 133 | | { |
| | 0 | 134 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException( |
| | 0 | 135 | | SR.Format(SR.SFxErrorDeserializingReplyBodyMore, OperationName, xe.Message), xe)); |
| | | 136 | | } |
| | 0 | 137 | | catch (FormatException fe) |
| | | 138 | | { |
| | 0 | 139 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException( |
| | 0 | 140 | | SR.Format(SR.SFxErrorDeserializingReplyBodyMore, OperationName, fe.Message), fe)); |
| | | 141 | | } |
| | 0 | 142 | | catch (SerializationException se) |
| | | 143 | | { |
| | 0 | 144 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException( |
| | 0 | 145 | | SR.Format(SR.SFxErrorDeserializingReplyBodyMore, OperationName, se.Message), se)); |
| | | 146 | | } |
| | 0 | 147 | | } |
| | | 148 | | |
| | | 149 | | private static object CreateTypedMessageInstance(Type messageContractType) |
| | | 150 | | { |
| | | 151 | | try |
| | | 152 | | { |
| | 17 | 153 | | object typeMessageInstance = Activator.CreateInstance(messageContractType); |
| | 17 | 154 | | return typeMessageInstance; |
| | | 155 | | } |
| | 0 | 156 | | catch (MissingMethodException mme) |
| | | 157 | | { |
| | 0 | 158 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.SFx |
| | | 159 | | } |
| | 17 | 160 | | } |
| | | 161 | | |
| | | 162 | | public void DeserializeRequest(Message message, object[] parameters) |
| | | 163 | | { |
| | 175 | 164 | | if (message == null) |
| | | 165 | | { |
| | 0 | 166 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(message)); |
| | | 167 | | } |
| | | 168 | | |
| | 175 | 169 | | if (parameters == null) |
| | | 170 | | { |
| | 0 | 171 | | throw TraceUtility.ThrowHelperError(new ArgumentNullException(nameof(parameters)), message); |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | try |
| | | 175 | | { |
| | 175 | 176 | | if (RequestDescription.IsTypedMessage) |
| | | 177 | | { |
| | 17 | 178 | | object typeMessageInstance = CreateTypedMessageInstance(RequestDescription.MessageType); |
| | 17 | 179 | | TypedMessageParts typedMessageParts = new TypedMessageParts(typeMessageInstance, RequestDescription) |
| | 17 | 180 | | object[] parts = new object[typedMessageParts.Count]; |
| | | 181 | | |
| | 17 | 182 | | GetPropertiesFromMessage(message, RequestDescription, parts); |
| | 17 | 183 | | GetHeadersFromMessage(message, RequestDescription, parts, true/*isRequest*/); |
| | 17 | 184 | | DeserializeBodyContents(message, parts, true/*isRequest*/); |
| | | 185 | | |
| | | 186 | | // copy values into the actual field/properties |
| | 17 | 187 | | typedMessageParts.SetTypedMessageParts(parts); |
| | | 188 | | |
| | 17 | 189 | | parameters[0] = typeMessageInstance; |
| | | 190 | | } |
| | | 191 | | else |
| | | 192 | | { |
| | 158 | 193 | | GetPropertiesFromMessage(message, RequestDescription, parameters); |
| | 158 | 194 | | GetHeadersFromMessage(message, RequestDescription, parameters, true/*isRequest*/); |
| | 158 | 195 | | DeserializeBodyContents(message, parameters, true/*isRequest*/); |
| | | 196 | | } |
| | 175 | 197 | | } |
| | 0 | 198 | | catch (XmlException xe) |
| | | 199 | | { |
| | 0 | 200 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 201 | | NetDispatcherFaultException.CreateDeserializationFailedFault( |
| | 0 | 202 | | SR.Format(SR.SFxErrorDeserializingRequestBodyMore, OperationName, xe.Message), |
| | 0 | 203 | | xe)); |
| | | 204 | | } |
| | 0 | 205 | | catch (FormatException fe) |
| | | 206 | | { |
| | 0 | 207 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 208 | | NetDispatcherFaultException.CreateDeserializationFailedFault( |
| | 0 | 209 | | SR.Format(SR.SFxErrorDeserializingRequestBodyMore, OperationName, fe.Message), |
| | 0 | 210 | | fe)); |
| | | 211 | | } |
| | 0 | 212 | | catch (SerializationException se) |
| | | 213 | | { |
| | 0 | 214 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException( |
| | 0 | 215 | | SR.Format(SR.SFxErrorDeserializingRequestBodyMore, OperationName, se.Message), |
| | 0 | 216 | | se)); |
| | | 217 | | } |
| | 175 | 218 | | } |
| | | 219 | | |
| | | 220 | | private object DeserializeBodyContents(Message message, object[] parameters, bool isRequest) |
| | | 221 | | { |
| | 175 | 222 | | SetupStreamAndMessageDescription(isRequest, out StreamFormatter streamFormatter, out MessageDescription mess |
| | | 223 | | |
| | 175 | 224 | | if (streamFormatter != null) |
| | | 225 | | { |
| | 16 | 226 | | object retVal = null; |
| | 16 | 227 | | streamFormatter.Deserialize(parameters, ref retVal, message); |
| | 16 | 228 | | return retVal; |
| | | 229 | | } |
| | | 230 | | |
| | 159 | 231 | | if (message.IsEmpty) |
| | | 232 | | { |
| | 2 | 233 | | return null; |
| | | 234 | | } |
| | | 235 | | else |
| | | 236 | | { |
| | 157 | 237 | | XmlDictionaryReader reader = message.GetReaderAtBodyContents(); |
| | 157 | 238 | | using (reader) |
| | | 239 | | { |
| | 157 | 240 | | object body = DeserializeBody(reader, message.Version, RequestAction, messageDescription, parameters |
| | 157 | 241 | | message.ReadFromBodyContentsToEnd(reader); |
| | 157 | 242 | | return body; |
| | | 243 | | } |
| | | 244 | | } |
| | 157 | 245 | | } |
| | | 246 | | |
| | | 247 | | public Message SerializeRequest(MessageVersion messageVersion, object[] parameters) |
| | | 248 | | { |
| | 0 | 249 | | if (messageVersion == null) |
| | | 250 | | { |
| | 0 | 251 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(messageVersion)); |
| | | 252 | | } |
| | | 253 | | |
| | 0 | 254 | | if (parameters == null) |
| | | 255 | | { |
| | 0 | 256 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parameters)); |
| | | 257 | | } |
| | | 258 | | |
| | | 259 | | object[] parts; |
| | 0 | 260 | | if (RequestDescription.IsTypedMessage) |
| | | 261 | | { |
| | 0 | 262 | | TypedMessageParts typedMessageParts = new TypedMessageParts(parameters[0], RequestDescription); |
| | | 263 | | |
| | | 264 | | // copy values from the actual field/properties |
| | 0 | 265 | | parts = new object[typedMessageParts.Count]; |
| | 0 | 266 | | typedMessageParts.GetTypedMessageParts(parts); |
| | | 267 | | } |
| | | 268 | | else |
| | | 269 | | { |
| | 0 | 270 | | parts = parameters; |
| | | 271 | | } |
| | 0 | 272 | | Message msg = new OperationFormatterMessage(this, messageVersion, |
| | 0 | 273 | | _action == null ? null : ActionHeader.Create(_action, messageVersion.Addressing), |
| | 0 | 274 | | parts, null, true/*isRequest*/); |
| | 0 | 275 | | AddPropertiesToMessage(msg, RequestDescription, parts); |
| | 0 | 276 | | AddHeadersToMessage(msg, RequestDescription, parts, true /*isRequest*/); |
| | | 277 | | |
| | 0 | 278 | | return msg; |
| | | 279 | | } |
| | | 280 | | |
| | | 281 | | public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result) |
| | | 282 | | { |
| | 167 | 283 | | if (messageVersion == null) |
| | | 284 | | { |
| | 0 | 285 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(messageVersion)); |
| | | 286 | | } |
| | | 287 | | |
| | 167 | 288 | | if (parameters == null) |
| | | 289 | | { |
| | 0 | 290 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parameters)); |
| | | 291 | | } |
| | | 292 | | |
| | | 293 | | object[] parts; |
| | | 294 | | object resultPart; |
| | 167 | 295 | | if (ReplyDescription.IsTypedMessage) |
| | | 296 | | { |
| | | 297 | | // If the response is a typed message then it must |
| | | 298 | | // me the response (as opposed to an out param). We will |
| | | 299 | | // serialize the response in the exact same way that we |
| | | 300 | | // would serialize a bunch of outs (with no return value). |
| | | 301 | | |
| | 13 | 302 | | TypedMessageParts typedMessageParts = new TypedMessageParts(result, ReplyDescription); |
| | | 303 | | |
| | | 304 | | // make a copy of the list so that we have the actual values of the field/properties |
| | 13 | 305 | | parts = new object[typedMessageParts.Count]; |
| | 13 | 306 | | typedMessageParts.GetTypedMessageParts(parts); |
| | | 307 | | |
| | 13 | 308 | | resultPart = null; |
| | | 309 | | } |
| | | 310 | | else |
| | | 311 | | { |
| | 154 | 312 | | parts = parameters; |
| | 154 | 313 | | resultPart = result; |
| | | 314 | | } |
| | | 315 | | |
| | 167 | 316 | | Message msg = new OperationFormatterMessage(this, messageVersion, |
| | 167 | 317 | | _replyAction == null ? null : ActionHeader.Create(_replyAction, messageVersion.Addressing), |
| | 167 | 318 | | parts, resultPart, false/*isRequest*/); |
| | 167 | 319 | | AddPropertiesToMessage(msg, ReplyDescription, parts); |
| | 167 | 320 | | AddHeadersToMessage(msg, ReplyDescription, parts, false /*isRequest*/); |
| | 167 | 321 | | return msg; |
| | | 322 | | } |
| | | 323 | | |
| | | 324 | | private void SetupStreamAndMessageDescription(bool isRequest, out StreamFormatter streamFormatter, out MessageDe |
| | | 325 | | { |
| | 342 | 326 | | if (isRequest) |
| | | 327 | | { |
| | 175 | 328 | | streamFormatter = requestStreamFormatter; |
| | 175 | 329 | | messageDescription = RequestDescription; |
| | | 330 | | } |
| | | 331 | | else |
| | | 332 | | { |
| | 167 | 333 | | streamFormatter = replyStreamFormatter; |
| | 167 | 334 | | messageDescription = ReplyDescription; |
| | | 335 | | } |
| | 167 | 336 | | } |
| | | 337 | | |
| | | 338 | | private async Task SerializeBodyContentsAsync(XmlDictionaryWriter writer, MessageVersion version, object[] param |
| | | 339 | | { |
| | 20 | 340 | | SetupStreamAndMessageDescription(isRequest, out StreamFormatter streamFormatter, out MessageDescription mess |
| | | 341 | | |
| | 20 | 342 | | if (streamFormatter != null) |
| | | 343 | | { |
| | 4 | 344 | | await streamFormatter.SerializeAsync(writer, parameters, returnValue); |
| | 4 | 345 | | return; |
| | | 346 | | } |
| | | 347 | | |
| | 16 | 348 | | SerializeBody(writer, version, RequestAction, messageDescription, returnValue, parameters, isRequest); |
| | 19 | 349 | | } |
| | | 350 | | |
| | | 351 | | private void SerializeBodyContents(XmlDictionaryWriter writer, MessageVersion version, object[] parameters, obje |
| | | 352 | | { |
| | 147 | 353 | | SetupStreamAndMessageDescription(isRequest, out StreamFormatter streamFormatter, out MessageDescription mess |
| | | 354 | | |
| | 147 | 355 | | if (streamFormatter != null) |
| | | 356 | | { |
| | 8 | 357 | | streamFormatter.Serialize(writer, parameters, returnValue); |
| | 8 | 358 | | return; |
| | | 359 | | } |
| | | 360 | | |
| | 139 | 361 | | SerializeBody(writer, version, RequestAction, messageDescription, returnValue, parameters, isRequest); |
| | 139 | 362 | | } |
| | | 363 | | |
| | | 364 | | private void AddPropertiesToMessage(Message message, MessageDescription messageDescription, object[] parameters) |
| | | 365 | | { |
| | 167 | 366 | | if (messageDescription.Properties.Count > 0) |
| | | 367 | | { |
| | 3 | 368 | | AddPropertiesToMessageCore(message, messageDescription, parameters); |
| | | 369 | | } |
| | 167 | 370 | | } |
| | | 371 | | |
| | | 372 | | private void AddPropertiesToMessageCore(Message message, MessageDescription messageDescription, object[] paramet |
| | | 373 | | { |
| | 3 | 374 | | MessageProperties properties = message.Properties; |
| | 3 | 375 | | MessagePropertyDescriptionCollection propertyDescriptions = messageDescription.Properties; |
| | 12 | 376 | | for (int i = 0; i < propertyDescriptions.Count; i++) |
| | | 377 | | { |
| | 3 | 378 | | MessagePropertyDescription propertyDescription = propertyDescriptions[i]; |
| | 3 | 379 | | object parameter = parameters[propertyDescription.Index]; |
| | 3 | 380 | | if (null != parameter) |
| | | 381 | | { |
| | 1 | 382 | | properties.Add(propertyDescription.Name, parameter); |
| | | 383 | | } |
| | | 384 | | } |
| | 3 | 385 | | } |
| | | 386 | | |
| | | 387 | | private void GetPropertiesFromMessage(Message message, MessageDescription messageDescription, object[] parameter |
| | | 388 | | { |
| | 175 | 389 | | if (messageDescription.Properties.Count > 0) |
| | | 390 | | { |
| | 2 | 391 | | GetPropertiesFromMessageCore(message, messageDescription, parameters); |
| | | 392 | | } |
| | 175 | 393 | | } |
| | | 394 | | |
| | | 395 | | private void GetPropertiesFromMessageCore(Message message, MessageDescription messageDescription, object[] param |
| | | 396 | | { |
| | 2 | 397 | | MessageProperties properties = message.Properties; |
| | 2 | 398 | | MessagePropertyDescriptionCollection propertyDescriptions = messageDescription.Properties; |
| | 8 | 399 | | for (int i = 0; i < propertyDescriptions.Count; i++) |
| | | 400 | | { |
| | 2 | 401 | | MessagePropertyDescription propertyDescription = propertyDescriptions[i]; |
| | 2 | 402 | | if (properties.ContainsKey(propertyDescription.Name)) |
| | | 403 | | { |
| | 0 | 404 | | parameters[propertyDescription.Index] = properties[propertyDescription.Name]; |
| | | 405 | | } |
| | | 406 | | } |
| | 2 | 407 | | } |
| | | 408 | | |
| | | 409 | | internal static object GetContentOfMessageHeaderOfT(MessageHeaderDescription headerDescription, object parameter |
| | | 410 | | { |
| | 8 | 411 | | actor = headerDescription.Actor; |
| | 8 | 412 | | mustUnderstand = headerDescription.MustUnderstand; |
| | 8 | 413 | | relay = headerDescription.Relay; |
| | | 414 | | |
| | 8 | 415 | | if (headerDescription.TypedHeader && parameterValue != null) |
| | | 416 | | { |
| | 0 | 417 | | parameterValue = TypedHeaderManager.GetContent(headerDescription.Type, parameterValue, out mustUnderstan |
| | | 418 | | } |
| | | 419 | | |
| | 8 | 420 | | return parameterValue; |
| | | 421 | | } |
| | | 422 | | |
| | | 423 | | internal static bool IsValidReturnValue(MessagePartDescription returnValue) |
| | | 424 | | { |
| | 3912 | 425 | | return (returnValue != null) && (returnValue.Type != typeof(void)); |
| | | 426 | | } |
| | | 427 | | |
| | | 428 | | internal static XmlDictionaryString AddToDictionary(XmlDictionary dictionary, string s) |
| | | 429 | | { |
| | 12306 | 430 | | if (!dictionary.TryLookup(s, out XmlDictionaryString dictionaryString)) |
| | | 431 | | { |
| | 9231 | 432 | | dictionaryString = dictionary.Add(s); |
| | | 433 | | } |
| | 12306 | 434 | | return dictionaryString; |
| | | 435 | | } |
| | | 436 | | |
| | | 437 | | internal static void Validate(OperationDescription operation, bool isRpc, bool isEncoded) |
| | | 438 | | { |
| | 3027 | 439 | | if (isEncoded && !isRpc) |
| | | 440 | | { |
| | 0 | 441 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.SFx |
| | | 442 | | } |
| | | 443 | | |
| | 3027 | 444 | | bool hasVoid = false; |
| | 3027 | 445 | | bool hasTypedOrUntypedMessage = false; |
| | 3027 | 446 | | bool hasParameter = false; |
| | 17534 | 447 | | for (int i = 0; i < operation.Messages.Count; i++) |
| | | 448 | | { |
| | 5740 | 449 | | MessageDescription message = operation.Messages[i]; |
| | 5740 | 450 | | if (message.IsTypedMessage || message.IsUntypedMessage) |
| | | 451 | | { |
| | 232 | 452 | | if (isRpc && operation.IsValidateRpcWrapperName) |
| | | 453 | | { |
| | 8 | 454 | | if (!isEncoded) |
| | | 455 | | { |
| | 0 | 456 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.F |
| | | 457 | | } |
| | | 458 | | } |
| | 232 | 459 | | hasTypedOrUntypedMessage = true; |
| | | 460 | | } |
| | 5508 | 461 | | else if (message.IsVoid) |
| | | 462 | | { |
| | 773 | 463 | | hasVoid = true; |
| | | 464 | | } |
| | | 465 | | else |
| | | 466 | | { |
| | 4735 | 467 | | hasParameter = true; |
| | | 468 | | } |
| | | 469 | | } |
| | 3027 | 470 | | if (hasParameter && hasTypedOrUntypedMessage) |
| | | 471 | | { |
| | 0 | 472 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.SFx |
| | | 473 | | } |
| | | 474 | | |
| | 3027 | 475 | | if (isRpc && hasTypedOrUntypedMessage && hasVoid) |
| | | 476 | | { |
| | 0 | 477 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.SFx |
| | | 478 | | } |
| | 3027 | 479 | | } |
| | | 480 | | |
| | | 481 | | internal static void GetActions(OperationDescription description, XmlDictionary dictionary, out XmlDictionaryStr |
| | | 482 | | { |
| | | 483 | | string actionString, replyActionString; |
| | 2839 | 484 | | actionString = description.Messages[0].Action; |
| | 2839 | 485 | | if (actionString == MessageHeaders.WildcardAction) |
| | | 486 | | { |
| | 0 | 487 | | actionString = null; |
| | | 488 | | } |
| | | 489 | | |
| | 2839 | 490 | | if (!description.IsOneWay) |
| | | 491 | | { |
| | 2525 | 492 | | replyActionString = description.Messages[1].Action; |
| | | 493 | | } |
| | | 494 | | else |
| | | 495 | | { |
| | 314 | 496 | | replyActionString = null; |
| | | 497 | | } |
| | | 498 | | |
| | 2839 | 499 | | if (replyActionString == MessageHeaders.WildcardAction) |
| | | 500 | | { |
| | 10 | 501 | | replyActionString = null; |
| | | 502 | | } |
| | | 503 | | |
| | 2839 | 504 | | action = replyAction = null; |
| | 2839 | 505 | | if (actionString != null) |
| | | 506 | | { |
| | 2839 | 507 | | action = AddToDictionary(dictionary, actionString); |
| | | 508 | | } |
| | | 509 | | |
| | 2839 | 510 | | if (replyActionString != null) |
| | | 511 | | { |
| | 2515 | 512 | | replyAction = AddToDictionary(dictionary, replyActionString); |
| | | 513 | | } |
| | 2839 | 514 | | } |
| | | 515 | | |
| | | 516 | | internal static void TraceAndSkipElement(XmlReader xmlReader) |
| | | 517 | | { |
| | | 518 | | //if (DiagnosticUtility.ShouldTraceVerbose) |
| | | 519 | | //{ |
| | | 520 | | // TraceUtility.TraceEvent(TraceEventType.Verbose, TraceCode.ElementIgnored, SR.SFxTraceCodeElementIgnore |
| | | 521 | | //} |
| | 4 | 522 | | xmlReader.Skip(); |
| | 4 | 523 | | } |
| | | 524 | | |
| | | 525 | | private class TypedMessageParts |
| | | 526 | | { |
| | | 527 | | private readonly object _instance; |
| | | 528 | | private readonly MemberInfo[] _members; |
| | | 529 | | |
| | 30 | 530 | | public TypedMessageParts(object instance, MessageDescription description) |
| | | 531 | | { |
| | 30 | 532 | | if (description == null) |
| | | 533 | | { |
| | 0 | 534 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(descripti |
| | | 535 | | } |
| | | 536 | | |
| | 30 | 537 | | _members = new MemberInfo[description.Body.Parts.Count + description.Properties.Count + description.Head |
| | | 538 | | |
| | 100 | 539 | | foreach (MessagePartDescription part in description.Headers) |
| | | 540 | | { |
| | 20 | 541 | | _members[part.Index] = part.MemberInfo; |
| | | 542 | | } |
| | | 543 | | |
| | 70 | 544 | | foreach (MessagePartDescription part in description.Properties) |
| | | 545 | | { |
| | 5 | 546 | | _members[part.Index] = part.MemberInfo; |
| | | 547 | | } |
| | | 548 | | |
| | 132 | 549 | | foreach (MessagePartDescription part in description.Body.Parts) |
| | | 550 | | { |
| | 36 | 551 | | _members[part.Index] = part.MemberInfo; |
| | | 552 | | } |
| | | 553 | | |
| | 30 | 554 | | _instance = instance ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullExcept |
| | 30 | 555 | | } |
| | | 556 | | |
| | | 557 | | private object GetValue(int index) |
| | | 558 | | { |
| | 29 | 559 | | MemberInfo memberInfo = _members[index]; |
| | 29 | 560 | | PropertyInfo propertyInfo = memberInfo as PropertyInfo; |
| | 29 | 561 | | if (propertyInfo != null) |
| | | 562 | | { |
| | 8 | 563 | | return propertyInfo.GetValue(_instance, null); |
| | | 564 | | } |
| | | 565 | | else |
| | | 566 | | { |
| | 21 | 567 | | return ((FieldInfo)memberInfo).GetValue(_instance); |
| | | 568 | | } |
| | | 569 | | } |
| | | 570 | | |
| | | 571 | | private void SetValue(object value, int index) |
| | | 572 | | { |
| | 32 | 573 | | MemberInfo memberInfo = _members[index]; |
| | 32 | 574 | | PropertyInfo propertyInfo = memberInfo as PropertyInfo; |
| | 32 | 575 | | if (propertyInfo != null) |
| | | 576 | | { |
| | 7 | 577 | | propertyInfo.SetValue(_instance, value, null); |
| | | 578 | | } |
| | | 579 | | else |
| | | 580 | | { |
| | 25 | 581 | | ((FieldInfo)memberInfo).SetValue(_instance, value); |
| | | 582 | | } |
| | 25 | 583 | | } |
| | | 584 | | |
| | | 585 | | internal void GetTypedMessageParts(object[] values) |
| | | 586 | | { |
| | 84 | 587 | | for (int i = 0; i < _members.Length; i++) |
| | | 588 | | { |
| | 29 | 589 | | values[i] = GetValue(i); |
| | | 590 | | } |
| | 13 | 591 | | } |
| | | 592 | | |
| | | 593 | | internal void SetTypedMessageParts(object[] values) |
| | | 594 | | { |
| | 98 | 595 | | for (int i = 0; i < _members.Length; i++) |
| | | 596 | | { |
| | 32 | 597 | | SetValue(values[i], i); |
| | | 598 | | } |
| | 17 | 599 | | } |
| | | 600 | | |
| | | 601 | | internal int Count |
| | | 602 | | { |
| | 30 | 603 | | get { return _members.Length; } |
| | | 604 | | } |
| | | 605 | | } |
| | | 606 | | |
| | | 607 | | internal class OperationFormatterMessage : BodyWriterMessage |
| | | 608 | | { |
| | | 609 | | private readonly OperationFormatter _operationFormatter; |
| | | 610 | | public OperationFormatterMessage(OperationFormatter operationFormatter, MessageVersion version, ActionHeader |
| | | 611 | | object[] parameters, object returnValue, bool isRequest) |
| | 167 | 612 | | : base(version, action, new OperationFormatterBodyWriter(operationFormatter, version, parameters, return |
| | | 613 | | { |
| | 167 | 614 | | _operationFormatter = operationFormatter; |
| | 167 | 615 | | } |
| | | 616 | | |
| | | 617 | | |
| | 0 | 618 | | public OperationFormatterMessage(MessageVersion version, string action, BodyWriter bodyWriter) : base(versio |
| | | 619 | | |
| | | 620 | | private OperationFormatterMessage(MessageHeaders headers, KeyValuePair<string, object>[] properties, Operati |
| | 11 | 621 | | : base(headers, properties, bodyWriter) |
| | | 622 | | { |
| | 11 | 623 | | _operationFormatter = bodyWriter.OperationFormatter; |
| | 11 | 624 | | } |
| | | 625 | | |
| | | 626 | | protected override void OnWriteStartBody(XmlDictionaryWriter writer) |
| | | 627 | | { |
| | 167 | 628 | | base.OnWriteStartBody(writer); |
| | 167 | 629 | | _operationFormatter.WriteBodyAttributes(writer, Version); |
| | 167 | 630 | | } |
| | | 631 | | |
| | | 632 | | protected override MessageBuffer OnCreateBufferedCopy(int maxBufferSize) |
| | | 633 | | { |
| | | 634 | | BodyWriter bufferedBodyWriter; |
| | 11 | 635 | | if (BodyWriter.IsBuffered) |
| | | 636 | | { |
| | 11 | 637 | | bufferedBodyWriter = BodyWriter; |
| | | 638 | | } |
| | | 639 | | else |
| | | 640 | | { |
| | 0 | 641 | | bufferedBodyWriter = BodyWriter.CreateBufferedCopy(maxBufferSize); |
| | | 642 | | } |
| | 11 | 643 | | KeyValuePair<string, object>[] properties = new KeyValuePair<string, object>[base.Properties.Count]; |
| | 11 | 644 | | ((ICollection<KeyValuePair<string, object>>)base.Properties).CopyTo(properties, 0); |
| | 11 | 645 | | return new OperationFormatterMessageBuffer(base.Headers, properties, bufferedBodyWriter); |
| | | 646 | | } |
| | | 647 | | |
| | | 648 | | private class OperationFormatterBodyWriter : BodyWriter |
| | | 649 | | { |
| | | 650 | | private readonly bool _isRequest; |
| | | 651 | | private readonly object[] _parameters; |
| | | 652 | | private readonly object _returnValue; |
| | | 653 | | private readonly MessageVersion _version; |
| | | 654 | | |
| | | 655 | | public OperationFormatterBodyWriter(OperationFormatter operationFormatter, MessageVersion version, |
| | | 656 | | object[] parameters, object returnValue, bool isRequest) |
| | 167 | 657 | | : base(AreParametersBuffered(isRequest, operationFormatter)) |
| | | 658 | | { |
| | 167 | 659 | | _parameters = parameters; |
| | 167 | 660 | | _returnValue = returnValue; |
| | 167 | 661 | | _isRequest = isRequest; |
| | 167 | 662 | | OperationFormatter = operationFormatter; |
| | 167 | 663 | | _version = version; |
| | 167 | 664 | | } |
| | | 665 | | |
| | | 666 | | private object ThisLock |
| | | 667 | | { |
| | 147 | 668 | | get { return this; } |
| | | 669 | | } |
| | | 670 | | |
| | | 671 | | private static bool AreParametersBuffered(bool isRequest, OperationFormatter operationFormatter) |
| | | 672 | | { |
| | 167 | 673 | | StreamFormatter streamFormatter = isRequest ? operationFormatter.requestStreamFormatter : operationF |
| | 167 | 674 | | return streamFormatter == null; |
| | | 675 | | } |
| | | 676 | | |
| | | 677 | | protected override void OnWriteBodyContents(XmlDictionaryWriter writer) |
| | | 678 | | { |
| | 147 | 679 | | lock (ThisLock) |
| | | 680 | | { |
| | 147 | 681 | | OperationFormatter.SerializeBodyContents(writer, _version, _parameters, _returnValue, _isRequest |
| | 147 | 682 | | } |
| | 147 | 683 | | } |
| | | 684 | | |
| | | 685 | | protected override Task OnWriteBodyContentsAsync(XmlDictionaryWriter writer) |
| | | 686 | | { |
| | 20 | 687 | | return OperationFormatter.SerializeBodyContentsAsync(writer, _version, _parameters, _returnValue, _i |
| | | 688 | | } |
| | | 689 | | |
| | 178 | 690 | | internal OperationFormatter OperationFormatter { get; } |
| | | 691 | | } |
| | | 692 | | |
| | | 693 | | private class OperationFormatterMessageBuffer : BodyWriterMessageBuffer |
| | | 694 | | { |
| | | 695 | | public OperationFormatterMessageBuffer(MessageHeaders headers, |
| | | 696 | | KeyValuePair<string, object>[] properties, BodyWriter bodyWriter) |
| | 11 | 697 | | : base(headers, properties, bodyWriter) |
| | | 698 | | { |
| | 11 | 699 | | } |
| | | 700 | | |
| | | 701 | | public override Message CreateMessage() |
| | | 702 | | { |
| | 11 | 703 | | if (!(BodyWriter is OperationFormatterBodyWriter operationFormatterBodyWriter)) |
| | | 704 | | { |
| | 0 | 705 | | return base.CreateMessage(); |
| | | 706 | | } |
| | | 707 | | |
| | 11 | 708 | | lock (ThisLock) |
| | | 709 | | { |
| | 11 | 710 | | if (Closed) |
| | | 711 | | { |
| | 0 | 712 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateBufferDisposedException()); |
| | | 713 | | } |
| | | 714 | | |
| | 11 | 715 | | return new OperationFormatterMessage(Headers, Properties, operationFormatterBodyWriter); |
| | | 716 | | } |
| | 11 | 717 | | } |
| | | 718 | | } |
| | | 719 | | } |
| | | 720 | | |
| | | 721 | | internal abstract class OperationFormatterHeader : MessageHeader |
| | | 722 | | { |
| | | 723 | | private readonly MessageHeader _innerHeader; //use innerHeader to handle versionSupported, actor/role handli |
| | | 724 | | |
| | 4 | 725 | | public OperationFormatterHeader(OperationFormatter operationFormatter, MessageVersion version, string name, |
| | | 726 | | { |
| | 4 | 727 | | if (actor != null) |
| | | 728 | | { |
| | 0 | 729 | | _innerHeader = CreateHeader(name, ns, null/*headerValue*/, mustUnderstand, actor, relay); |
| | | 730 | | } |
| | | 731 | | else |
| | | 732 | | { |
| | 4 | 733 | | _innerHeader = CreateHeader(name, ns, null/*headerValue*/, mustUnderstand, "", relay); |
| | | 734 | | } |
| | 4 | 735 | | } |
| | | 736 | | |
| | | 737 | | |
| | | 738 | | public override bool IsMessageVersionSupported(MessageVersion messageVersion) |
| | | 739 | | { |
| | 4 | 740 | | return _innerHeader.IsMessageVersionSupported(messageVersion); |
| | | 741 | | } |
| | | 742 | | |
| | | 743 | | |
| | | 744 | | public override string Name |
| | | 745 | | { |
| | 4 | 746 | | get { return _innerHeader.Name; } |
| | | 747 | | } |
| | | 748 | | |
| | | 749 | | public override string Namespace |
| | | 750 | | { |
| | 20 | 751 | | get { return _innerHeader.Namespace; } |
| | | 752 | | } |
| | | 753 | | |
| | | 754 | | public override bool MustUnderstand |
| | | 755 | | { |
| | 8 | 756 | | get { return _innerHeader.MustUnderstand; } |
| | | 757 | | } |
| | | 758 | | |
| | | 759 | | public override bool Relay |
| | | 760 | | { |
| | 4 | 761 | | get { return _innerHeader.Relay; } |
| | | 762 | | } |
| | | 763 | | |
| | | 764 | | public override string Actor |
| | | 765 | | { |
| | 4 | 766 | | get { return _innerHeader.Actor; } |
| | | 767 | | } |
| | | 768 | | |
| | | 769 | | protected override void OnWriteStartHeader(XmlDictionaryWriter writer, MessageVersion messageVersion) |
| | | 770 | | { |
| | | 771 | | //Prefix needed since there may be xsi:type attribute at toplevel with qname value where ns = "" |
| | 4 | 772 | | writer.WriteStartElement((Namespace == null || Namespace.Length == 0) ? string.Empty : "h", Name, Namesp |
| | 4 | 773 | | OnWriteHeaderAttributes(writer, messageVersion); |
| | 4 | 774 | | } |
| | | 775 | | |
| | | 776 | | protected virtual void OnWriteHeaderAttributes(XmlDictionaryWriter writer, MessageVersion messageVersion) |
| | | 777 | | { |
| | 0 | 778 | | WriteHeaderAttributes(writer, messageVersion); |
| | 0 | 779 | | } |
| | | 780 | | } |
| | | 781 | | |
| | | 782 | | internal class XmlElementMessageHeader : OperationFormatterHeader |
| | | 783 | | { |
| | | 784 | | protected XmlElement headerValue; |
| | | 785 | | public XmlElementMessageHeader(OperationFormatter operationFormatter, MessageVersion version, string name, s |
| | 4 | 786 | | base(operationFormatter, version, name, ns, mustUnderstand, actor, relay) |
| | | 787 | | { |
| | 4 | 788 | | this.headerValue = headerValue; |
| | 4 | 789 | | } |
| | | 790 | | |
| | | 791 | | protected override void OnWriteHeaderAttributes(XmlDictionaryWriter writer, MessageVersion messageVersion) |
| | | 792 | | { |
| | 4 | 793 | | WriteHeaderAttributes(writer, messageVersion); |
| | 4 | 794 | | XmlDictionaryReader nodeReader = XmlDictionaryReader.CreateDictionaryReader(new XmlNodeReader(headerValu |
| | 4 | 795 | | nodeReader.MoveToContent(); |
| | 4 | 796 | | writer.WriteAttributes(nodeReader, false); |
| | 4 | 797 | | } |
| | | 798 | | |
| | | 799 | | protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion) |
| | | 800 | | { |
| | 4 | 801 | | headerValue.WriteContentTo(writer); |
| | 4 | 802 | | } |
| | | 803 | | } |
| | | 804 | | internal struct QName |
| | | 805 | | { |
| | | 806 | | internal string Name; |
| | | 807 | | internal string Namespace; |
| | | 808 | | internal QName(string name, string ns) |
| | | 809 | | { |
| | 228 | 810 | | Name = name; |
| | 228 | 811 | | Namespace = ns; |
| | 228 | 812 | | } |
| | | 813 | | } |
| | | 814 | | internal class QNameComparer : IEqualityComparer<QName> |
| | | 815 | | { |
| | 4 | 816 | | internal static QNameComparer Singleton = new QNameComparer(); |
| | | 817 | | |
| | 8 | 818 | | private QNameComparer() { } |
| | | 819 | | |
| | | 820 | | public bool Equals(QName x, QName y) |
| | | 821 | | { |
| | 16 | 822 | | return x.Name == y.Name && x.Namespace == y.Namespace; |
| | | 823 | | } |
| | | 824 | | |
| | | 825 | | public int GetHashCode(QName obj) |
| | | 826 | | { |
| | 228 | 827 | | return obj.Name.GetHashCode(); |
| | | 828 | | } |
| | | 829 | | } |
| | | 830 | | internal class MessageHeaderDescriptionTable : Dictionary<QName, MessageHeaderDescription> |
| | | 831 | | { |
| | 3488 | 832 | | internal MessageHeaderDescriptionTable() : base(QNameComparer.Singleton) { } |
| | | 833 | | internal void Add(string name, string ns, MessageHeaderDescription message) |
| | | 834 | | { |
| | 190 | 835 | | Add(new QName(name, ns), message); |
| | 190 | 836 | | } |
| | | 837 | | internal MessageHeaderDescription Get(string name, string ns) |
| | | 838 | | { |
| | 38 | 839 | | if (TryGetValue(new QName(name, ns), out MessageHeaderDescription message)) |
| | | 840 | | { |
| | 16 | 841 | | return message; |
| | | 842 | | } |
| | | 843 | | |
| | 22 | 844 | | return null; |
| | | 845 | | } |
| | | 846 | | } |
| | | 847 | | } |
| | | 848 | | } |