| | | 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.IO; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using System.Xml; |
| | | 8 | | using CoreWCF.Channels; |
| | | 9 | | using CoreWCF.Description; |
| | | 10 | | using CoreWCF.Diagnostics; |
| | | 11 | | using CoreWCF.Runtime; |
| | | 12 | | |
| | | 13 | | namespace CoreWCF.Dispatcher |
| | | 14 | | { |
| | | 15 | | internal class StreamFormatter |
| | | 16 | | { |
| | | 17 | | private readonly int _streamIndex; |
| | | 18 | | private readonly bool _isRequest; |
| | | 19 | | private readonly string _operationName; |
| | | 20 | | private const int returnValueIndex = -1; |
| | | 21 | | |
| | | 22 | | internal static StreamFormatter Create(MessageDescription messageDescription, string operationName, bool isReque |
| | | 23 | | { |
| | 1968 | 24 | | MessagePartDescription streamPart = ValidateAndGetStreamPart(messageDescription, isRequest, operationName); |
| | 1968 | 25 | | if (streamPart == null) |
| | | 26 | | { |
| | 1349 | 27 | | return null; |
| | | 28 | | } |
| | | 29 | | |
| | 619 | 30 | | return new StreamFormatter(messageDescription, streamPart, operationName, isRequest); |
| | | 31 | | } |
| | | 32 | | |
| | 619 | 33 | | private StreamFormatter(MessageDescription messageDescription, MessagePartDescription streamPart, string operati |
| | | 34 | | { |
| | 619 | 35 | | if ((object)streamPart == (object)messageDescription.Body.ReturnValue) |
| | | 36 | | { |
| | 228 | 37 | | _streamIndex = returnValueIndex; |
| | | 38 | | } |
| | | 39 | | else |
| | | 40 | | { |
| | 391 | 41 | | _streamIndex = streamPart.Index; |
| | | 42 | | } |
| | | 43 | | |
| | 619 | 44 | | WrapperName = messageDescription.Body.WrapperName; |
| | 619 | 45 | | WrapperNamespace = messageDescription.Body.WrapperNamespace; |
| | 619 | 46 | | PartName = streamPart.Name; |
| | 619 | 47 | | PartNamespace = streamPart.Namespace; |
| | 619 | 48 | | _isRequest = isRequest; |
| | 619 | 49 | | _operationName = operationName; |
| | 619 | 50 | | } |
| | | 51 | | |
| | | 52 | | internal void Serialize(XmlDictionaryWriter writer, object[] parameters, object returnValue) |
| | | 53 | | { |
| | 8 | 54 | | Stream streamValue = GetStreamAndWriteStartWrapperIfNecessary(writer, parameters, returnValue); |
| | 8 | 55 | | var streamProvider = new OperationStreamProvider(streamValue); |
| | 8 | 56 | | writer.WriteValue(streamProvider); |
| | 8 | 57 | | WriteEndWrapperIfNecessary(writer); |
| | 8 | 58 | | } |
| | | 59 | | |
| | | 60 | | internal async Task SerializeAsync(XmlDictionaryWriter writer, object[] parameters, object returnValue) |
| | | 61 | | { |
| | 4 | 62 | | using (TaskHelpers.RunTaskContinuationsOnOurThreads()) // If inner stream doesn't have sync implementation, |
| | | 63 | | { |
| | 4 | 64 | | Stream streamValue = GetStreamAndWriteStartWrapperIfNecessary(writer, parameters, returnValue); |
| | 4 | 65 | | var streamProvider = new OperationStreamProvider(streamValue); |
| | 4 | 66 | | await writer.WriteValueAsync(streamProvider); |
| | 4 | 67 | | await WriteEndWrapperIfNecessaryAsync(writer); |
| | 4 | 68 | | } |
| | 4 | 69 | | } |
| | | 70 | | |
| | | 71 | | private Stream GetStreamAndWriteStartWrapperIfNecessary(XmlDictionaryWriter writer, object[] parameters, object |
| | | 72 | | { |
| | 12 | 73 | | Stream streamValue = GetStreamValue(parameters, returnValue); |
| | 12 | 74 | | if (streamValue == null) |
| | | 75 | | { |
| | 0 | 76 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(PartName); |
| | | 77 | | } |
| | | 78 | | |
| | 12 | 79 | | if (WrapperName != null) |
| | | 80 | | { |
| | 12 | 81 | | writer.WriteStartElement(null, WrapperName, WrapperNamespace); |
| | | 82 | | } |
| | | 83 | | |
| | 12 | 84 | | writer.WriteStartElement(null, PartName, PartNamespace); |
| | 12 | 85 | | return streamValue; |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | private void WriteEndWrapperIfNecessary(XmlDictionaryWriter writer) |
| | | 89 | | { |
| | 8 | 90 | | writer.WriteEndElement(); |
| | 8 | 91 | | if (WrapperName != null) |
| | | 92 | | { |
| | 8 | 93 | | writer.WriteEndElement(); |
| | | 94 | | } |
| | 8 | 95 | | } |
| | | 96 | | |
| | | 97 | | private Task WriteEndWrapperIfNecessaryAsync(XmlDictionaryWriter writer) |
| | | 98 | | { |
| | 4 | 99 | | writer.WriteEndElement(); |
| | 4 | 100 | | if (WrapperName != null) |
| | | 101 | | { |
| | 4 | 102 | | writer.WriteEndElement(); |
| | | 103 | | } |
| | | 104 | | |
| | 4 | 105 | | return Task.CompletedTask; |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | internal void Deserialize(object[] parameters, ref object retVal, Message message) |
| | | 109 | | { |
| | 16 | 110 | | SetStreamValue(parameters, ref retVal, new MessageBodyStream(message, WrapperName, WrapperNamespace, PartNam |
| | 16 | 111 | | } |
| | | 112 | | |
| | 731 | 113 | | internal string WrapperName { get; set; } |
| | | 114 | | |
| | 707 | 115 | | internal string WrapperNamespace { get; set; } |
| | | 116 | | |
| | 88 | 117 | | internal string PartName { get; } |
| | | 118 | | |
| | 88 | 119 | | internal string PartNamespace { get; } |
| | | 120 | | |
| | | 121 | | private Stream GetStreamValue(object[] parameters, object returnValue) |
| | | 122 | | { |
| | 12 | 123 | | if (_streamIndex == returnValueIndex) |
| | | 124 | | { |
| | 5 | 125 | | return (Stream)returnValue; |
| | | 126 | | } |
| | | 127 | | |
| | 7 | 128 | | return (Stream)parameters[_streamIndex]; |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | private void SetStreamValue(object[] parameters, ref object returnValue, Stream streamValue) |
| | | 132 | | { |
| | 16 | 133 | | if (_streamIndex == returnValueIndex) |
| | | 134 | | { |
| | 0 | 135 | | returnValue = streamValue; |
| | | 136 | | } |
| | | 137 | | else |
| | | 138 | | { |
| | 16 | 139 | | parameters[_streamIndex] = streamValue; |
| | | 140 | | } |
| | 16 | 141 | | } |
| | | 142 | | |
| | | 143 | | private static MessagePartDescription ValidateAndGetStreamPart(MessageDescription messageDescription, bool isReq |
| | | 144 | | { |
| | 1968 | 145 | | MessagePartDescription part = GetStreamPart(messageDescription); |
| | 1968 | 146 | | if (part != null) |
| | | 147 | | { |
| | 619 | 148 | | return part; |
| | | 149 | | } |
| | | 150 | | |
| | 1349 | 151 | | if (HasStream(messageDescription)) |
| | | 152 | | { |
| | 0 | 153 | | if (messageDescription.IsTypedMessage) |
| | | 154 | | { |
| | 0 | 155 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 156 | | } |
| | 0 | 157 | | else if (isRequest) |
| | | 158 | | { |
| | 0 | 159 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 160 | | } |
| | | 161 | | else |
| | | 162 | | { |
| | 0 | 163 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 164 | | } |
| | | 165 | | } |
| | 1349 | 166 | | return null; |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | private static bool HasStream(MessageDescription messageDescription) |
| | | 170 | | { |
| | 1349 | 171 | | if (messageDescription.Body.ReturnValue != null && messageDescription.Body.ReturnValue.Type == typeof(Stream |
| | | 172 | | { |
| | 0 | 173 | | return true; |
| | | 174 | | } |
| | | 175 | | |
| | 4578 | 176 | | foreach (MessagePartDescription part in messageDescription.Body.Parts) |
| | | 177 | | { |
| | 940 | 178 | | if (part.Type == typeof(Stream)) |
| | | 179 | | { |
| | 0 | 180 | | return true; |
| | | 181 | | } |
| | | 182 | | } |
| | 1349 | 183 | | return false; |
| | 0 | 184 | | } |
| | | 185 | | |
| | | 186 | | private static MessagePartDescription GetStreamPart(MessageDescription messageDescription) |
| | | 187 | | { |
| | 1968 | 188 | | if (OperationFormatter.IsValidReturnValue(messageDescription.Body.ReturnValue)) |
| | | 189 | | { |
| | 722 | 190 | | if (messageDescription.Body.Parts.Count == 0) |
| | | 191 | | { |
| | 584 | 192 | | if (messageDescription.Body.ReturnValue.Type == typeof(Stream)) |
| | | 193 | | { |
| | 228 | 194 | | return messageDescription.Body.ReturnValue; |
| | | 195 | | } |
| | | 196 | | } |
| | | 197 | | } |
| | | 198 | | else |
| | | 199 | | { |
| | 1246 | 200 | | if (messageDescription.Body.Parts.Count == 1) |
| | | 201 | | { |
| | 1157 | 202 | | if (messageDescription.Body.Parts[0].Type == typeof(Stream)) |
| | | 203 | | { |
| | 391 | 204 | | return messageDescription.Body.Parts[0]; |
| | | 205 | | } |
| | | 206 | | } |
| | | 207 | | } |
| | 1349 | 208 | | return null; |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | internal static bool IsStream(MessageDescription messageDescription) |
| | | 212 | | { |
| | 0 | 213 | | return GetStreamPart(messageDescription) != null; |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | internal class OperationStreamProvider : IStreamProvider |
| | | 217 | | { |
| | | 218 | | private readonly Stream _stream; |
| | | 219 | | |
| | 12 | 220 | | internal OperationStreamProvider(Stream stream) |
| | | 221 | | { |
| | 12 | 222 | | _stream = stream; |
| | 12 | 223 | | } |
| | | 224 | | |
| | | 225 | | public Stream GetStream() |
| | | 226 | | { |
| | 12 | 227 | | return _stream; |
| | | 228 | | } |
| | | 229 | | public void ReleaseStream(Stream stream) |
| | | 230 | | { |
| | | 231 | | //Noop |
| | 12 | 232 | | } |
| | | 233 | | } |
| | | 234 | | } |
| | | 235 | | } |