| | | 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 CoreWCF.Channels; |
| | | 7 | | using CoreWCF.Runtime; |
| | | 8 | | using CoreWCF.Web; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.Dispatcher |
| | | 11 | | { |
| | | 12 | | internal class MultiplexingDispatchMessageFormatter : IDispatchMessageFormatter |
| | | 13 | | { |
| | | 14 | | private readonly Dictionary<WebMessageFormat, IDispatchMessageFormatter> _formatters; |
| | | 15 | | |
| | 30 | 16 | | public WebMessageFormat DefaultFormat { get; } |
| | | 17 | | |
| | 344 | 18 | | public Dictionary<WebMessageFormat, string> DefaultContentTypes { get; } |
| | | 19 | | |
| | 160 | 20 | | public MultiplexingDispatchMessageFormatter(Dictionary<WebMessageFormat, IDispatchMessageFormatter> formatters, |
| | | 21 | | { |
| | 160 | 22 | | _formatters = formatters ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(formatte |
| | 160 | 23 | | DefaultFormat = defaultFormat; |
| | 160 | 24 | | DefaultContentTypes = new Dictionary<WebMessageFormat, string>(); |
| | | 25 | | |
| | | 26 | | Fx.Assert(_formatters.ContainsKey(DefaultFormat), "The default format should always be included in the dicti |
| | 160 | 27 | | } |
| | | 28 | | |
| | | 29 | | public void DeserializeRequest(Message message, object[] parameters) |
| | | 30 | | { |
| | 0 | 31 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Format(SR.Serializing |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result) |
| | | 35 | | { |
| | 30 | 36 | | WebOperationContext currentContext = WebOperationContext.Current; |
| | 30 | 37 | | OutgoingWebResponseContext outgoingResponse = null; |
| | | 38 | | |
| | 30 | 39 | | if (currentContext != null) |
| | | 40 | | { |
| | 30 | 41 | | outgoingResponse = currentContext.OutgoingResponse; |
| | | 42 | | } |
| | | 43 | | |
| | 30 | 44 | | WebMessageFormat format = DefaultFormat; |
| | 30 | 45 | | if (outgoingResponse != null) |
| | | 46 | | { |
| | 30 | 47 | | WebMessageFormat? nullableFormat = outgoingResponse.Format; |
| | 30 | 48 | | if (nullableFormat.HasValue) |
| | | 49 | | { |
| | 0 | 50 | | format = nullableFormat.Value; |
| | | 51 | | } |
| | | 52 | | } |
| | | 53 | | |
| | 30 | 54 | | if (!_formatters.ContainsKey(format)) |
| | | 55 | | { |
| | 0 | 56 | | string operationName = "<null>"; |
| | | 57 | | |
| | 0 | 58 | | if (OperationContext.Current != null) |
| | | 59 | | { |
| | 0 | 60 | | MessageProperties messageProperties = OperationContext.Current.IncomingMessageProperties; |
| | 0 | 61 | | if (messageProperties.ContainsKey(WebHttpDispatchOperationSelector.HttpOperationNamePropertyName)) |
| | | 62 | | { |
| | 0 | 63 | | operationName = messageProperties[WebHttpDispatchOperationSelector.HttpOperationNamePropertyName |
| | | 64 | | } |
| | | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Format(SR.Operati |
| | | 68 | | } |
| | | 69 | | |
| | 30 | 70 | | if (outgoingResponse != null && string.IsNullOrEmpty(outgoingResponse.ContentType)) |
| | | 71 | | { |
| | 29 | 72 | | string automatedSelectionContentType = outgoingResponse.AutomatedFormatSelectionContentType; |
| | 29 | 73 | | if (!string.IsNullOrEmpty(automatedSelectionContentType)) |
| | | 74 | | { |
| | | 75 | | // Don't set the content-type if it is default xml for backwards compatibility |
| | 0 | 76 | | if (!string.Equals(automatedSelectionContentType, DefaultContentTypes[WebMessageFormat.Xml], StringC |
| | | 77 | | { |
| | 0 | 78 | | outgoingResponse.ContentType = automatedSelectionContentType; |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | else |
| | | 82 | | { |
| | | 83 | | // Don't set the content-type if it is default xml for backwards compatibility |
| | 29 | 84 | | if (format != WebMessageFormat.Xml) |
| | | 85 | | { |
| | 24 | 86 | | outgoingResponse.ContentType = DefaultContentTypes[format]; |
| | | 87 | | } |
| | | 88 | | } |
| | | 89 | | } |
| | | 90 | | |
| | 30 | 91 | | Message message = _formatters[format].SerializeReply(messageVersion, parameters, result); |
| | | 92 | | |
| | 30 | 93 | | return message; |
| | | 94 | | } |
| | | 95 | | |
| | 0 | 96 | | public bool SupportsMessageFormat(WebMessageFormat format) => _formatters.ContainsKey(format); |
| | | 97 | | } |
| | | 98 | | } |