| | | 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.Globalization; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using System.Xml; |
| | | 9 | | using CoreWCF.Channels; |
| | | 10 | | using CoreWCF.Configuration; |
| | | 11 | | using CoreWCF.Description; |
| | | 12 | | using CoreWCF.Diagnostics; |
| | | 13 | | using CoreWCF.Runtime; |
| | | 14 | | using SessionIdleManager = CoreWCF.Channels.ServiceChannel.SessionIdleManager; |
| | | 15 | | |
| | | 16 | | namespace CoreWCF.Dispatcher |
| | | 17 | | { |
| | | 18 | | internal class ChannelHandler : IServiceChannelDispatcher, IDefaultCommunicationTimeouts |
| | | 19 | | { |
| | 0 | 20 | | public static readonly TimeSpan CloseAfterFaultTimeout = TimeSpan.FromSeconds(10); |
| | | 21 | | public const string MessageBufferPropertyName = "_RequestMessageBuffer_"; |
| | | 22 | | private ServiceChannel _channel; |
| | | 23 | | private bool _doneReceiving; |
| | | 24 | | private readonly ServiceDispatcher _serviceDispatcher; |
| | | 25 | | private readonly MessageVersion _messageVersion; |
| | | 26 | | private readonly bool _isManualAddressing; |
| | | 27 | | private readonly IChannelBinder _binder; |
| | | 28 | | private readonly ServiceThrottle _throttle; |
| | | 29 | | private readonly bool _wasChannelThrottled; |
| | | 30 | | private readonly DuplexChannelBinder _duplexBinder; |
| | | 31 | | private readonly ServiceHostBase _host; |
| | | 32 | | private readonly bool _hasSession; |
| | | 33 | | private readonly bool _isConcurrent; |
| | | 34 | | private readonly SessionIdleManager _idleManager; |
| | | 35 | | private readonly SessionOpenNotification _sessionOpenNotification; |
| | | 36 | | private bool _needToCreateSessionOpenNotificationMessage; |
| | | 37 | | //private RequestInfo _requestInfo; |
| | | 38 | | private bool _isChannelTerminated; |
| | | 39 | | private bool _shouldRejectMessageWithOnOpenActionHeader; |
| | | 40 | | private RequestContext _replied; |
| | | 41 | | private readonly bool _incrementedActivityCountInConstructor; |
| | | 42 | | private readonly AsyncManualResetEvent _asyncManualResetEvent; |
| | | 43 | | private bool _openCalled; |
| | | 44 | | private AsyncLock _thisLock; |
| | | 45 | | |
| | 2318 | 46 | | internal ChannelHandler(MessageVersion messageVersion, IChannelBinder binder, ServiceThrottle throttle, |
| | 2318 | 47 | | ServiceDispatcher serviceDispatcher, bool wasChannelThrottled, SessionIdleManager idleManager) |
| | | 48 | | { |
| | 2318 | 49 | | ChannelDispatcher channelDispatcher = serviceDispatcher.ChannelDispatcher; |
| | 2318 | 50 | | _serviceDispatcher = serviceDispatcher; |
| | 2318 | 51 | | _messageVersion = messageVersion; |
| | 2318 | 52 | | _isManualAddressing = channelDispatcher.ManualAddressing; |
| | 2318 | 53 | | _binder = binder; |
| | 2318 | 54 | | _throttle = throttle; |
| | 2318 | 55 | | _wasChannelThrottled = wasChannelThrottled; |
| | 2318 | 56 | | _host = channelDispatcher.Host; |
| | 2318 | 57 | | _duplexBinder = binder as DuplexChannelBinder; |
| | 2318 | 58 | | _hasSession = binder.HasSession; |
| | 2318 | 59 | | _isConcurrent = ConcurrencyBehavior.IsConcurrent(channelDispatcher, _hasSession); |
| | 2318 | 60 | | _thisLock = new AsyncLock(); |
| | | 61 | | |
| | | 62 | | // TODO: Work out if MultipleReceiveBinder is necessary |
| | | 63 | | //if (channelDispatcher.MaxPendingReceives > 1) |
| | | 64 | | //{ |
| | | 65 | | // // We need to preserve order if the ChannelHandler is not concurrent. |
| | | 66 | | // this.binder = new MultipleReceiveBinder( |
| | | 67 | | // this.binder, |
| | | 68 | | // channelDispatcher.MaxPendingReceives, |
| | | 69 | | // !this.isConcurrent); |
| | | 70 | | //} |
| | | 71 | | |
| | 2318 | 72 | | _idleManager = idleManager; |
| | | 73 | | |
| | 2318 | 74 | | if (_binder.HasSession) |
| | | 75 | | { |
| | 87 | 76 | | _sessionOpenNotification = _binder.Channel.GetProperty<SessionOpenNotification>(); |
| | 87 | 77 | | _needToCreateSessionOpenNotificationMessage = _sessionOpenNotification != null && _sessionOpenNotificati |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | //_requestInfo = new RequestInfo(this); |
| | | 81 | | |
| | | 82 | | // TODO: Wire up lifetime management in place of listener state |
| | | 83 | | //if (this.listener.State == CommunicationState.Opened) |
| | | 84 | | //{ |
| | 2318 | 85 | | _serviceDispatcher.ChannelDispatcher.Channels.IncrementActivityCount(); |
| | 2318 | 86 | | _incrementedActivityCountInConstructor = true; |
| | | 87 | | //} |
| | 2318 | 88 | | _asyncManualResetEvent = new AsyncManualResetEvent(); |
| | 2318 | 89 | | } |
| | | 90 | | |
| | | 91 | | internal IServiceChannelDispatcher GetDispatcher() |
| | | 92 | | { |
| | 2318 | 93 | | return _binder; |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | internal async Task OpenAsync() |
| | | 97 | | { |
| | 2318 | 98 | | _binder.SetNextDispatcher(this); |
| | 2318 | 99 | | Exception exception = null; |
| | | 100 | | try |
| | | 101 | | { |
| | 2318 | 102 | | await _binder.Channel.OpenAsync(); |
| | 2318 | 103 | | } |
| | 0 | 104 | | catch (Exception e) |
| | | 105 | | { |
| | 0 | 106 | | if (Fx.IsFatal(e)) |
| | | 107 | | { |
| | 0 | 108 | | throw; |
| | | 109 | | } |
| | 0 | 110 | | exception = e; |
| | 0 | 111 | | } |
| | | 112 | | |
| | 2318 | 113 | | if (exception != null) |
| | | 114 | | { |
| | | 115 | | //if (DiagnosticUtility.ShouldTraceWarning) |
| | | 116 | | //{ |
| | | 117 | | // TraceUtility.TraceEvent(System.Diagnostics.TraceEventType.Warning, |
| | | 118 | | // TraceCode.FailedToOpenIncomingChannel, |
| | | 119 | | // SR.GetString(SR.TraceCodeFailedToOpenIncomingChannel)); |
| | | 120 | | //} |
| | 0 | 121 | | if ((_throttle != null) && _hasSession) |
| | | 122 | | { |
| | 0 | 123 | | _throttle.DeactivateChannel(); |
| | | 124 | | } |
| | | 125 | | |
| | 0 | 126 | | bool errorHandled = HandleError(exception); |
| | | 127 | | |
| | | 128 | | //if (this.incrementedActivityCountInConstructor) |
| | | 129 | | //{ |
| | | 130 | | // this.listener.ChannelDispatcher.Channels.DecrementActivityCount(); |
| | | 131 | | //} |
| | | 132 | | |
| | 0 | 133 | | if (!errorHandled) |
| | | 134 | | { |
| | 0 | 135 | | _binder.Channel.Abort(); |
| | | 136 | | } |
| | | 137 | | } |
| | | 138 | | else |
| | | 139 | | { |
| | 2318 | 140 | | ReleasePump(); |
| | 2318 | 141 | | _shouldRejectMessageWithOnOpenActionHeader = !_needToCreateSessionOpenNotificationMessage; |
| | 2318 | 142 | | if (_needToCreateSessionOpenNotificationMessage) |
| | | 143 | | { |
| | 1 | 144 | | _needToCreateSessionOpenNotificationMessage = false; |
| | 1 | 145 | | RequestContext requestContext = GetSessionOpenNotificationRequestContext(); |
| | 1 | 146 | | await HandleReceiveCompleteAsync(requestContext); |
| | 1 | 147 | | HandleRequestAsync(requestContext); |
| | 1 | 148 | | } |
| | 2318 | 149 | | ReleasePump(); |
| | | 150 | | } |
| | | 151 | | |
| | 2318 | 152 | | _openCalled = true; |
| | 2318 | 153 | | } |
| | | 154 | | |
| | 0 | 155 | | internal bool HasRegisterBeenCalled { get; set; } |
| | | 156 | | |
| | | 157 | | internal InstanceContext InstanceContext |
| | | 158 | | { |
| | 0 | 159 | | get { return _channel?.InstanceContext; } |
| | | 160 | | } |
| | | 161 | | |
| | 2932 | 162 | | internal ServiceThrottle InstanceContextServiceThrottle { get; set; } |
| | | 163 | | |
| | | 164 | | private bool IsOpen |
| | | 165 | | { |
| | 19 | 166 | | get { return _binder.Channel.State == CommunicationState.Opened; } |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | private EndpointAddress LocalAddress |
| | | 170 | | { |
| | | 171 | | get |
| | | 172 | | { |
| | 1 | 173 | | if (_binder != null) |
| | | 174 | | { |
| | 1 | 175 | | if (_binder.Channel is IInputChannel input) |
| | | 176 | | { |
| | 1 | 177 | | return input.LocalAddress; |
| | | 178 | | } |
| | | 179 | | |
| | 0 | 180 | | if (_binder.Channel is IReplyChannel reply) |
| | | 181 | | { |
| | 0 | 182 | | return reply.LocalAddress; |
| | | 183 | | } |
| | | 184 | | } |
| | | 185 | | |
| | 0 | 186 | | return null; |
| | | 187 | | } |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | public async Task CloseBinderAsync() |
| | | 191 | | { |
| | | 192 | | try |
| | | 193 | | { |
| | 3 | 194 | | await _binder.Channel.CloseAsync(); |
| | 2 | 195 | | } |
| | 1 | 196 | | catch (Exception e) |
| | | 197 | | { |
| | 1 | 198 | | if (Fx.IsFatal(e)) |
| | | 199 | | { |
| | 0 | 200 | | throw; |
| | | 201 | | } |
| | | 202 | | |
| | 1 | 203 | | HandleError(e); |
| | 1 | 204 | | } |
| | 3 | 205 | | } |
| | | 206 | | |
| | | 207 | | public void EnsureReceive() |
| | | 208 | | { |
| | 2536 | 209 | | _asyncManualResetEvent.Set(); |
| | 2536 | 210 | | } |
| | | 211 | | |
| | | 212 | | public Task DispatchAsync(Message message) |
| | | 213 | | { |
| | 0 | 214 | | RequestContext requestContext = _binder.CreateRequestContext(message); |
| | 0 | 215 | | return DispatchAsync(requestContext); |
| | | 216 | | } |
| | | 217 | | |
| | | 218 | | public async Task DispatchAsync(RequestContext requestContext) |
| | | 219 | | { |
| | 2685 | 220 | | if (!_openCalled) |
| | | 221 | | { |
| | 0 | 222 | | throw TraceUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SRCommon.CommunicationObject |
| | | 223 | | } |
| | | 224 | | |
| | 2685 | 225 | | await TryAcquirePumpAsync(); |
| | 2685 | 226 | | await HandleReceiveCompleteAsync(requestContext); |
| | 2685 | 227 | | if (requestContext == null) |
| | | 228 | | { |
| | 128 | 229 | | return; |
| | | 230 | | } |
| | | 231 | | |
| | | 232 | | // Don't await handling the request to allow caller to start receving the next incoming message |
| | 2557 | 233 | | HandleRequestAsync(requestContext); |
| | 2685 | 234 | | } |
| | | 235 | | |
| | | 236 | | private RequestContext GetSessionOpenNotificationRequestContext() |
| | | 237 | | { |
| | | 238 | | Fx.Assert(_sessionOpenNotification != null, "this.sessionOpenNotification should not be null."); |
| | 1 | 239 | | Message message = Message.CreateMessage(_binder.Channel.GetProperty<MessageVersion>() ?? _messageVersion, Op |
| | | 240 | | Fx.Assert(LocalAddress != null, "this.LocalAddress should not be null."); |
| | 1 | 241 | | message.Headers.To = LocalAddress.Uri; |
| | 1 | 242 | | _sessionOpenNotification.UpdateMessageProperties(message.Properties); |
| | 1 | 243 | | return _binder.CreateRequestContext(message); |
| | | 244 | | } |
| | | 245 | | |
| | | 246 | | private async void HandleRequestAsync(RequestContext request) |
| | | 247 | | { |
| | 2558 | 248 | | if (request == null) |
| | | 249 | | { |
| | | 250 | | // channel EOF, stop receiving |
| | 0 | 251 | | return; |
| | | 252 | | } |
| | | 253 | | |
| | 2558 | 254 | | var requestInfo = new RequestInfo(this); |
| | | 255 | | |
| | | 256 | | //ServiceModelActivity activity = DiagnosticUtility.ShouldUseActivity ? TraceUtility.ExtractActivity(request |
| | | 257 | | |
| | | 258 | | //using (ServiceModelActivity.BoundOperation(activity)) |
| | | 259 | | //{ |
| | 2558 | 260 | | if (HandleRequestAsReply(request)) |
| | | 261 | | { |
| | 1 | 262 | | ReleasePump(); |
| | 1 | 263 | | return; |
| | | 264 | | } |
| | | 265 | | |
| | 2557 | 266 | | if (_isChannelTerminated) |
| | | 267 | | { |
| | 0 | 268 | | ReleasePump(); |
| | 0 | 269 | | await ReplyChannelTerminatedAsync(request, requestInfo); |
| | 0 | 270 | | return; |
| | | 271 | | } |
| | | 272 | | |
| | 2557 | 273 | | requestInfo.RequestContext = request; |
| | | 274 | | |
| | 2557 | 275 | | await TryAcquireCallThrottleAsync(request); |
| | | 276 | | |
| | | 277 | | Fx.Assert(!requestInfo.ChannelHandlerOwnsCallThrottle, "ChannelHandler.HandleRequest: this.requestInfo.Chann |
| | | 278 | | |
| | 2557 | 279 | | requestInfo.ChannelHandlerOwnsCallThrottle = true; |
| | | 280 | | |
| | 2557 | 281 | | if (!await TryRetrievingInstanceContextAsync(request, requestInfo)) |
| | | 282 | | { |
| | | 283 | | //Would have replied and close the request. |
| | 18 | 284 | | return; |
| | | 285 | | } |
| | | 286 | | |
| | 2539 | 287 | | requestInfo.Channel.CompletedIOOperation(); |
| | | 288 | | |
| | | 289 | | //Only acquire InstanceContext throttle if one doesnt already exist. |
| | 2539 | 290 | | await TryAcquireThrottleAsync(request, requestInfo.ExistingInstanceContext == null); |
| | | 291 | | Fx.Assert(!requestInfo.ChannelHandlerOwnsInstanceContextThrottle, "ChannelHandler.HandleRequest: this.reques |
| | 2539 | 292 | | requestInfo.ChannelHandlerOwnsInstanceContextThrottle = (requestInfo.ExistingInstanceContext == null); |
| | | 293 | | |
| | 2539 | 294 | | await DispatchAndReleasePumpAsync(request, true, requestInfo); |
| | | 295 | | //} |
| | 2558 | 296 | | } |
| | | 297 | | |
| | | 298 | | private async Task DispatchAndReleasePumpAsync(RequestContext request, bool cleanThread, RequestInfo requestInfo |
| | | 299 | | { |
| | 2539 | 300 | | OperationContext currentOperationContext = null; |
| | 2539 | 301 | | ServiceChannel channel = requestInfo.Channel; |
| | 2539 | 302 | | EndpointDispatcher endpoint = requestInfo.Endpoint; |
| | 2539 | 303 | | bool releasedPump = false; |
| | | 304 | | |
| | | 305 | | try |
| | | 306 | | { |
| | 2539 | 307 | | DispatchRuntime dispatchBehavior = requestInfo.DispatchRuntime; |
| | | 308 | | |
| | 2539 | 309 | | if (channel == null || dispatchBehavior == null) |
| | | 310 | | { |
| | | 311 | | Fx.Assert("System.ServiceModel.Dispatcher.ChannelHandler.Dispatch(): (channel == null || dispatchBeh |
| | 0 | 312 | | return; |
| | | 313 | | } |
| | | 314 | | |
| | | 315 | | Message message; |
| | | 316 | | |
| | | 317 | | //EventTraceActivity eventTraceActivity = TraceDispatchMessageStart(request.RequestMessage); |
| | 2539 | 318 | | message = request.RequestMessage; |
| | | 319 | | |
| | 2539 | 320 | | DispatchOperationRuntime operation = dispatchBehavior.GetOperation(ref message); |
| | 2538 | 321 | | if (operation == null) |
| | | 322 | | { |
| | | 323 | | Fx.Assert("ChannelHandler.Dispatch (operation == null)"); |
| | 0 | 324 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(string.Forma |
| | | 325 | | } |
| | | 326 | | |
| | 2538 | 327 | | if (_shouldRejectMessageWithOnOpenActionHeader && message.Headers.Action == OperationDescription.Session |
| | | 328 | | { |
| | 0 | 329 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 330 | | } |
| | | 331 | | |
| | | 332 | | //if (MessageLogger.LoggingEnabled) |
| | | 333 | | //{ |
| | | 334 | | // MessageLogger.LogMessage(ref message, (operation.IsOneWay ? MessageLoggingSource.ServiceLevelRecei |
| | | 335 | | //} |
| | | 336 | | |
| | 2538 | 337 | | if (operation.IsTerminating && _hasSession) |
| | | 338 | | { |
| | 4 | 339 | | _isChannelTerminated = true; |
| | | 340 | | } |
| | | 341 | | |
| | | 342 | | bool hasOperationContextBeenSet; |
| | 2538 | 343 | | if (currentOperationContext != null) |
| | | 344 | | { |
| | 0 | 345 | | hasOperationContextBeenSet = true; |
| | 0 | 346 | | currentOperationContext.ReInit(request, message, channel); |
| | | 347 | | } |
| | | 348 | | else |
| | | 349 | | { |
| | 2538 | 350 | | hasOperationContextBeenSet = false; |
| | 2538 | 351 | | currentOperationContext = new OperationContext(request, message, channel, _host); |
| | | 352 | | } |
| | | 353 | | |
| | 2538 | 354 | | if (currentOperationContext.EndpointDispatcher == null && _serviceDispatcher != null) |
| | | 355 | | { |
| | 2538 | 356 | | currentOperationContext.EndpointDispatcher = endpoint; |
| | | 357 | | } |
| | | 358 | | |
| | 2538 | 359 | | var rpc = new MessageRpc(request, message, operation, channel, _host, |
| | 2538 | 360 | | this, cleanThread, currentOperationContext, requestInfo.ExistingInstanceContext); |
| | | 361 | | |
| | | 362 | | //TraceUtility.MessageFlowAtMessageReceived(message, currentOperationContext, eventTraceActivity, true); |
| | | 363 | | |
| | | 364 | | // passing responsibility for call throttle to MessageRpc |
| | | 365 | | // (MessageRpc implicitly owns this throttle once it's created) |
| | 2538 | 366 | | requestInfo.ChannelHandlerOwnsCallThrottle = false; |
| | | 367 | | // explicitly passing responsibility for instance throttle to MessageRpc |
| | 2538 | 368 | | rpc.MessageRpcOwnsInstanceContextThrottle = requestInfo.ChannelHandlerOwnsInstanceContextThrottle; |
| | 2538 | 369 | | requestInfo.ChannelHandlerOwnsInstanceContextThrottle = false; |
| | | 370 | | |
| | | 371 | | // These need to happen before Dispatch but after accessing any ChannelHandler |
| | | 372 | | // state, because we go multi-threaded after this |
| | 2538 | 373 | | ReleasePump(); |
| | 2538 | 374 | | releasedPump = true; |
| | | 375 | | |
| | 2538 | 376 | | await operation.Parent.DispatchAsync(rpc, hasOperationContextBeenSet); |
| | 2538 | 377 | | } |
| | 1 | 378 | | catch (Exception e) |
| | | 379 | | { |
| | 1 | 380 | | if (Fx.IsFatal(e)) |
| | | 381 | | { |
| | 0 | 382 | | throw; |
| | | 383 | | } |
| | | 384 | | |
| | 1 | 385 | | await HandleErrorAsync(e, request, requestInfo, channel); |
| | | 386 | | } |
| | | 387 | | finally |
| | | 388 | | { |
| | 2539 | 389 | | if (!releasedPump) |
| | | 390 | | { |
| | 1 | 391 | | ReleasePump(); |
| | | 392 | | } |
| | | 393 | | } |
| | 2539 | 394 | | } |
| | | 395 | | |
| | | 396 | | private async Task HandleReceiveCompleteAsync(RequestContext request) |
| | | 397 | | { |
| | | 398 | | try |
| | | 399 | | { |
| | 2686 | 400 | | if (_channel != null) |
| | | 401 | | { |
| | 102 | 402 | | await _channel.HandleReceiveCompleteAsync(request); |
| | | 403 | | } |
| | | 404 | | else |
| | | 405 | | { |
| | 2584 | 406 | | if (request == null && _hasSession) |
| | | 407 | | { |
| | | 408 | | bool close; |
| | 3 | 409 | | await using (await _thisLock.TakeLockAsync()) |
| | | 410 | | { |
| | 3 | 411 | | close = !_doneReceiving; |
| | 3 | 412 | | _doneReceiving = true; |
| | | 413 | | } |
| | | 414 | | |
| | 3 | 415 | | if (close) |
| | | 416 | | { |
| | 3 | 417 | | await CloseBinderAsync(); |
| | | 418 | | |
| | 3 | 419 | | if (_idleManager != null) |
| | | 420 | | { |
| | 3 | 421 | | _idleManager.CancelTimer(); |
| | | 422 | | } |
| | | 423 | | |
| | 3 | 424 | | ServiceThrottle throttle = _throttle; |
| | 3 | 425 | | if (throttle != null) |
| | | 426 | | { |
| | 0 | 427 | | throttle.DeactivateChannel(); |
| | | 428 | | } |
| | | 429 | | } |
| | | 430 | | } |
| | | 431 | | } |
| | 2686 | 432 | | } |
| | | 433 | | finally |
| | | 434 | | { |
| | 2686 | 435 | | if ((request == null) && _incrementedActivityCountInConstructor) |
| | | 436 | | { |
| | 128 | 437 | | _serviceDispatcher.ChannelDispatcher.Channels.DecrementActivityCount(); |
| | | 438 | | } |
| | | 439 | | } |
| | 2686 | 440 | | } |
| | | 441 | | |
| | | 442 | | private bool HandleRequestAsReply(RequestContext request) |
| | | 443 | | { |
| | 2558 | 444 | | if (_duplexBinder != null) |
| | | 445 | | { |
| | 89 | 446 | | return _duplexBinder.HandleRequestAsReply(request.RequestMessage); |
| | | 447 | | } |
| | | 448 | | |
| | 2469 | 449 | | return false; |
| | | 450 | | } |
| | | 451 | | |
| | | 452 | | private async Task EnsureChannelAndEndpointAsync(RequestContext request, RequestInfo requestInfo) |
| | | 453 | | { |
| | 2557 | 454 | | requestInfo.Channel = _channel; |
| | | 455 | | |
| | 2557 | 456 | | if (requestInfo.Channel == null) |
| | | 457 | | { |
| | | 458 | | (ServiceChannel serviceChannel, EndpointDispatcher endpoint, bool addressMatched) channelInfo; |
| | 2534 | 459 | | if (_hasSession) |
| | | 460 | | { |
| | 85 | 461 | | channelInfo = await GetSessionChannelAsync(request.RequestMessage); |
| | | 462 | | } |
| | | 463 | | else |
| | | 464 | | { |
| | 2449 | 465 | | channelInfo = await GetDatagramChannelAsync(request.RequestMessage); |
| | | 466 | | } |
| | | 467 | | |
| | 2518 | 468 | | requestInfo.Channel = channelInfo.serviceChannel; |
| | 2518 | 469 | | requestInfo.Endpoint = channelInfo.endpoint; |
| | | 470 | | |
| | 2518 | 471 | | if (requestInfo.Channel == null) |
| | | 472 | | { |
| | | 473 | | // TODO: Enable UnknownMessageReceived handler |
| | | 474 | | //this.host.RaiseUnknownMessageReceived(request.RequestMessage); |
| | 2 | 475 | | if (channelInfo.addressMatched) |
| | | 476 | | { |
| | 2 | 477 | | await ReplyContractFilterDidNotMatchAsync(request, requestInfo); |
| | | 478 | | } |
| | | 479 | | else |
| | | 480 | | { |
| | 0 | 481 | | await ReplyAddressFilterDidNotMatchAsync(request, requestInfo); |
| | | 482 | | } |
| | | 483 | | } |
| | | 484 | | } |
| | | 485 | | else |
| | | 486 | | { |
| | 23 | 487 | | requestInfo.Endpoint = requestInfo.Channel.EndpointDispatcher; |
| | | 488 | | |
| | | 489 | | //For sessionful contracts, the InstanceContext throttle is not copied over to the channel |
| | | 490 | | //as we create the channel before acquiring the lock |
| | 23 | 491 | | if (InstanceContextServiceThrottle != null && requestInfo.Channel.InstanceContextServiceThrottle == null |
| | | 492 | | { |
| | 0 | 493 | | requestInfo.Channel.InstanceContextServiceThrottle = InstanceContextServiceThrottle; |
| | | 494 | | } |
| | | 495 | | } |
| | | 496 | | |
| | 2541 | 497 | | requestInfo.EndpointLookupDone = true; |
| | | 498 | | |
| | 2541 | 499 | | if (requestInfo.Channel == null) |
| | | 500 | | { |
| | | 501 | | // SFx drops a message here |
| | 2 | 502 | | TraceUtility.TraceDroppedMessage(request.RequestMessage, requestInfo.Endpoint); |
| | 2 | 503 | | await request.CloseAsync(); |
| | 2 | 504 | | return; |
| | | 505 | | } |
| | | 506 | | |
| | 2539 | 507 | | if (requestInfo.Channel.HasSession) |
| | | 508 | | { |
| | 106 | 509 | | requestInfo.DispatchRuntime = requestInfo.Channel.DispatchRuntime; |
| | | 510 | | } |
| | | 511 | | else |
| | | 512 | | { |
| | 2433 | 513 | | requestInfo.DispatchRuntime = requestInfo.Endpoint.DispatchRuntime; |
| | | 514 | | } |
| | 2541 | 515 | | } |
| | | 516 | | |
| | | 517 | | private async Task<(ServiceChannel serviceChannel, EndpointDispatcher endpoint, bool addressMatched)> GetDatagra |
| | | 518 | | { |
| | 2433 | 519 | | var endpoint = GetEndpointDispatcher(message, out var addressMatched); |
| | | 520 | | |
| | 2433 | 521 | | if (endpoint == null) |
| | | 522 | | { |
| | 0 | 523 | | return (null, null, addressMatched); |
| | | 524 | | } |
| | | 525 | | |
| | 2433 | 526 | | if (endpoint.DatagramChannel == null) |
| | | 527 | | { |
| | 433 | 528 | | await using (await _serviceDispatcher.ThisLock.TakeLockAsync()) |
| | | 529 | | { |
| | 433 | 530 | | if (endpoint.DatagramChannel == null) |
| | | 531 | | { |
| | 433 | 532 | | endpoint.DatagramChannel = new ServiceChannel(_binder, endpoint, _serviceDispatcher, |
| | 433 | 533 | | _idleManager.UseIfNeeded(_binder, _serviceDispatcher.Binding.ReceiveTimeout)); |
| | 433 | 534 | | await InitializeServiceChannelAsync(endpoint.DatagramChannel); |
| | | 535 | | } |
| | | 536 | | } |
| | | 537 | | } |
| | | 538 | | |
| | 2433 | 539 | | return (endpoint.DatagramChannel, endpoint, addressMatched); |
| | 2433 | 540 | | } |
| | | 541 | | |
| | | 542 | | private async Task<(ServiceChannel serviceChannel, EndpointDispatcher endpoint, bool addressMatched)> GetSession |
| | | 543 | | { |
| | 85 | 544 | | var addressMatched = false; |
| | | 545 | | |
| | 85 | 546 | | if (_channel == null) |
| | | 547 | | { |
| | 85 | 548 | | await using (await _thisLock.TakeLockAsync()) |
| | | 549 | | { |
| | 85 | 550 | | if (_channel == null) |
| | | 551 | | { |
| | 85 | 552 | | var endpoint = GetEndpointDispatcher(message, out addressMatched); |
| | 85 | 553 | | if (endpoint != null) |
| | | 554 | | { |
| | 83 | 555 | | _channel = new ServiceChannel(_binder, endpoint, _serviceDispatcher, |
| | 83 | 556 | | _idleManager.UseIfNeeded(_binder, _serviceDispatcher.Binding.ReceiveTimeout)); |
| | 83 | 557 | | await InitializeServiceChannelAsync(_channel); |
| | | 558 | | } |
| | | 559 | | } |
| | | 560 | | } |
| | | 561 | | } |
| | | 562 | | |
| | 85 | 563 | | return (_channel, _channel?.EndpointDispatcher, addressMatched); |
| | 85 | 564 | | } |
| | | 565 | | |
| | | 566 | | private Task InitializeServiceChannelAsync(ServiceChannel channel) |
| | | 567 | | { |
| | 516 | 568 | | if (_wasChannelThrottled) |
| | | 569 | | { |
| | | 570 | | // Comment preserved from .NET Framework: |
| | | 571 | | // When the idle timeout was hit, the constructor of ServiceChannel will abort itself directly. So |
| | | 572 | | // the session throttle will not be released and thus lead to a service unavailablity. |
| | | 573 | | // Note that if the channel is already aborted, the next line "channel.ServiceThrottle = this.throttle;" |
| | | 574 | | // so we are not going to do any more work inside this method. |
| | | 575 | | // Ideally we should do a thorough refactoring work for this throttling issue. However, it's too risky. |
| | | 576 | | // this in a whole release. |
| | | 577 | | // Note that the "wasChannelThrottled" boolean will only be true if we aquired the session throttle. So |
| | | 578 | | // again here. |
| | 0 | 579 | | if (channel.Aborted && _throttle != null) |
| | | 580 | | { |
| | | 581 | | // This line will release the "session" throttle. |
| | 0 | 582 | | _throttle.DeactivateChannel(); |
| | | 583 | | } |
| | | 584 | | |
| | 0 | 585 | | channel.ServiceThrottle = _throttle; |
| | | 586 | | } |
| | | 587 | | |
| | 516 | 588 | | if (InstanceContextServiceThrottle != null) |
| | | 589 | | { |
| | 0 | 590 | | channel.InstanceContextServiceThrottle = InstanceContextServiceThrottle; |
| | | 591 | | } |
| | | 592 | | |
| | 516 | 593 | | ClientRuntime clientRuntime = channel.ClientRuntime; |
| | 516 | 594 | | if (clientRuntime != null) |
| | | 595 | | { |
| | 516 | 596 | | Type contractType = clientRuntime.ContractClientType; |
| | 516 | 597 | | Type callbackType = clientRuntime.CallbackClientType; |
| | | 598 | | |
| | 516 | 599 | | if (contractType != null) |
| | | 600 | | { |
| | 1 | 601 | | channel.Proxy = ServiceChannelFactory.CreateProxy(contractType, callbackType, MessageDirection.Outpu |
| | | 602 | | } |
| | | 603 | | } |
| | | 604 | | |
| | 516 | 605 | | if (_serviceDispatcher != null) |
| | | 606 | | { |
| | 516 | 607 | | _serviceDispatcher.ChannelDispatcher.InitializeChannel((IClientChannel)channel.Proxy); |
| | | 608 | | } |
| | | 609 | | |
| | 516 | 610 | | return ((IChannel)channel).OpenAsync(); |
| | | 611 | | } |
| | | 612 | | |
| | | 613 | | private void ProvideFault(Exception e, RequestInfo requestInfo, ref ErrorHandlerFaultInfo faultInfo) |
| | | 614 | | { |
| | 19 | 615 | | if (_serviceDispatcher != null) |
| | | 616 | | { |
| | 19 | 617 | | _serviceDispatcher.ChannelDispatcher.ProvideFault(e, requestInfo.Channel == null ? _binder.Channel.GetPr |
| | | 618 | | } |
| | | 619 | | // No client yet |
| | | 620 | | //else if (_channel != null) |
| | | 621 | | //{ |
| | | 622 | | // DispatchRuntime dispatchBehavior = _channel.ClientRuntime.CallbackDispatchRuntime; |
| | | 623 | | // dispatchBehavior.ChannelDispatcher.ProvideFault(e, this.channel.GetProperty<FaultConverter>(), ref fau |
| | | 624 | | //} |
| | 19 | 625 | | } |
| | | 626 | | |
| | | 627 | | internal bool HandleError(Exception e) |
| | | 628 | | { |
| | 2 | 629 | | ErrorHandlerFaultInfo dummy = new ErrorHandlerFaultInfo(); |
| | 2 | 630 | | return HandleError(e, ref dummy); |
| | | 631 | | } |
| | | 632 | | |
| | | 633 | | private bool HandleError(Exception e, ref ErrorHandlerFaultInfo faultInfo) |
| | | 634 | | { |
| | 4 | 635 | | if (e == null) |
| | | 636 | | { |
| | | 637 | | Fx.Assert(SR.SFxNonExceptionThrown); |
| | 0 | 638 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.SFxNonExcepti |
| | | 639 | | } |
| | 4 | 640 | | if (_serviceDispatcher != null) |
| | | 641 | | { |
| | 4 | 642 | | return _serviceDispatcher.ChannelDispatcher.HandleError(e, ref faultInfo); |
| | | 643 | | } |
| | | 644 | | // No client yet. |
| | | 645 | | //else if (this.channel != null) |
| | | 646 | | //{ |
| | | 647 | | // return this.channel.ClientRuntime.CallbackDispatchRuntime.ChannelDispatcher.HandleError(e, ref faultIn |
| | | 648 | | //} |
| | | 649 | | else |
| | | 650 | | { |
| | 0 | 651 | | return false; |
| | | 652 | | } |
| | | 653 | | } |
| | | 654 | | |
| | | 655 | | private Task HandleErrorAsync(Exception e, RequestContext request, RequestInfo requestInfo, ServiceChannel chann |
| | | 656 | | { |
| | 17 | 657 | | var faultInfo = new ErrorHandlerFaultInfo(_messageVersion.Addressing.DefaultFaultAction); |
| | 17 | 658 | | return ProvideFaultAndReplyFailureAsync(request, requestInfo, e, faultInfo); |
| | | 659 | | } |
| | | 660 | | |
| | | 661 | | private Task ReplyAddressFilterDidNotMatchAsync(RequestContext request, RequestInfo requestInfo) |
| | | 662 | | { |
| | 0 | 663 | | FaultCode code = FaultCode.CreateSenderFaultCode(AddressingStrings.DestinationUnreachable, |
| | 0 | 664 | | _messageVersion.Addressing.Namespace); |
| | 0 | 665 | | string reason = SR.Format(SR.SFxNoEndpointMatchingAddress, request.RequestMessage.Headers.To); |
| | | 666 | | |
| | 0 | 667 | | return ReplyFailureAsync(request, requestInfo, code, reason); |
| | | 668 | | } |
| | | 669 | | |
| | | 670 | | private Task ReplyContractFilterDidNotMatchAsync(RequestContext request, RequestInfo requestInfo) |
| | | 671 | | { |
| | | 672 | | // By default, the contract filter is just a filter over the set of initiating actions in |
| | | 673 | | // the contract, so we do error messages accordingly |
| | 2 | 674 | | AddressingVersion addressingVersion = _messageVersion.Addressing; |
| | 2 | 675 | | if (addressingVersion != AddressingVersion.None && request.RequestMessage.Headers.Action == null) |
| | | 676 | | { |
| | 0 | 677 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 678 | | new MessageHeaderException( |
| | 0 | 679 | | SR.Format(SR.SFxMissingActionHeader, addressingVersion.Namespace), AddressingStrings.Action, address |
| | | 680 | | } |
| | | 681 | | else |
| | | 682 | | { |
| | | 683 | | // some of this code is duplicated in DispatchRuntime.UnhandledActionInvoker |
| | | 684 | | // ideally both places would use FaultConverter and ActionNotSupportedException |
| | 2 | 685 | | FaultCode code = FaultCode.CreateSenderFaultCode(AddressingStrings.ActionNotSupported, |
| | 2 | 686 | | _messageVersion.Addressing.Namespace); |
| | 2 | 687 | | string reason = SR.Format(SR.SFxNoEndpointMatchingContract, request.RequestMessage.Headers.Action); |
| | 2 | 688 | | return ReplyFailureAsync(request, requestInfo, code, reason, _messageVersion.Addressing.FaultAction); |
| | | 689 | | } |
| | | 690 | | } |
| | | 691 | | |
| | | 692 | | private Task ReplyChannelTerminatedAsync(RequestContext request, RequestInfo requestInfo) |
| | | 693 | | { |
| | 0 | 694 | | FaultCode code = FaultCode.CreateSenderFaultCode(FaultCodeConstants.Codes.SessionTerminated, |
| | 0 | 695 | | FaultCodeConstants.Namespaces.NetDispatch); |
| | 0 | 696 | | string reason = SR.SFxChannelTerminated0; |
| | 0 | 697 | | string action = FaultCodeConstants.Actions.NetDispatcher; |
| | 0 | 698 | | Message fault = Message.CreateMessage(_messageVersion, code, reason, action); |
| | 0 | 699 | | return ReplyFailureAsync(request, requestInfo, fault, action, reason, code); |
| | | 700 | | } |
| | | 701 | | |
| | | 702 | | private Task ReplyFailureAsync(RequestContext request, RequestInfo requestInfo, FaultCode code, string reason) |
| | | 703 | | { |
| | 0 | 704 | | string action = _messageVersion.Addressing.DefaultFaultAction; |
| | 0 | 705 | | return ReplyFailureAsync(request, requestInfo, code, reason, action); |
| | | 706 | | } |
| | | 707 | | |
| | | 708 | | private Task ReplyFailureAsync(RequestContext request, RequestInfo requestInfo, FaultCode code, string reason, s |
| | | 709 | | { |
| | 2 | 710 | | Message fault = Message.CreateMessage(_messageVersion, code, reason, action); |
| | 2 | 711 | | return ReplyFailureAsync(request, requestInfo, fault, action, reason, code); |
| | | 712 | | } |
| | | 713 | | |
| | | 714 | | private async Task ReplyFailureAsync(RequestContext request, RequestInfo requestInfo, Message fault, string acti |
| | | 715 | | { |
| | 2 | 716 | | FaultException exception = new FaultException(reason, code); |
| | 2 | 717 | | ErrorBehavior.ThrowAndCatch(exception); |
| | 2 | 718 | | ErrorHandlerFaultInfo faultInfo = new ErrorHandlerFaultInfo(action) |
| | 2 | 719 | | { |
| | 2 | 720 | | Fault = fault |
| | 2 | 721 | | }; |
| | 2 | 722 | | faultInfo = await ProvideFaultAndReplyFailureAsync(request, requestInfo, exception, faultInfo); |
| | 2 | 723 | | HandleError(exception, ref faultInfo); |
| | 2 | 724 | | } |
| | | 725 | | |
| | | 726 | | private async Task<ErrorHandlerFaultInfo> ProvideFaultAndReplyFailureAsync(RequestContext request, RequestInfo r |
| | | 727 | | { |
| | 19 | 728 | | bool requestMessageIsFault = false; |
| | | 729 | | try |
| | | 730 | | { |
| | 19 | 731 | | requestMessageIsFault = request.RequestMessage.IsFault; |
| | 3 | 732 | | } |
| | | 733 | | catch (Exception e) |
| | | 734 | | { |
| | 16 | 735 | | if (Fx.IsFatal(e)) |
| | | 736 | | { |
| | 0 | 737 | | throw; |
| | | 738 | | } |
| | | 739 | | // swallow it |
| | 16 | 740 | | } |
| | | 741 | | |
| | 19 | 742 | | bool enableFaults = false; |
| | 19 | 743 | | if (_serviceDispatcher != null) |
| | | 744 | | { |
| | 19 | 745 | | enableFaults = _serviceDispatcher.ChannelDispatcher.EnableFaults; |
| | | 746 | | } |
| | | 747 | | // No client yet. |
| | | 748 | | //else if (this._channel != null && this._channel.IsClient) |
| | | 749 | | //{ |
| | | 750 | | // enableFaults = this._channel.ClientRuntime.EnableFaults; |
| | | 751 | | //} |
| | | 752 | | |
| | 19 | 753 | | if ((!requestMessageIsFault) && enableFaults) |
| | | 754 | | { |
| | 19 | 755 | | ProvideFault(exception, requestInfo, ref faultInfo); |
| | 19 | 756 | | if (faultInfo.Fault != null) |
| | | 757 | | { |
| | 19 | 758 | | Message reply = faultInfo.Fault; |
| | | 759 | | try |
| | | 760 | | { |
| | | 761 | | try |
| | | 762 | | { |
| | 19 | 763 | | if (PrepareReply(request, reply)) |
| | | 764 | | { |
| | 19 | 765 | | await request.ReplyAsync(reply); |
| | | 766 | | } |
| | 19 | 767 | | } |
| | | 768 | | finally |
| | | 769 | | { |
| | 19 | 770 | | reply.Close(); |
| | | 771 | | } |
| | 19 | 772 | | } |
| | 0 | 773 | | catch (Exception e) |
| | | 774 | | { |
| | 0 | 775 | | if (Fx.IsFatal(e)) |
| | | 776 | | { |
| | 0 | 777 | | throw; |
| | | 778 | | } |
| | 0 | 779 | | HandleError(e); |
| | 0 | 780 | | } |
| | 19 | 781 | | } |
| | | 782 | | } |
| | | 783 | | |
| | 19 | 784 | | return faultInfo; |
| | 19 | 785 | | } |
| | | 786 | | |
| | | 787 | | /// <summary> |
| | | 788 | | /// Prepares a reply |
| | | 789 | | /// </summary> |
| | | 790 | | /// <param name="request">The request context to prepare</param> |
| | | 791 | | /// <param name="reply">The reply to prepare</param> |
| | | 792 | | /// <returns>True if channel is open and prepared reply should be sent; otherwise false.</returns> |
| | | 793 | | private bool PrepareReply(RequestContext request, Message reply) |
| | | 794 | | { |
| | | 795 | | // Ensure we only reply once (we may hit the same error multiple times) |
| | 19 | 796 | | if (_replied == request) |
| | | 797 | | { |
| | 0 | 798 | | return false; |
| | | 799 | | } |
| | | 800 | | |
| | 19 | 801 | | _replied = request; |
| | | 802 | | |
| | 19 | 803 | | bool canSendReply = true; |
| | | 804 | | |
| | 19 | 805 | | Message requestMessage = null; |
| | | 806 | | try |
| | | 807 | | { |
| | 19 | 808 | | requestMessage = request.RequestMessage; |
| | 3 | 809 | | } |
| | | 810 | | catch (Exception e) |
| | | 811 | | { |
| | 16 | 812 | | if (Fx.IsFatal(e)) |
| | | 813 | | { |
| | 0 | 814 | | throw; |
| | | 815 | | } |
| | | 816 | | // swallow it |
| | 16 | 817 | | } |
| | 19 | 818 | | if (!ReferenceEquals(requestMessage, null)) |
| | | 819 | | { |
| | 3 | 820 | | UniqueId requestID = null; |
| | | 821 | | try |
| | | 822 | | { |
| | 3 | 823 | | requestID = requestMessage.Headers.MessageId; |
| | 3 | 824 | | } |
| | | 825 | | #pragma warning disable CA1031 // Do not catch general exception types - intentionally swallowing this exception |
| | 0 | 826 | | catch (MessageHeaderException) |
| | | 827 | | { |
| | | 828 | | // swallow it - we don't need to correlate the reply if the MessageId header is bad |
| | 0 | 829 | | } |
| | | 830 | | #pragma warning restore CA1031 // Do not catch general exception types |
| | 3 | 831 | | if (!ReferenceEquals(requestID, null) && !_isManualAddressing) |
| | | 832 | | { |
| | 2 | 833 | | RequestReplyCorrelator.PrepareReply(reply, requestID); |
| | | 834 | | } |
| | 3 | 835 | | if (!_hasSession && !_isManualAddressing) |
| | | 836 | | { |
| | | 837 | | try |
| | | 838 | | { |
| | 1 | 839 | | canSendReply = RequestReplyCorrelator.AddressReply(reply, requestMessage); |
| | 1 | 840 | | } |
| | | 841 | | #pragma warning disable CA1031 // Do not catch general exception types - intentionally swallowing exception |
| | 0 | 842 | | catch (MessageHeaderException) |
| | | 843 | | { |
| | | 844 | | // swallow it - we don't need to address the reply if the FaultTo header is bad |
| | 0 | 845 | | } |
| | | 846 | | #pragma warning restore CA1031 // Do not catch general exception types |
| | | 847 | | } |
| | | 848 | | } |
| | | 849 | | |
| | | 850 | | // ObjectDisposeException can happen |
| | | 851 | | // if the channel is closed in a different |
| | | 852 | | // thread. 99% this check will avoid false |
| | | 853 | | // exceptions. |
| | 19 | 854 | | return IsOpen && canSendReply; |
| | | 855 | | } |
| | | 856 | | |
| | | 857 | | private EndpointDispatcher GetEndpointDispatcher(Message message, out bool addressMatched) |
| | | 858 | | { |
| | 2518 | 859 | | return _serviceDispatcher.Endpoints.Lookup(message, out addressMatched); |
| | | 860 | | } |
| | | 861 | | |
| | | 862 | | private Task TryAcquireThrottleAsync(RequestContext request, bool acquireInstanceContextThrottle) |
| | | 863 | | { |
| | 2539 | 864 | | ServiceThrottle throttle = _throttle; |
| | 2539 | 865 | | if ((throttle != null) && (throttle.IsActive)) |
| | | 866 | | { |
| | 0 | 867 | | return throttle.AcquireInstanceContextAndDynamicAsync(this, acquireInstanceContextThrottle); |
| | | 868 | | } |
| | | 869 | | |
| | 2539 | 870 | | return Task.CompletedTask; |
| | | 871 | | } |
| | | 872 | | |
| | | 873 | | private Task TryAcquireCallThrottleAsync(RequestContext request) |
| | | 874 | | { |
| | 2557 | 875 | | ServiceThrottle throttle = _throttle; |
| | 2557 | 876 | | if ((throttle != null) && (throttle.IsActive)) |
| | | 877 | | { |
| | 0 | 878 | | return throttle.AcquireCallAsync(); |
| | | 879 | | } |
| | | 880 | | |
| | 2557 | 881 | | return Task.CompletedTask; |
| | | 882 | | } |
| | | 883 | | |
| | | 884 | | private async Task<bool> TryRetrievingInstanceContextAsync(RequestContext request, RequestInfo requestInfo) |
| | | 885 | | { |
| | | 886 | | try |
| | | 887 | | { |
| | 2557 | 888 | | return await TryRetrievingInstanceContextCoreAsync(request, requestInfo); |
| | | 889 | | } |
| | | 890 | | catch (Exception ex) |
| | | 891 | | { |
| | 0 | 892 | | if (Fx.IsFatal(ex)) |
| | | 893 | | { |
| | 0 | 894 | | throw; |
| | | 895 | | } |
| | | 896 | | |
| | 0 | 897 | | DiagnosticUtility.TraceHandledException(ex, TraceEventType.Error); |
| | | 898 | | |
| | | 899 | | try |
| | | 900 | | { |
| | 0 | 901 | | await request.CloseAsync(); |
| | 0 | 902 | | } |
| | | 903 | | catch (Exception e) |
| | | 904 | | { |
| | 0 | 905 | | if (Fx.IsFatal(e)) |
| | | 906 | | { |
| | 0 | 907 | | throw; |
| | | 908 | | } |
| | | 909 | | |
| | 0 | 910 | | request.Abort(); |
| | 0 | 911 | | } |
| | | 912 | | |
| | 0 | 913 | | return false; |
| | | 914 | | } |
| | 2557 | 915 | | } |
| | | 916 | | |
| | | 917 | | //Return: False denotes failure, Caller should discard the request. |
| | | 918 | | // : True denotes operation is sucessful. |
| | | 919 | | private async Task<bool> TryRetrievingInstanceContextCoreAsync(RequestContext request, RequestInfo requestInfo) |
| | | 920 | | { |
| | 2557 | 921 | | bool releasePump = true; |
| | | 922 | | try |
| | | 923 | | { |
| | 2557 | 924 | | if (!requestInfo.EndpointLookupDone) |
| | | 925 | | { |
| | 2557 | 926 | | await EnsureChannelAndEndpointAsync(request, requestInfo); |
| | | 927 | | } |
| | | 928 | | |
| | 2541 | 929 | | if (requestInfo.Channel == null) |
| | | 930 | | { |
| | 2 | 931 | | return false; |
| | | 932 | | } |
| | | 933 | | |
| | 2539 | 934 | | if (requestInfo.DispatchRuntime != null) |
| | | 935 | | { |
| | 2539 | 936 | | IContextChannel transparentProxy = requestInfo.Channel.Proxy as IContextChannel; |
| | | 937 | | try |
| | | 938 | | { |
| | 2539 | 939 | | requestInfo.ExistingInstanceContext = requestInfo.DispatchRuntime.InstanceContextProvider.GetExi |
| | 2539 | 940 | | releasePump = false; |
| | 2539 | 941 | | } |
| | | 942 | | catch (Exception e) |
| | | 943 | | { |
| | 0 | 944 | | if (Fx.IsFatal(e)) |
| | | 945 | | { |
| | 0 | 946 | | throw; |
| | | 947 | | } |
| | 0 | 948 | | requestInfo.Channel = null; |
| | 0 | 949 | | await HandleErrorAsync(e, request, requestInfo, _channel); |
| | 0 | 950 | | return false; |
| | | 951 | | } |
| | | 952 | | } |
| | | 953 | | else |
| | | 954 | | { |
| | | 955 | | // This can happen if we are pumping for an async client, |
| | | 956 | | // and we receive a bogus reply. In that case, there is no |
| | | 957 | | // DispatchRuntime, because we are only expecting replies. |
| | | 958 | | // |
| | | 959 | | // One possible fix for this would be in DuplexChannelBinder |
| | | 960 | | // to drop all messages with a RelatesTo that do not match a |
| | | 961 | | // pending request. |
| | | 962 | | // |
| | | 963 | | // However, that would not fix: |
| | | 964 | | // (a) we could get a valid request message with a |
| | | 965 | | // RelatesTo that we should try to process. |
| | | 966 | | // (b) we could get a reply message that does not have |
| | | 967 | | // a RelatesTo. |
| | | 968 | | // |
| | | 969 | | // So we do the null check here. |
| | | 970 | | // |
| | | 971 | | // SFx drops a message here |
| | 0 | 972 | | await request.CloseAsync(); |
| | 0 | 973 | | return false; |
| | | 974 | | } |
| | 2539 | 975 | | } |
| | 16 | 976 | | catch (Exception e) |
| | | 977 | | { |
| | 16 | 978 | | if (Fx.IsFatal(e)) |
| | | 979 | | { |
| | 0 | 980 | | throw; |
| | | 981 | | } |
| | | 982 | | |
| | 16 | 983 | | await HandleErrorAsync(e, request, requestInfo, _channel); |
| | | 984 | | |
| | 16 | 985 | | return false; |
| | | 986 | | } |
| | | 987 | | finally |
| | | 988 | | { |
| | 2557 | 989 | | if (releasePump) |
| | | 990 | | { |
| | 18 | 991 | | ReleasePump(); |
| | | 992 | | } |
| | | 993 | | } |
| | | 994 | | |
| | 2539 | 995 | | return true; |
| | 2557 | 996 | | } |
| | | 997 | | |
| | | 998 | | private void ReleasePump() |
| | | 999 | | { |
| | 7194 | 1000 | | if (_isConcurrent) |
| | | 1001 | | { |
| | 7054 | 1002 | | _asyncManualResetEvent.Set(); |
| | | 1003 | | } |
| | 7194 | 1004 | | } |
| | | 1005 | | |
| | | 1006 | | private async Task TryAcquirePumpAsync() |
| | | 1007 | | { |
| | 2685 | 1008 | | if (_isConcurrent) |
| | | 1009 | | { |
| | 2587 | 1010 | | await _asyncManualResetEvent.WaitAsync(); |
| | 2587 | 1011 | | _asyncManualResetEvent.Reset(); |
| | | 1012 | | } |
| | 2685 | 1013 | | } |
| | | 1014 | | |
| | 89 | 1015 | | TimeSpan IDefaultCommunicationTimeouts.CloseTimeout => _serviceDispatcher.Binding.CloseTimeout; |
| | 0 | 1016 | | TimeSpan IDefaultCommunicationTimeouts.OpenTimeout => _serviceDispatcher.Binding.OpenTimeout; |
| | 0 | 1017 | | TimeSpan IDefaultCommunicationTimeouts.ReceiveTimeout => _serviceDispatcher.Binding.ReceiveTimeout; |
| | 89 | 1018 | | TimeSpan IDefaultCommunicationTimeouts.SendTimeout => _serviceDispatcher.Binding.SendTimeout; |
| | | 1019 | | |
| | | 1020 | | // TODO: Revert back to struct or pool objects. |
| | | 1021 | | internal class RequestInfo |
| | | 1022 | | { |
| | | 1023 | | public EndpointDispatcher Endpoint; |
| | | 1024 | | public InstanceContext ExistingInstanceContext; |
| | | 1025 | | public ServiceChannel Channel; |
| | | 1026 | | public bool EndpointLookupDone; |
| | | 1027 | | public DispatchRuntime DispatchRuntime; |
| | | 1028 | | public RequestContext RequestContext; |
| | | 1029 | | public ChannelHandler ChannelHandler; |
| | | 1030 | | public bool ChannelHandlerOwnsCallThrottle; // if true, we are responsible for call throttle |
| | | 1031 | | public bool ChannelHandlerOwnsInstanceContextThrottle; // if true, we are responsible for instance/dynamic t |
| | | 1032 | | |
| | 2558 | 1033 | | public RequestInfo(ChannelHandler channelHandler) |
| | | 1034 | | { |
| | 2558 | 1035 | | Endpoint = null; |
| | 2558 | 1036 | | ExistingInstanceContext = null; |
| | 2558 | 1037 | | Channel = null; |
| | 2558 | 1038 | | EndpointLookupDone = false; |
| | 2558 | 1039 | | DispatchRuntime = null; |
| | 2558 | 1040 | | RequestContext = null; |
| | 2558 | 1041 | | ChannelHandler = channelHandler; |
| | 2558 | 1042 | | ChannelHandlerOwnsCallThrottle = false; |
| | 2558 | 1043 | | ChannelHandlerOwnsInstanceContextThrottle = false; |
| | 2558 | 1044 | | } |
| | | 1045 | | |
| | | 1046 | | public void Cleanup() |
| | | 1047 | | { |
| | 0 | 1048 | | if (ChannelHandlerOwnsInstanceContextThrottle) |
| | | 1049 | | { |
| | 0 | 1050 | | ChannelHandler._throttle?.DeactivateInstanceContext(); |
| | 0 | 1051 | | ChannelHandlerOwnsInstanceContextThrottle = false; |
| | | 1052 | | } |
| | | 1053 | | |
| | 0 | 1054 | | Endpoint = null; |
| | 0 | 1055 | | ExistingInstanceContext = null; |
| | 0 | 1056 | | Channel = null; |
| | 0 | 1057 | | EndpointLookupDone = false; |
| | 0 | 1058 | | RequestContext = null; |
| | 0 | 1059 | | if (ChannelHandlerOwnsCallThrottle) |
| | | 1060 | | { |
| | 0 | 1061 | | ChannelHandler._throttle?.DeactivateCall(); |
| | 0 | 1062 | | ChannelHandlerOwnsCallThrottle = false; |
| | | 1063 | | } |
| | 0 | 1064 | | } |
| | | 1065 | | } |
| | | 1066 | | } |
| | | 1067 | | } |