| | | 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.Diagnostics; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using CoreWCF.Runtime; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.Channels |
| | | 11 | | { |
| | | 12 | | internal abstract class RequestContextBase : RequestContext |
| | | 13 | | { |
| | | 14 | | private TimeSpan _defaultSendTimeout; |
| | | 15 | | private TimeSpan _defaultCloseTimeout; |
| | 50 | 16 | | private CommunicationState _state = CommunicationState.Opened; |
| | | 17 | | private Message _requestMessage; |
| | | 18 | | private Exception _requestMessageException; |
| | | 19 | | private bool _replySent; |
| | | 20 | | |
| | 50 | 21 | | protected RequestContextBase(Message requestMessage, TimeSpan defaultCloseTimeout, TimeSpan defaultSendTimeout) |
| | | 22 | | { |
| | 50 | 23 | | _defaultSendTimeout = defaultSendTimeout; |
| | 50 | 24 | | _defaultCloseTimeout = defaultCloseTimeout; |
| | 50 | 25 | | _requestMessage = requestMessage; |
| | 50 | 26 | | } |
| | | 27 | | |
| | | 28 | | public void ReInitialize(Message requestMessage) |
| | | 29 | | { |
| | 0 | 30 | | _state = CommunicationState.Opened; |
| | 0 | 31 | | _requestMessageException = null; |
| | 0 | 32 | | _replySent = false; |
| | 0 | 33 | | ReplyInitiated = false; |
| | 0 | 34 | | Aborted = false; |
| | 0 | 35 | | _requestMessage = requestMessage; |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | public override Message RequestMessage |
| | | 39 | | { |
| | | 40 | | get |
| | | 41 | | { |
| | 150 | 42 | | if (_requestMessageException != null) |
| | | 43 | | { |
| | 0 | 44 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(_requestMessageException); |
| | | 45 | | } |
| | | 46 | | |
| | 150 | 47 | | return _requestMessage; |
| | | 48 | | } |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | protected void SetRequestMessage(Message requestMessage) |
| | | 52 | | { |
| | | 53 | | Fx.Assert(_requestMessageException == null, "Cannot have both a requestMessage and a requestException."); |
| | 0 | 54 | | _requestMessage = requestMessage; |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | protected void SetRequestMessage(Exception requestMessageException) |
| | | 58 | | { |
| | | 59 | | Fx.Assert(_requestMessage == null, "Cannot have both a requestMessage and a requestException."); |
| | 0 | 60 | | _requestMessageException = requestMessageException; |
| | 0 | 61 | | } |
| | | 62 | | |
| | 150 | 63 | | protected bool ReplyInitiated { get; private set; } |
| | | 64 | | |
| | 250 | 65 | | protected object ThisLock { get; } = new object(); |
| | | 66 | | |
| | 0 | 67 | | public bool Aborted { get; private set; } |
| | | 68 | | |
| | | 69 | | public TimeSpan DefaultCloseTimeout |
| | | 70 | | { |
| | 0 | 71 | | get { return _defaultCloseTimeout; } |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | public TimeSpan DefaultSendTimeout |
| | | 75 | | { |
| | 0 | 76 | | get { return _defaultSendTimeout; } |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | public override void Abort() |
| | | 80 | | { |
| | 0 | 81 | | lock (ThisLock) |
| | | 82 | | { |
| | 0 | 83 | | if (_state == CommunicationState.Closed) |
| | | 84 | | { |
| | 0 | 85 | | return; |
| | | 86 | | } |
| | | 87 | | |
| | 0 | 88 | | _state = CommunicationState.Closing; |
| | | 89 | | |
| | 0 | 90 | | Aborted = true; |
| | 0 | 91 | | } |
| | | 92 | | |
| | | 93 | | //if (DiagnosticUtility.ShouldTraceWarning) |
| | | 94 | | //{ |
| | | 95 | | // TraceUtility.TraceEvent(TraceEventType.Warning, TraceCode.RequestContextAbort, |
| | | 96 | | // SRCommon.Format(SRCommon.TraceCodeRequestContextAbort), this); |
| | | 97 | | //} |
| | | 98 | | |
| | | 99 | | try |
| | | 100 | | { |
| | 0 | 101 | | OnAbort(); |
| | 0 | 102 | | } |
| | | 103 | | finally |
| | | 104 | | { |
| | 0 | 105 | | _state = CommunicationState.Closed; |
| | 0 | 106 | | } |
| | 0 | 107 | | } |
| | | 108 | | |
| | | 109 | | public override Task CloseAsync() |
| | | 110 | | { |
| | 50 | 111 | | var helper = new TimeoutHelper(_defaultCloseTimeout); |
| | 50 | 112 | | return CloseAsync(helper.GetCancellationToken()); |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | public override async Task CloseAsync(CancellationToken token) |
| | | 116 | | { |
| | 50 | 117 | | bool sendAck = false; |
| | 50 | 118 | | lock (ThisLock) |
| | | 119 | | { |
| | 50 | 120 | | if (_state != CommunicationState.Opened) |
| | | 121 | | { |
| | 0 | 122 | | return; |
| | | 123 | | } |
| | | 124 | | |
| | 50 | 125 | | if (TryInitiateReply()) |
| | | 126 | | { |
| | 0 | 127 | | sendAck = true; |
| | | 128 | | } |
| | | 129 | | |
| | 50 | 130 | | _state = CommunicationState.Closing; |
| | 50 | 131 | | } |
| | | 132 | | |
| | 50 | 133 | | bool throwing = true; |
| | | 134 | | |
| | | 135 | | try |
| | | 136 | | { |
| | 50 | 137 | | if (sendAck) |
| | | 138 | | { |
| | 0 | 139 | | await OnReplyAsync(null, token); |
| | | 140 | | } |
| | | 141 | | |
| | 50 | 142 | | await OnCloseAsync(token); |
| | 50 | 143 | | _state = CommunicationState.Closed; |
| | 50 | 144 | | throwing = false; |
| | 50 | 145 | | } |
| | | 146 | | finally |
| | | 147 | | { |
| | 50 | 148 | | if (throwing) |
| | | 149 | | { |
| | 0 | 150 | | Abort(); |
| | | 151 | | } |
| | | 152 | | } |
| | 50 | 153 | | } |
| | | 154 | | |
| | | 155 | | protected override void Dispose(bool disposing) |
| | | 156 | | { |
| | 0 | 157 | | base.Dispose(disposing); |
| | | 158 | | |
| | 0 | 159 | | if (!disposing) |
| | | 160 | | { |
| | 0 | 161 | | return; |
| | | 162 | | } |
| | | 163 | | |
| | 0 | 164 | | if (_replySent) |
| | | 165 | | { |
| | 0 | 166 | | CloseAsync().GetAwaiter().GetResult(); |
| | | 167 | | } |
| | | 168 | | else |
| | | 169 | | { |
| | 0 | 170 | | Abort(); |
| | | 171 | | } |
| | 0 | 172 | | } |
| | | 173 | | |
| | | 174 | | protected abstract void OnAbort(); |
| | | 175 | | protected abstract Task OnCloseAsync(CancellationToken token); |
| | | 176 | | protected abstract Task OnReplyAsync(Message message, CancellationToken token); |
| | | 177 | | |
| | | 178 | | protected void ThrowIfInvalidReply() |
| | | 179 | | { |
| | 50 | 180 | | if (_state == CommunicationState.Closed || _state == CommunicationState.Closing) |
| | | 181 | | { |
| | 0 | 182 | | if (Aborted) |
| | | 183 | | { |
| | 0 | 184 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationObjectAbortedException(SR |
| | | 185 | | } |
| | | 186 | | else |
| | | 187 | | { |
| | 0 | 188 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(GetType().Full |
| | | 189 | | } |
| | | 190 | | } |
| | | 191 | | |
| | 50 | 192 | | if (ReplyInitiated) |
| | | 193 | | { |
| | 0 | 194 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SRCommon.ReplyAl |
| | | 195 | | } |
| | 50 | 196 | | } |
| | | 197 | | |
| | | 198 | | /// <summary> |
| | | 199 | | /// Attempts to initiate the reply. If a reply is not initiated already (and the object is opened), |
| | | 200 | | /// then it initiates the reply and returns true. Otherwise, it returns false. |
| | | 201 | | /// </summary> |
| | | 202 | | protected bool TryInitiateReply() |
| | | 203 | | { |
| | 50 | 204 | | lock (ThisLock) |
| | | 205 | | { |
| | 50 | 206 | | if ((_state != CommunicationState.Opened) || ReplyInitiated) |
| | | 207 | | { |
| | 50 | 208 | | return false; |
| | | 209 | | } |
| | | 210 | | else |
| | | 211 | | { |
| | 0 | 212 | | ReplyInitiated = true; |
| | 0 | 213 | | return true; |
| | | 214 | | } |
| | | 215 | | } |
| | 50 | 216 | | } |
| | | 217 | | |
| | | 218 | | public override Task ReplyAsync(Message message) |
| | | 219 | | { |
| | 0 | 220 | | var helper = new TimeoutHelper(_defaultSendTimeout); |
| | 0 | 221 | | return ReplyAsync(message, helper.GetCancellationToken()); |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | public override async Task ReplyAsync(Message message, CancellationToken token) |
| | | 225 | | { |
| | | 226 | | // "null" is a valid reply (signals a 202-style "ack"), so we don't have a null-check here |
| | 50 | 227 | | lock (ThisLock) |
| | | 228 | | { |
| | 50 | 229 | | ThrowIfInvalidReply(); |
| | 50 | 230 | | ReplyInitiated = true; |
| | 50 | 231 | | } |
| | | 232 | | |
| | 50 | 233 | | await OnReplyAsync(message, token); |
| | 50 | 234 | | _replySent = true; |
| | 50 | 235 | | } |
| | | 236 | | |
| | | 237 | | // This method is designed for WebSocket only, and will only be used once the WebSocket response was sent. |
| | | 238 | | // For WebSocket, we never call HttpRequestContext.Reply to send the response back. |
| | | 239 | | // Instead we call AcceptWebSocket directly. So we need to set the replyInitiated and |
| | | 240 | | // replySent boolean to be true once the response was sent successfully. Otherwise when we |
| | | 241 | | // are disposing the HttpRequestContext, we will see a bunch of warnings in trace log. |
| | | 242 | | protected void SetReplySent() |
| | | 243 | | { |
| | 0 | 244 | | lock (ThisLock) |
| | | 245 | | { |
| | 0 | 246 | | ThrowIfInvalidReply(); |
| | 0 | 247 | | ReplyInitiated = true; |
| | 0 | 248 | | } |
| | | 249 | | |
| | 0 | 250 | | _replySent = true; |
| | 0 | 251 | | } |
| | | 252 | | } |
| | | 253 | | |
| | | 254 | | internal class RequestContextMessageProperty : IDisposable |
| | | 255 | | { |
| | | 256 | | private RequestContext _context; |
| | | 257 | | private readonly object _thisLock = new object(); |
| | | 258 | | |
| | | 259 | | public RequestContextMessageProperty(RequestContext context) |
| | | 260 | | { |
| | | 261 | | _context = context; |
| | | 262 | | } |
| | | 263 | | |
| | | 264 | | public static string Name |
| | | 265 | | { |
| | | 266 | | get { return "requestContext"; } |
| | | 267 | | } |
| | | 268 | | |
| | | 269 | | void IDisposable.Dispose() |
| | | 270 | | { |
| | | 271 | | bool success = false; |
| | | 272 | | RequestContext thisContext; |
| | | 273 | | |
| | | 274 | | lock (_thisLock) |
| | | 275 | | { |
| | | 276 | | if (_context == null) |
| | | 277 | | { |
| | | 278 | | return; |
| | | 279 | | } |
| | | 280 | | |
| | | 281 | | thisContext = _context; |
| | | 282 | | _context = null; |
| | | 283 | | } |
| | | 284 | | |
| | | 285 | | try |
| | | 286 | | { |
| | | 287 | | thisContext.CloseAsync().GetAwaiter().GetResult(); |
| | | 288 | | success = true; |
| | | 289 | | } |
| | | 290 | | catch (CommunicationException e) |
| | | 291 | | { |
| | | 292 | | DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); |
| | | 293 | | } |
| | | 294 | | catch (TimeoutException e) |
| | | 295 | | { |
| | | 296 | | //if (TD.CloseTimeoutIsEnabled()) |
| | | 297 | | //{ |
| | | 298 | | // TD.CloseTimeout(e.Message); |
| | | 299 | | //} |
| | | 300 | | DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); |
| | | 301 | | } |
| | | 302 | | finally |
| | | 303 | | { |
| | | 304 | | if (!success) |
| | | 305 | | { |
| | | 306 | | thisContext.Abort(); |
| | | 307 | | } |
| | | 308 | | } |
| | | 309 | | } |
| | | 310 | | } |
| | | 311 | | } |