| | | 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.Diagnostics; |
| | | 7 | | using System.Globalization; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | using CoreWCF.Configuration; |
| | | 11 | | using CoreWCF.Description; |
| | | 12 | | using CoreWCF.Dispatcher; |
| | | 13 | | using CoreWCF.Runtime; |
| | | 14 | | |
| | | 15 | | namespace CoreWCF.Channels |
| | | 16 | | { |
| | | 17 | | // This class is sealed because the constructor could call Abort, which is virtual |
| | | 18 | | internal sealed class ServiceChannel : CommunicationObject, IChannel, IClientChannel, IDuplexContextChannel, IOutput |
| | | 19 | | { |
| | | 20 | | private int _activityCount = 0; |
| | | 21 | | private readonly bool _allowOutputBatching = false; |
| | 516 | 22 | | private bool _autoClose = true; |
| | | 23 | | |
| | | 24 | | //CallOnceManager autoDisplayUIManager; |
| | | 25 | | private CallOnceManager _autoOpenManager; |
| | 516 | 26 | | private readonly bool _closeBinder = true; |
| | | 27 | | private bool _doneReceiving; |
| | | 28 | | private EndpointDispatcher _endpointDispatcher; |
| | | 29 | | private bool _explicitlyOpened; |
| | | 30 | | private ExtensionCollection<IContextChannel> _extensions; |
| | | 31 | | private readonly SessionIdleManager _idleManager; |
| | | 32 | | private EndpointAddress _localAddress; |
| | | 33 | | private readonly bool _openBinder = false; |
| | | 34 | | private TimeSpan _operationTimeout; |
| | | 35 | | private object _proxy; |
| | | 36 | | private ServiceThrottle _serviceThrottle; |
| | | 37 | | private string _terminatingOperationName; |
| | | 38 | | private bool _hasCleanedUpChannelCollections; |
| | | 39 | | |
| | | 40 | | //EventTraceActivity eventActivity; |
| | | 41 | | private readonly IDefaultCommunicationTimeouts _timeouts; |
| | | 42 | | private EventHandler<UnknownMessageReceivedEventArgs> _unknownMessageReceived; |
| | | 43 | | |
| | 516 | 44 | | private ServiceChannel(IChannelBinder binder, Binding binding) |
| | | 45 | | { |
| | 516 | 46 | | MessageVersion = binding.MessageVersion; |
| | 516 | 47 | | Binder = binder ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(binder)); |
| | 516 | 48 | | IsReplyChannel = Binder.Channel is IReplyChannel; |
| | | 49 | | |
| | 516 | 50 | | IChannel innerChannel = binder.Channel; |
| | 516 | 51 | | HasSession = (innerChannel is ISessionChannel<IDuplexSession>) || |
| | 516 | 52 | | (innerChannel is ISessionChannel<IInputSession>) || |
| | 516 | 53 | | (innerChannel is ISessionChannel<IOutputSession>); |
| | | 54 | | |
| | 516 | 55 | | IncrementActivity(); |
| | 516 | 56 | | _openBinder = (binder.Channel.State == CommunicationState.Created); |
| | | 57 | | |
| | 516 | 58 | | _operationTimeout = binding.SendTimeout; |
| | 516 | 59 | | _timeouts = binding; |
| | 516 | 60 | | } |
| | | 61 | | |
| | | 62 | | // Only used by ServiceChannelFactory |
| | | 63 | | //internal ServiceChannel(ServiceChannelFactory factory, IChannelBinder binder) |
| | | 64 | | // : this(binder, factory.MessageVersion, factory) |
| | | 65 | | //{ |
| | | 66 | | // this.factory = factory; |
| | | 67 | | // this.clientRuntime = factory.ClientRuntime; |
| | | 68 | | |
| | | 69 | | // this.SetupInnerChannelFaultHandler(); |
| | | 70 | | |
| | | 71 | | // DispatchRuntime dispatch = factory.ClientRuntime.DispatchRuntime; |
| | | 72 | | // if (dispatch != null) |
| | | 73 | | // { |
| | | 74 | | // this.autoClose = dispatch.AutomaticInputSessionShutdown; |
| | | 75 | | // } |
| | | 76 | | |
| | | 77 | | // factory.ChannelCreated(this); |
| | | 78 | | //} |
| | | 79 | | |
| | | 80 | | internal ServiceChannel(IChannelBinder binder, |
| | | 81 | | EndpointDispatcher endpointDispatcher, |
| | | 82 | | ServiceDispatcher serviceDispatcher, |
| | | 83 | | SessionIdleManager idleManager) |
| | 516 | 84 | | : this(binder, serviceDispatcher.Binding) |
| | | 85 | | { |
| | 516 | 86 | | ChannelDispatcher = serviceDispatcher.ChannelDispatcher; |
| | 516 | 87 | | _endpointDispatcher = endpointDispatcher ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull |
| | 516 | 88 | | ClientRuntime = endpointDispatcher.DispatchRuntime.CallbackClientRuntime; |
| | | 89 | | |
| | 516 | 90 | | SetupInnerChannelFaultHandler(); |
| | | 91 | | |
| | 516 | 92 | | _autoClose = endpointDispatcher.DispatchRuntime.AutomaticInputSessionShutdown; |
| | 516 | 93 | | IsPending = true; |
| | | 94 | | |
| | 516 | 95 | | _idleManager = idleManager; |
| | | 96 | | |
| | 516 | 97 | | if (!binder.HasSession) |
| | | 98 | | { |
| | 433 | 99 | | _closeBinder = false; |
| | | 100 | | } |
| | | 101 | | |
| | 516 | 102 | | if (_idleManager != null) |
| | | 103 | | { |
| | 83 | 104 | | _idleManager.RegisterChannel(this, out bool didIdleAbort); |
| | 83 | 105 | | if (didIdleAbort) |
| | | 106 | | { |
| | 0 | 107 | | Abort(); |
| | | 108 | | } |
| | | 109 | | } |
| | 516 | 110 | | } |
| | | 111 | | |
| | | 112 | | private CallOnceManager AutoOpenManager |
| | | 113 | | { |
| | | 114 | | get |
| | | 115 | | { |
| | 0 | 116 | | if (!_explicitlyOpened && (_autoOpenManager == null)) |
| | | 117 | | { |
| | 0 | 118 | | EnsureAutoOpenManagers(); |
| | | 119 | | } |
| | 0 | 120 | | return _autoOpenManager; |
| | | 121 | | } |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | //CallOnceManager AutoDisplayUIManager |
| | | 125 | | //{ |
| | | 126 | | // get |
| | | 127 | | // { |
| | | 128 | | // if (!this.explicitlyOpened && (this.autoDisplayUIManager == null)) |
| | | 129 | | // { |
| | | 130 | | // this.EnsureAutoOpenManagers(); |
| | | 131 | | // } |
| | | 132 | | // return this.autoDisplayUIManager; |
| | | 133 | | // } |
| | | 134 | | //} |
| | | 135 | | |
| | | 136 | | |
| | | 137 | | //internal EventTraceActivity EventActivity |
| | | 138 | | //{ |
| | | 139 | | // get |
| | | 140 | | // { |
| | | 141 | | // if (this.eventActivity == null) |
| | | 142 | | // { |
| | | 143 | | // //Take the id on the thread so that we know the initiating operation. |
| | | 144 | | // this.eventActivity = EventTraceActivity.GetFromThreadOrCreate(); |
| | | 145 | | // } |
| | | 146 | | // return this.eventActivity; |
| | | 147 | | // } |
| | | 148 | | //} |
| | | 149 | | |
| | 0 | 150 | | internal bool CloseFactory { get; set; } |
| | | 151 | | |
| | | 152 | | protected override TimeSpan DefaultCloseTimeout |
| | | 153 | | { |
| | 0 | 154 | | get { return CloseTimeout; } |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | protected override TimeSpan DefaultOpenTimeout |
| | | 158 | | { |
| | 516 | 159 | | get { return OpenTimeout; } |
| | | 160 | | } |
| | | 161 | | |
| | | 162 | | internal DispatchRuntime DispatchRuntime |
| | | 163 | | { |
| | | 164 | | get |
| | | 165 | | { |
| | 2644 | 166 | | if (_endpointDispatcher != null) |
| | | 167 | | { |
| | 2644 | 168 | | return _endpointDispatcher.DispatchRuntime; |
| | | 169 | | } |
| | 0 | 170 | | if (ClientRuntime != null) |
| | | 171 | | { |
| | 0 | 172 | | return ClientRuntime.DispatchRuntime; |
| | | 173 | | } |
| | 0 | 174 | | return null; |
| | | 175 | | } |
| | | 176 | | } |
| | | 177 | | |
| | 2539 | 178 | | internal MessageVersion MessageVersion { get; } |
| | | 179 | | |
| | 2578 | 180 | | internal IChannelBinder Binder { get; } |
| | | 181 | | |
| | | 182 | | internal TimeSpan CloseTimeout |
| | | 183 | | { |
| | | 184 | | get |
| | | 185 | | { |
| | | 186 | | //if (this.IsClient) |
| | | 187 | | //{ |
| | | 188 | | // return factory.InternalCloseTimeout; |
| | | 189 | | //} |
| | | 190 | | //else |
| | | 191 | | //{ |
| | 2473 | 192 | | return _timeouts.CloseTimeout; |
| | | 193 | | //} |
| | | 194 | | } |
| | | 195 | | } |
| | | 196 | | |
| | 1029 | 197 | | internal ChannelDispatcher ChannelDispatcher { get; } |
| | | 198 | | |
| | | 199 | | internal EndpointDispatcher EndpointDispatcher |
| | | 200 | | { |
| | 106 | 201 | | get { return _endpointDispatcher; } |
| | | 202 | | set |
| | | 203 | | { |
| | 0 | 204 | | lock (ThisLock) |
| | | 205 | | { |
| | 0 | 206 | | _endpointDispatcher = value; |
| | 0 | 207 | | ClientRuntime = value.DispatchRuntime.CallbackClientRuntime; |
| | 0 | 208 | | } |
| | 0 | 209 | | } |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | //internal ServiceChannelFactory Factory |
| | | 213 | | //{ |
| | | 214 | | // get { return this.factory; } |
| | | 215 | | //} |
| | | 216 | | |
| | | 217 | | internal IChannel InnerChannel |
| | | 218 | | { |
| | 1540 | 219 | | get { return Binder.Channel; } |
| | | 220 | | } |
| | | 221 | | |
| | 3565 | 222 | | internal bool IsPending { get; set; } |
| | | 223 | | |
| | 13214 | 224 | | internal bool HasSession { get; } |
| | | 225 | | |
| | 728 | 226 | | internal bool IsReplyChannel { get; } |
| | | 227 | | |
| | | 228 | | public Uri ListenUri |
| | | 229 | | { |
| | | 230 | | get |
| | | 231 | | { |
| | 0 | 232 | | return Binder.ListenUri; |
| | | 233 | | } |
| | | 234 | | } |
| | | 235 | | |
| | | 236 | | public EndpointAddress LocalAddress |
| | | 237 | | { |
| | | 238 | | get |
| | | 239 | | { |
| | 3 | 240 | | if (_localAddress == null) |
| | | 241 | | { |
| | 3 | 242 | | if (_endpointDispatcher != null) |
| | | 243 | | { |
| | 3 | 244 | | _localAddress = _endpointDispatcher.EndpointAddress; |
| | | 245 | | } |
| | | 246 | | else |
| | | 247 | | { |
| | 0 | 248 | | _localAddress = Binder.LocalAddress; |
| | | 249 | | } |
| | | 250 | | } |
| | 3 | 251 | | return _localAddress; |
| | | 252 | | } |
| | | 253 | | } |
| | | 254 | | |
| | | 255 | | internal TimeSpan OpenTimeout |
| | | 256 | | { |
| | | 257 | | get |
| | | 258 | | { |
| | | 259 | | //if (this.IsClient) |
| | | 260 | | //{ |
| | | 261 | | // return factory.InternalOpenTimeout; |
| | | 262 | | //} |
| | | 263 | | //else |
| | | 264 | | //{ |
| | 516 | 265 | | return ChannelDispatcher.InternalOpenTimeout; |
| | | 266 | | //} |
| | | 267 | | } |
| | | 268 | | } |
| | | 269 | | |
| | | 270 | | public TimeSpan OperationTimeout |
| | | 271 | | { |
| | 728 | 272 | | get { return _operationTimeout; } |
| | | 273 | | set |
| | | 274 | | { |
| | 0 | 275 | | if (value < TimeSpan.Zero) |
| | | 276 | | { |
| | 0 | 277 | | string message = SRCommon.SFxTimeoutOutOfRange0; |
| | 0 | 278 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 279 | | } |
| | 0 | 280 | | if (TimeoutHelper.IsTooLarge(value)) |
| | | 281 | | { |
| | 0 | 282 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 283 | | } |
| | | 284 | | |
| | | 285 | | |
| | 0 | 286 | | _operationTimeout = value; |
| | 0 | 287 | | } |
| | | 288 | | } |
| | | 289 | | |
| | | 290 | | internal object Proxy |
| | | 291 | | { |
| | | 292 | | get |
| | | 293 | | { |
| | 8113 | 294 | | object proxy = _proxy; |
| | 8113 | 295 | | if (proxy != null) |
| | | 296 | | { |
| | 8 | 297 | | return proxy; |
| | | 298 | | } |
| | | 299 | | else |
| | | 300 | | { |
| | 8105 | 301 | | return this; |
| | | 302 | | } |
| | | 303 | | } |
| | | 304 | | set |
| | | 305 | | { |
| | 1 | 306 | | _proxy = value; |
| | 1 | 307 | | EventSender = value; // need to use "proxy" as open/close event source |
| | 1 | 308 | | } |
| | | 309 | | } |
| | | 310 | | |
| | 1114 | 311 | | internal ClientRuntime ClientRuntime { get; private set; } |
| | | 312 | | |
| | | 313 | | public EndpointAddress RemoteAddress |
| | | 314 | | { |
| | | 315 | | get |
| | | 316 | | { |
| | 683 | 317 | | if (InnerChannel is IOutputChannel outputChannel) |
| | | 318 | | { |
| | 83 | 319 | | return outputChannel.RemoteAddress; |
| | | 320 | | } |
| | | 321 | | |
| | 600 | 322 | | if (InnerChannel is IRequestChannel requestChannel) |
| | | 323 | | { |
| | 0 | 324 | | return requestChannel.RemoteAddress; |
| | | 325 | | } |
| | | 326 | | |
| | 600 | 327 | | return null; |
| | | 328 | | } |
| | | 329 | | } |
| | | 330 | | |
| | | 331 | | private ProxyOperationRuntime UnhandledProxyOperation |
| | | 332 | | { |
| | 0 | 333 | | get { return ClientRuntime.GetRuntime().UnhandledProxyOperation; } |
| | | 334 | | } |
| | | 335 | | |
| | | 336 | | public Uri Via |
| | | 337 | | { |
| | | 338 | | get |
| | | 339 | | { |
| | 0 | 340 | | if (InnerChannel is IOutputChannel outputChannel) |
| | | 341 | | { |
| | 0 | 342 | | return outputChannel.Via; |
| | | 343 | | } |
| | | 344 | | |
| | 0 | 345 | | if (InnerChannel is IRequestChannel requestChannel) |
| | | 346 | | { |
| | 0 | 347 | | return requestChannel.Via; |
| | | 348 | | } |
| | | 349 | | |
| | 0 | 350 | | return null; |
| | | 351 | | } |
| | | 352 | | } |
| | | 353 | | |
| | 2541 | 354 | | internal InstanceContext InstanceContext { get; set; } |
| | | 355 | | |
| | 0 | 356 | | internal ServiceThrottle InstanceContextServiceThrottle { get; set; } |
| | | 357 | | |
| | | 358 | | internal ServiceThrottle ServiceThrottle |
| | | 359 | | { |
| | 0 | 360 | | get { return _serviceThrottle; } |
| | | 361 | | set |
| | | 362 | | { |
| | 0 | 363 | | ThrowIfDisposed(); |
| | 0 | 364 | | _serviceThrottle = value; |
| | 0 | 365 | | } |
| | | 366 | | } |
| | | 367 | | |
| | | 368 | | private void SetupInnerChannelFaultHandler() |
| | | 369 | | { |
| | | 370 | | // need to call this method after this.binder and this.clientRuntime are set to prevent a potential |
| | | 371 | | // NullReferenceException in this method or in the OnInnerChannelFaulted method; |
| | | 372 | | // because this method accesses this.binder and OnInnerChannelFaulted accesses this.clientRuntime. |
| | 516 | 373 | | Binder.Channel.Faulted += OnInnerChannelFaulted; |
| | 516 | 374 | | } |
| | | 375 | | |
| | | 376 | | //void BindDuplexCallbacks() |
| | | 377 | | //{ |
| | | 378 | | // IDuplexChannel duplexChannel = this.InnerChannel as IDuplexChannel; |
| | | 379 | | // if ((duplexChannel != null) && (this.factory != null) && (this.instanceContext != null)) |
| | | 380 | | // { |
| | | 381 | | // if (this.binder is DuplexChannelBinder) |
| | | 382 | | // ((DuplexChannelBinder)this.binder).EnsurePumping(); |
| | | 383 | | // } |
| | | 384 | | //} |
| | | 385 | | |
| | | 386 | | internal bool CanCastTo(Type t) |
| | | 387 | | { |
| | 0 | 388 | | if (t.IsAssignableFrom(typeof(IClientChannel))) |
| | | 389 | | { |
| | 0 | 390 | | return true; |
| | | 391 | | } |
| | | 392 | | |
| | 0 | 393 | | if (t.IsAssignableFrom(typeof(IDuplexContextChannel))) |
| | | 394 | | { |
| | 0 | 395 | | return InnerChannel is IDuplexChannel; |
| | | 396 | | } |
| | | 397 | | |
| | 0 | 398 | | if (t.IsAssignableFrom(typeof(IServiceChannel))) |
| | | 399 | | { |
| | 0 | 400 | | return true; |
| | | 401 | | } |
| | | 402 | | |
| | 0 | 403 | | return false; |
| | | 404 | | } |
| | | 405 | | |
| | | 406 | | internal void CompletedIOOperation() |
| | | 407 | | { |
| | 3056 | 408 | | if (_idleManager != null) |
| | | 409 | | { |
| | 190 | 410 | | _idleManager.CompletedActivity(); |
| | | 411 | | } |
| | 3056 | 412 | | } |
| | | 413 | | |
| | | 414 | | private void EnsureAutoOpenManagers() |
| | | 415 | | { |
| | 0 | 416 | | lock (ThisLock) |
| | | 417 | | { |
| | 0 | 418 | | if (!_explicitlyOpened) |
| | | 419 | | { |
| | 0 | 420 | | if (_autoOpenManager == null) |
| | | 421 | | { |
| | 0 | 422 | | _autoOpenManager = new CallOnceManager(this, CallOpenOnce.Instance); |
| | | 423 | | } |
| | | 424 | | } |
| | 0 | 425 | | } |
| | 0 | 426 | | } |
| | | 427 | | |
| | | 428 | | private async Task EnsureOpenedAsync(CancellationToken token) |
| | | 429 | | { |
| | | 430 | | ///// TASKS ****** |
| | 0 | 431 | | CallOnceManager manager = AutoOpenManager; |
| | 0 | 432 | | if (manager != null) |
| | | 433 | | { |
| | 0 | 434 | | await manager.CallOnceAsync(token); |
| | | 435 | | } |
| | | 436 | | |
| | 0 | 437 | | ThrowIfOpening(); |
| | 0 | 438 | | ThrowIfDisposedOrNotOpen(); |
| | 0 | 439 | | } |
| | | 440 | | |
| | | 441 | | public T GetProperty<T>() where T : class |
| | | 442 | | { |
| | 164 | 443 | | IChannel innerChannel = InnerChannel; |
| | 164 | 444 | | if (innerChannel != null) |
| | | 445 | | { |
| | 164 | 446 | | return innerChannel.GetProperty<T>(); |
| | | 447 | | } |
| | | 448 | | |
| | 0 | 449 | | return null; |
| | | 450 | | } |
| | | 451 | | |
| | | 452 | | private void PrepareCall(ProxyOperationRuntime operation, bool oneway, ref ProxyRpc rpc) |
| | | 453 | | { |
| | 1 | 454 | | OperationContext context = OperationContext.Current; |
| | | 455 | | // Doing a request reply callback when dispatching in-order deadlocks. |
| | | 456 | | // We never receive the reply until we finish processing the current message. |
| | 1 | 457 | | if (!oneway) |
| | | 458 | | { |
| | 1 | 459 | | DispatchRuntime dispatchBehavior = ClientRuntime.DispatchRuntime; |
| | 1 | 460 | | if ((dispatchBehavior != null) && (dispatchBehavior.ConcurrencyMode == ConcurrencyMode.Single)) |
| | | 461 | | { |
| | 0 | 462 | | if ((context != null) && (!context.IsUserContext) && (context.InternalServiceChannel == this)) |
| | | 463 | | { |
| | 0 | 464 | | if (dispatchBehavior.IsOnServer) |
| | | 465 | | { |
| | 0 | 466 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.F |
| | | 467 | | } |
| | | 468 | | else |
| | | 469 | | { |
| | 0 | 470 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.F |
| | | 471 | | } |
| | | 472 | | } |
| | | 473 | | } |
| | | 474 | | } |
| | | 475 | | |
| | 1 | 476 | | if ((State == CommunicationState.Created) && !operation.IsInitiating) |
| | | 477 | | { |
| | 0 | 478 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.SFx |
| | | 479 | | } |
| | | 480 | | |
| | 1 | 481 | | if (_terminatingOperationName != null) |
| | | 482 | | { |
| | 0 | 483 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.SFx |
| | | 484 | | } |
| | | 485 | | |
| | 1 | 486 | | operation.BeforeRequest(ref rpc); |
| | 1 | 487 | | AddMessageProperties(rpc.Request, context); |
| | 1 | 488 | | if (!oneway && !ClientRuntime.ManualAddressing && rpc.Request.Version.Addressing != AddressingVersion.None) |
| | | 489 | | { |
| | 1 | 490 | | RequestReplyCorrelator.PrepareRequest(rpc.Request); |
| | | 491 | | |
| | 1 | 492 | | MessageHeaders headers = rpc.Request.Headers; |
| | 1 | 493 | | EndpointAddress localAddress = LocalAddress; |
| | 1 | 494 | | EndpointAddress replyTo = headers.ReplyTo; |
| | | 495 | | |
| | 1 | 496 | | if (replyTo == null) |
| | | 497 | | { |
| | 1 | 498 | | headers.ReplyTo = localAddress ?? EndpointAddress.AnonymousAddress; |
| | | 499 | | } |
| | | 500 | | } |
| | | 501 | | |
| | | 502 | | //if (TraceUtility.MessageFlowTracingOnly) |
| | | 503 | | //{ |
| | | 504 | | // //always set a new ID if none provided |
| | | 505 | | // if (Trace.CorrelationManager.ActivityId == Guid.Empty) |
| | | 506 | | // { |
| | | 507 | | // rpc.ActivityId = Guid.NewGuid(); |
| | | 508 | | // FxTrace.Trace.SetAndTraceTransfer(rpc.ActivityId, true); |
| | | 509 | | // } |
| | | 510 | | //} |
| | | 511 | | |
| | | 512 | | //if (rpc.Activity != null) |
| | | 513 | | //{ |
| | | 514 | | // TraceUtility.SetActivity(rpc.Request, rpc.Activity); |
| | | 515 | | // if (TraceUtility.ShouldPropagateActivity) |
| | | 516 | | // { |
| | | 517 | | // TraceUtility.AddActivityHeader(rpc.Request); |
| | | 518 | | // } |
| | | 519 | | //} |
| | | 520 | | //else if (TraceUtility.PropagateUserActivity || TraceUtility.ShouldPropagateActivity) |
| | | 521 | | //{ |
| | | 522 | | // TraceUtility.AddAmbientActivityToMessage(rpc.Request); |
| | | 523 | | //} |
| | 1 | 524 | | operation.Parent.BeforeSendRequest(ref rpc); |
| | | 525 | | |
| | | 526 | | //Attach and transfer Activity |
| | | 527 | | //if (FxTrace.Trace.IsEnd2EndActivityTracingEnabled) |
| | | 528 | | //{ |
| | | 529 | | // TraceClientOperationPrepared(ref rpc); |
| | | 530 | | //} |
| | | 531 | | |
| | | 532 | | //TraceUtility.MessageFlowAtMessageSent(rpc.Request, rpc.EventTraceActivity); |
| | | 533 | | |
| | | 534 | | //if (MessageLogger.LogMessagesAtServiceLevel) |
| | | 535 | | //{ |
| | | 536 | | // MessageLogger.LogMessage(ref rpc.Request, (oneway ? MessageLoggingSource.ServiceLevelSendDatagram : Me |
| | | 537 | | //} |
| | 1 | 538 | | } |
| | | 539 | | |
| | | 540 | | //private void TraceClientOperationPrepared(ref ProxyRpc rpc) |
| | | 541 | | //{ |
| | | 542 | | // //Retrieve the old id on the RPC and attach the id on the message since we have a message id now. |
| | | 543 | | // Guid previousId = rpc.EventTraceActivity != null ? rpc.EventTraceActivity.ActivityId : Guid.Empty; |
| | | 544 | | // EventTraceActivity requestActivity = EventTraceActivityHelper.TryExtractActivity(rpc.Request); |
| | | 545 | | // if (requestActivity == null) |
| | | 546 | | // { |
| | | 547 | | // requestActivity = EventTraceActivity.GetFromThreadOrCreate(); |
| | | 548 | | // EventTraceActivityHelper.TryAttachActivity(rpc.Request, requestActivity); |
| | | 549 | | // } |
| | | 550 | | // rpc.EventTraceActivity = requestActivity; |
| | | 551 | | |
| | | 552 | | // if (TD.ClientOperationPreparedIsEnabled()) |
| | | 553 | | // { |
| | | 554 | | // string remoteAddress = string.Empty; |
| | | 555 | | // if (this.RemoteAddress != null && this.RemoteAddress.Uri != null) |
| | | 556 | | // { |
| | | 557 | | // remoteAddress = this.RemoteAddress.Uri.AbsoluteUri; |
| | | 558 | | // } |
| | | 559 | | // TD.ClientOperationPrepared(rpc.EventTraceActivity, |
| | | 560 | | // rpc.Action, |
| | | 561 | | // this.clientRuntime.ContractName, |
| | | 562 | | // remoteAddress, |
| | | 563 | | // previousId); |
| | | 564 | | // } |
| | | 565 | | |
| | | 566 | | //} |
| | | 567 | | |
| | | 568 | | internal IAsyncResult BeginCall(string action, bool oneway, ProxyOperationRuntime operation, object[] ins, Async |
| | | 569 | | { |
| | 0 | 570 | | return BeginCall(action, oneway, operation, ins, _operationTimeout, callback, asyncState); |
| | | 571 | | } |
| | | 572 | | |
| | | 573 | | internal IAsyncResult BeginCall(string action, bool oneway, ProxyOperationRuntime operation, object[] ins, TimeS |
| | | 574 | | { |
| | 0 | 575 | | var helper = new TimeoutHelper(_operationTimeout); |
| | 0 | 576 | | return BeginCallAsync(action, oneway, operation, ins, helper.GetCancellationToken()).ToApm(callback, asyncSt |
| | | 577 | | } |
| | | 578 | | |
| | | 579 | | internal async Task<ProxyRpc> BeginCallAsync(string action, bool oneway, ProxyOperationRuntime operation, object |
| | | 580 | | { |
| | 0 | 581 | | ThrowIfIdleAborted(operation); |
| | 0 | 582 | | ThrowIfIsConnectionOpened(operation); |
| | | 583 | | |
| | 0 | 584 | | ProxyRpc rpc = new ProxyRpc(this, operation, action, ins, token); |
| | | 585 | | |
| | 0 | 586 | | PrepareCall(operation, oneway, ref rpc); |
| | | 587 | | |
| | 0 | 588 | | if (!_explicitlyOpened) |
| | | 589 | | { |
| | 0 | 590 | | await EnsureOpenedAsync(token); |
| | | 591 | | } |
| | | 592 | | else |
| | | 593 | | { |
| | 0 | 594 | | ThrowIfOpening(); |
| | 0 | 595 | | ThrowIfDisposedOrNotOpen(); |
| | | 596 | | } |
| | | 597 | | |
| | | 598 | | try |
| | | 599 | | { |
| | 0 | 600 | | ConcurrencyBehavior.UnlockInstanceBeforeCallout(OperationContext.Current); |
| | | 601 | | |
| | 0 | 602 | | if (oneway) |
| | | 603 | | { |
| | 0 | 604 | | await Binder.SendAsync(rpc.Request, rpc.CancellationToken); |
| | | 605 | | } |
| | | 606 | | else |
| | | 607 | | { |
| | 0 | 608 | | rpc.Reply = await Binder.RequestAsync(rpc.Request, rpc.CancellationToken); |
| | | 609 | | |
| | 0 | 610 | | if (rpc.Reply == null) |
| | | 611 | | { |
| | 0 | 612 | | ThrowIfFaulted(); |
| | 0 | 613 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(SR.SFxServe |
| | | 614 | | } |
| | | 615 | | } |
| | | 616 | | } |
| | | 617 | | finally |
| | | 618 | | { |
| | 0 | 619 | | CompletedIOOperation(); |
| | 0 | 620 | | CallOnceManager.SignalNextIfNonNull(_autoOpenManager); |
| | 0 | 621 | | await ConcurrencyBehavior.LockInstanceAfterCalloutAsync(OperationContext.Current); |
| | | 622 | | } |
| | | 623 | | |
| | 0 | 624 | | return rpc; |
| | 0 | 625 | | } |
| | | 626 | | |
| | | 627 | | |
| | | 628 | | internal Task<object> CallAsync(string action, bool oneway, ProxyOperationRuntime operation, object[] ins, objec |
| | | 629 | | { |
| | 1 | 630 | | var helper = new TimeoutHelper(_operationTimeout); |
| | 1 | 631 | | return CallAsync(action, oneway, operation, ins, outs, helper.GetCancellationToken()); |
| | | 632 | | } |
| | | 633 | | |
| | | 634 | | internal async Task<object> CallAsync(string action, bool oneway, ProxyOperationRuntime operation, object[] ins, |
| | | 635 | | { |
| | 1 | 636 | | ThrowIfIdleAborted(operation); |
| | 1 | 637 | | ThrowIfIsConnectionOpened(operation); |
| | | 638 | | |
| | 1 | 639 | | ProxyRpc rpc = new ProxyRpc(this, operation, action, ins, token); |
| | | 640 | | |
| | | 641 | | //TraceServiceChannelCallStart(rpc.EventTraceActivity, true); |
| | | 642 | | |
| | | 643 | | //using (rpc.Activity = DiagnosticUtility.ShouldUseActivity ? ServiceModelActivity.CreateBoundedActivity() : |
| | | 644 | | //{ |
| | | 645 | | // if (DiagnosticUtility.ShouldUseActivity) |
| | | 646 | | // { |
| | | 647 | | // ServiceModelActivity.Start(rpc.Activity, SR.Format(SR.ActivityProcessAction, action), ActivityType |
| | | 648 | | // } |
| | | 649 | | |
| | 1 | 650 | | PrepareCall(operation, oneway, ref rpc); |
| | | 651 | | |
| | 1 | 652 | | if (!_explicitlyOpened) |
| | | 653 | | { |
| | 0 | 654 | | await EnsureOpenedAsync(token); |
| | | 655 | | } |
| | | 656 | | else |
| | | 657 | | { |
| | 1 | 658 | | ThrowIfOpening(); |
| | 1 | 659 | | ThrowIfDisposedOrNotOpen(); |
| | | 660 | | } |
| | | 661 | | |
| | | 662 | | try |
| | | 663 | | { |
| | 1 | 664 | | ConcurrencyBehavior.UnlockInstanceBeforeCallout(OperationContext.Current); |
| | | 665 | | |
| | 1 | 666 | | if (oneway) |
| | | 667 | | { |
| | 0 | 668 | | await Binder.SendAsync(rpc.Request, rpc.CancellationToken); |
| | | 669 | | } |
| | | 670 | | else |
| | | 671 | | { |
| | 1 | 672 | | rpc.Reply = await Binder.RequestAsync(rpc.Request, rpc.CancellationToken); |
| | | 673 | | |
| | 1 | 674 | | if (rpc.Reply == null) |
| | | 675 | | { |
| | 0 | 676 | | ThrowIfFaulted(); |
| | 0 | 677 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(SR.SFxServe |
| | | 678 | | } |
| | | 679 | | } |
| | | 680 | | } |
| | | 681 | | finally |
| | | 682 | | { |
| | 1 | 683 | | CompletedIOOperation(); |
| | 1 | 684 | | CallOnceManager.SignalNextIfNonNull(_autoOpenManager); |
| | 1 | 685 | | await ConcurrencyBehavior.LockInstanceAfterCalloutAsync(OperationContext.Current); |
| | | 686 | | } |
| | | 687 | | |
| | 1 | 688 | | rpc.OutputParameters = outs; |
| | 1 | 689 | | HandleReply(operation, ref rpc); |
| | | 690 | | //} |
| | 1 | 691 | | return rpc.ReturnValue; |
| | 1 | 692 | | } |
| | | 693 | | |
| | | 694 | | internal object EndCall(string action, object[] outs, IAsyncResult result) |
| | | 695 | | { |
| | 0 | 696 | | ProxyRpc rpc = result.ToApmEnd<ProxyRpc>(); |
| | 0 | 697 | | rpc.OutputParameters = outs; |
| | 0 | 698 | | HandleReply(rpc.Operation, ref rpc); |
| | 0 | 699 | | return rpc.ReturnValue; |
| | | 700 | | } |
| | | 701 | | |
| | | 702 | | internal Task DecrementActivityAsync() |
| | | 703 | | { |
| | 2558 | 704 | | int updatedActivityCount = Interlocked.Decrement(ref _activityCount); |
| | | 705 | | |
| | 2558 | 706 | | if (!((updatedActivityCount >= 0))) |
| | | 707 | | { |
| | 0 | 708 | | throw Fx.AssertAndThrowFatal("ServiceChannel.DecrementActivity: (updatedActivityCount >= 0)"); |
| | | 709 | | } |
| | | 710 | | |
| | 2558 | 711 | | if (updatedActivityCount == 0 && _autoClose && State == CommunicationState.Opened) |
| | | 712 | | { |
| | 78 | 713 | | return AutoCloseAsync(); |
| | | 714 | | } |
| | | 715 | | |
| | 2480 | 716 | | return Task.CompletedTask; |
| | | 717 | | |
| | | 718 | | async Task AutoCloseAsync() |
| | | 719 | | { |
| | | 720 | | try |
| | | 721 | | { |
| | 78 | 722 | | var helper = new TimeoutHelper(CloseTimeout); |
| | 78 | 723 | | await CloseAsync(helper.GetCancellationToken()); |
| | 78 | 724 | | } |
| | | 725 | | catch (CommunicationException e) |
| | | 726 | | { |
| | 0 | 727 | | DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); |
| | 0 | 728 | | } |
| | | 729 | | catch (TimeoutException e) |
| | | 730 | | { |
| | | 731 | | //if (TD.CloseTimeoutIsEnabled()) |
| | | 732 | | //{ |
| | | 733 | | // TD.CloseTimeout(e.Message); |
| | | 734 | | //} |
| | 0 | 735 | | DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); |
| | 0 | 736 | | } |
| | | 737 | | catch (ObjectDisposedException e) |
| | | 738 | | { |
| | 0 | 739 | | DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); |
| | 0 | 740 | | } |
| | | 741 | | catch (InvalidOperationException e) |
| | | 742 | | { |
| | 0 | 743 | | DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); |
| | 0 | 744 | | } |
| | 78 | 745 | | } |
| | | 746 | | } |
| | | 747 | | |
| | | 748 | | internal void FireUnknownMessageReceived(Message message) |
| | | 749 | | { |
| | 0 | 750 | | EventHandler<UnknownMessageReceivedEventArgs> handler = _unknownMessageReceived; |
| | 0 | 751 | | if (handler != null) |
| | | 752 | | { |
| | 0 | 753 | | handler(_proxy, new UnknownMessageReceivedEventArgs(message)); |
| | | 754 | | } |
| | 0 | 755 | | } |
| | | 756 | | |
| | | 757 | | private TimeoutException GetOpenTimeoutException(TimeSpan timeout) |
| | | 758 | | { |
| | 0 | 759 | | EndpointAddress address = RemoteAddress ?? LocalAddress; |
| | 0 | 760 | | if (address != null) |
| | | 761 | | { |
| | 0 | 762 | | return new TimeoutException(SR.Format(SR.TimeoutServiceChannelConcurrentOpen2, address, timeout)); |
| | | 763 | | } |
| | | 764 | | else |
| | | 765 | | { |
| | 0 | 766 | | return new TimeoutException(SR.Format(SR.TimeoutServiceChannelConcurrentOpen1, timeout)); |
| | | 767 | | } |
| | | 768 | | } |
| | | 769 | | |
| | | 770 | | internal Task HandleReceiveCompleteAsync(RequestContext context) |
| | | 771 | | { |
| | 102 | 772 | | if (context == null && HasSession) |
| | | 773 | | { |
| | | 774 | | bool first; |
| | 78 | 775 | | lock (ThisLock) |
| | | 776 | | { |
| | 78 | 777 | | first = !_doneReceiving; |
| | 78 | 778 | | _doneReceiving = true; |
| | 78 | 779 | | } |
| | | 780 | | |
| | 78 | 781 | | if (first) |
| | | 782 | | { |
| | 78 | 783 | | DispatchRuntime dispatchBehavior = ClientRuntime.DispatchRuntime; |
| | 78 | 784 | | if (dispatchBehavior != null) |
| | | 785 | | { |
| | 78 | 786 | | dispatchBehavior.GetRuntime().InputSessionDoneReceiving(this); |
| | | 787 | | } |
| | | 788 | | |
| | 78 | 789 | | return DecrementActivityAsync(); |
| | | 790 | | } |
| | | 791 | | } |
| | | 792 | | |
| | 24 | 793 | | return Task.CompletedTask; |
| | | 794 | | } |
| | | 795 | | |
| | | 796 | | private void HandleReply(ProxyOperationRuntime operation, ref ProxyRpc rpc) |
| | | 797 | | { |
| | | 798 | | try |
| | | 799 | | { |
| | | 800 | | //set the ID after response |
| | | 801 | | //if (TraceUtility.MessageFlowTracingOnly && rpc.ActivityId != Guid.Empty) |
| | | 802 | | //{ |
| | | 803 | | // System.Runtime.Diagnostics.DiagnosticTraceBase.ActivityId = rpc.ActivityId; |
| | | 804 | | //} |
| | | 805 | | |
| | 1 | 806 | | if (rpc.Reply != null) |
| | | 807 | | { |
| | | 808 | | //TraceUtility.MessageFlowAtMessageReceived(rpc.Reply, null, rpc.EventTraceActivity, false); |
| | | 809 | | |
| | | 810 | | //if (MessageLogger.LogMessagesAtServiceLevel) |
| | | 811 | | //{ |
| | | 812 | | // MessageLogger.LogMessage(ref rpc.Reply, MessageLoggingSource.ServiceLevelReceiveReply | Messag |
| | | 813 | | //} |
| | 1 | 814 | | operation.Parent.AfterReceiveReply(ref rpc); |
| | | 815 | | |
| | 1 | 816 | | if ((operation.ReplyAction != MessageHeaders.WildcardAction) && !rpc.Reply.IsFault && rpc.Reply.Head |
| | | 817 | | { |
| | 1 | 818 | | if (string.CompareOrdinal(operation.ReplyAction, rpc.Reply.Headers.Action) != 0) |
| | | 819 | | { |
| | 0 | 820 | | Exception error = new ProtocolException(SR.Format(SR.SFxReplyActionMismatch3, operation.Name |
| | 0 | 821 | | rpc.Reply.Headers.Action, |
| | 0 | 822 | | operation.ReplyAction)); |
| | 0 | 823 | | TerminateIfNecessary(ref rpc); |
| | 0 | 824 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(error); |
| | | 825 | | } |
| | | 826 | | } |
| | 1 | 827 | | if (operation.DeserializeReply && ClientRuntime.IsFault(ref rpc.Reply)) |
| | | 828 | | { |
| | 0 | 829 | | MessageFault fault = MessageFault.CreateFault(rpc.Reply, ClientRuntime.MaxFaultSize); |
| | 0 | 830 | | string action = rpc.Reply.Headers.Action; |
| | 0 | 831 | | if (action == rpc.Reply.Version.Addressing.DefaultFaultAction) |
| | | 832 | | { |
| | 0 | 833 | | action = null; |
| | | 834 | | } |
| | 0 | 835 | | ThrowIfFaultUnderstood(rpc.Reply, fault, action, rpc.Reply.Version, rpc.Channel.GetProperty<Faul |
| | 0 | 836 | | FaultException fe = rpc.Operation.FaultFormatter.Deserialize(fault, action); |
| | 0 | 837 | | TerminateIfNecessary(ref rpc); |
| | 0 | 838 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(fe); |
| | | 839 | | } |
| | | 840 | | |
| | 1 | 841 | | operation.AfterReply(ref rpc); |
| | | 842 | | } |
| | 1 | 843 | | } |
| | | 844 | | finally |
| | | 845 | | { |
| | 1 | 846 | | if (operation.SerializeRequest) |
| | | 847 | | { |
| | 1 | 848 | | rpc.Request.Close(); |
| | | 849 | | } |
| | | 850 | | |
| | 1 | 851 | | OperationContext operationContext = OperationContext.Current; |
| | 1 | 852 | | bool consumed = ((rpc.Reply != null) && (rpc.Reply.State != MessageState.Created)); |
| | | 853 | | |
| | 1 | 854 | | if ((operationContext != null) && operationContext.IsUserContext) |
| | | 855 | | { |
| | 0 | 856 | | operationContext.SetClientReply(rpc.Reply, consumed); |
| | | 857 | | } |
| | 1 | 858 | | else if (consumed) |
| | | 859 | | { |
| | 1 | 860 | | rpc.Reply.Close(); |
| | | 861 | | } |
| | | 862 | | |
| | | 863 | | //if (TraceUtility.MessageFlowTracingOnly) |
| | | 864 | | //{ |
| | | 865 | | // if (rpc.ActivityId != Guid.Empty) |
| | | 866 | | // { |
| | | 867 | | // //reset the ID as it was created internally - ensures each call is uniquely correlatable |
| | | 868 | | // System.Runtime.Diagnostics.DiagnosticTraceBase.ActivityId = Guid.Empty; |
| | | 869 | | // rpc.ActivityId = Guid.Empty; |
| | | 870 | | // } |
| | | 871 | | //} |
| | 1 | 872 | | } |
| | 1 | 873 | | TerminateIfNecessary(ref rpc); |
| | | 874 | | |
| | | 875 | | //if (TD.ServiceChannelCallStopIsEnabled()) |
| | | 876 | | //{ |
| | | 877 | | // string remoteAddress = string.Empty; |
| | | 878 | | // if (this.RemoteAddress != null && this.RemoteAddress.Uri != null) |
| | | 879 | | // { |
| | | 880 | | // remoteAddress = this.RemoteAddress.Uri.AbsoluteUri; |
| | | 881 | | // } |
| | | 882 | | // TD.ServiceChannelCallStop(rpc.EventTraceActivity, rpc.Action, |
| | | 883 | | // this.clientRuntime.ContractName, |
| | | 884 | | // remoteAddress); |
| | | 885 | | //} |
| | 1 | 886 | | } |
| | | 887 | | |
| | | 888 | | private void TerminateIfNecessary(ref ProxyRpc rpc) |
| | | 889 | | { |
| | 1 | 890 | | if (rpc.Operation.IsTerminating) |
| | | 891 | | { |
| | 0 | 892 | | _terminatingOperationName = rpc.Operation.Name; |
| | 0 | 893 | | TerminatingOperationBehavior.AfterReply(ref rpc); |
| | | 894 | | } |
| | 1 | 895 | | } |
| | | 896 | | |
| | | 897 | | private void ThrowIfFaultUnderstood(Message reply, MessageFault fault, string action, MessageVersion version, Fa |
| | | 898 | | { |
| | 0 | 899 | | if (faultConverter != null && faultConverter.TryCreateException(reply, fault, out Exception exception)) |
| | | 900 | | { |
| | 0 | 901 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(exception); |
| | | 902 | | } |
| | | 903 | | |
| | | 904 | | bool checkSender; |
| | | 905 | | bool checkReceiver; |
| | | 906 | | FaultCode code; |
| | | 907 | | |
| | 0 | 908 | | if (version.Envelope == EnvelopeVersion.Soap11) |
| | | 909 | | { |
| | 0 | 910 | | checkSender = true; |
| | 0 | 911 | | checkReceiver = true; |
| | 0 | 912 | | code = fault.Code; |
| | | 913 | | } |
| | | 914 | | else |
| | | 915 | | { |
| | 0 | 916 | | checkSender = fault.Code.IsSenderFault; |
| | 0 | 917 | | checkReceiver = fault.Code.IsReceiverFault; |
| | 0 | 918 | | code = fault.Code.SubCode; |
| | | 919 | | } |
| | | 920 | | |
| | 0 | 921 | | if (code == null) |
| | | 922 | | { |
| | 0 | 923 | | return; |
| | | 924 | | } |
| | | 925 | | |
| | 0 | 926 | | if (code.Namespace == null) |
| | | 927 | | { |
| | 0 | 928 | | return; |
| | | 929 | | } |
| | | 930 | | |
| | 0 | 931 | | if (checkSender) |
| | | 932 | | { |
| | 0 | 933 | | if (string.Compare(code.Namespace, FaultCodeConstants.Namespaces.NetDispatch, StringComparison.Ordinal) |
| | | 934 | | { |
| | 0 | 935 | | if (string.Compare(code.Name, FaultCodeConstants.Codes.SessionTerminated, StringComparison.Ordinal) |
| | | 936 | | { |
| | 0 | 937 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new ChannelTerminatedException(fault |
| | | 938 | | } |
| | | 939 | | |
| | | 940 | | //if (string.Compare(code.Name, FaultCodeConstants.Codes.TransactionAborted, StringComparison.Ordina |
| | | 941 | | //{ |
| | | 942 | | // throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new ProtocolException(fault.Reason |
| | | 943 | | //} |
| | | 944 | | } |
| | | 945 | | |
| | | 946 | | // throw SecurityAccessDeniedException explicitly |
| | | 947 | | // MessageSecurity |
| | | 948 | | //if (string.Compare(code.Namespace, SecurityVersion.Default.HeaderNamespace.Value, StringComparison.Ord |
| | | 949 | | //{ |
| | | 950 | | // if (string.Compare(code.Name, SecurityVersion.Default.FailedAuthenticationFaultCode.Value, StringC |
| | | 951 | | // { |
| | | 952 | | // throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new SecurityAccessDeniedException( |
| | | 953 | | // } |
| | | 954 | | //} |
| | | 955 | | } |
| | | 956 | | |
| | 0 | 957 | | if (checkReceiver) |
| | | 958 | | { |
| | 0 | 959 | | if (string.Compare(code.Namespace, FaultCodeConstants.Namespaces.NetDispatch, StringComparison.Ordinal) |
| | | 960 | | { |
| | 0 | 961 | | if (string.Compare(code.Name, FaultCodeConstants.Codes.InternalServiceFault, StringComparison.Ordina |
| | | 962 | | { |
| | 0 | 963 | | if (HasSession) |
| | | 964 | | { |
| | 0 | 965 | | Fault(); |
| | | 966 | | } |
| | 0 | 967 | | if (fault.HasDetail) |
| | | 968 | | { |
| | 0 | 969 | | ExceptionDetail detail = fault.GetDetail<ExceptionDetail>(); |
| | 0 | 970 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new FaultException<ExceptionDeta |
| | | 971 | | } |
| | 0 | 972 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new FaultException(fault, action)); |
| | | 973 | | } |
| | 0 | 974 | | if (string.Compare(code.Name, FaultCodeConstants.Codes.DeserializationFailed, StringComparison.Ordin |
| | | 975 | | { |
| | 0 | 976 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new ProtocolException( |
| | 0 | 977 | | fault.Reason.GetMatchingTranslation(CultureInfo.CurrentCulture).Text)); |
| | | 978 | | } |
| | | 979 | | } |
| | | 980 | | } |
| | 0 | 981 | | } |
| | | 982 | | |
| | | 983 | | private void ThrowIfIdleAborted(ProxyOperationRuntime operation) |
| | | 984 | | { |
| | 1 | 985 | | if (_idleManager != null && _idleManager.DidIdleAbort) |
| | | 986 | | { |
| | 0 | 987 | | string text = SR.Format(SR.SFxServiceChannelIdleAborted, operation.Name); |
| | 0 | 988 | | Exception error = new CommunicationObjectAbortedException(text); |
| | 0 | 989 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(error); |
| | | 990 | | } |
| | 1 | 991 | | } |
| | | 992 | | |
| | | 993 | | private void ThrowIfIsConnectionOpened(ProxyOperationRuntime operation) |
| | | 994 | | { |
| | 1 | 995 | | if (operation.IsSessionOpenNotificationEnabled) |
| | | 996 | | { |
| | 0 | 997 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 998 | | SR.Format(SR.SFxServiceChannelCannotBeCalledBecauseIsSessionOpenNotificationEnabled, operation.Name, |
| | | 999 | | } |
| | 1 | 1000 | | } |
| | | 1001 | | |
| | | 1002 | | private void ThrowIfOpening() |
| | | 1003 | | { |
| | 1 | 1004 | | if (State == CommunicationState.Opening) |
| | | 1005 | | { |
| | 0 | 1006 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.SFxCannotCall |
| | | 1007 | | } |
| | 1 | 1008 | | } |
| | | 1009 | | |
| | | 1010 | | internal void IncrementActivity() |
| | | 1011 | | { |
| | 2996 | 1012 | | Interlocked.Increment(ref _activityCount); |
| | 2996 | 1013 | | } |
| | | 1014 | | |
| | | 1015 | | private void OnInnerChannelFaulted(object sender, EventArgs e) |
| | | 1016 | | { |
| | 0 | 1017 | | Fault(); |
| | | 1018 | | |
| | 0 | 1019 | | if (HasSession) |
| | | 1020 | | { |
| | 0 | 1021 | | DispatchRuntime dispatchRuntime = ClientRuntime.DispatchRuntime; |
| | 0 | 1022 | | if (dispatchRuntime != null) |
| | | 1023 | | { |
| | 0 | 1024 | | dispatchRuntime.GetRuntime().InputSessionFaulted(this); |
| | | 1025 | | } |
| | | 1026 | | } |
| | | 1027 | | |
| | 0 | 1028 | | if (_autoClose) |
| | | 1029 | | { |
| | 0 | 1030 | | Abort(); |
| | | 1031 | | } |
| | 0 | 1032 | | } |
| | | 1033 | | |
| | | 1034 | | private void AddMessageProperties(Message message, OperationContext context) |
| | | 1035 | | { |
| | 1 | 1036 | | if (_allowOutputBatching) |
| | | 1037 | | { |
| | 0 | 1038 | | message.Properties.AllowOutputBatching = true; |
| | | 1039 | | } |
| | | 1040 | | |
| | 1 | 1041 | | if (context != null && context.InternalServiceChannel == this) |
| | | 1042 | | { |
| | 1 | 1043 | | if (!context.OutgoingMessageVersion.IsMatch(message.Headers.MessageVersion)) |
| | | 1044 | | { |
| | 0 | 1045 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 1046 | | SR.Format(SR.SFxVersionMismatchInOperationContextAndMessage2, context.OutgoingMessageVersion, me |
| | 0 | 1047 | | )); |
| | | 1048 | | } |
| | | 1049 | | |
| | 1 | 1050 | | if (context.HasOutgoingMessageHeaders) |
| | | 1051 | | { |
| | 0 | 1052 | | message.Headers.CopyHeadersFrom(context.OutgoingMessageHeaders); |
| | | 1053 | | } |
| | | 1054 | | |
| | 1 | 1055 | | if (context.HasOutgoingMessageProperties) |
| | | 1056 | | { |
| | 0 | 1057 | | message.Properties.CopyProperties(context.OutgoingMessageProperties); |
| | | 1058 | | } |
| | | 1059 | | } |
| | 1 | 1060 | | } |
| | | 1061 | | |
| | | 1062 | | #region IChannel Members |
| | | 1063 | | public Task SendAsync(Message message) |
| | | 1064 | | { |
| | 0 | 1065 | | var helper = new TimeoutHelper(OperationTimeout); |
| | 0 | 1066 | | return SendAsync(message, helper.GetCancellationToken()); |
| | | 1067 | | } |
| | | 1068 | | |
| | | 1069 | | public Task SendAsync(Message message, CancellationToken token) |
| | | 1070 | | { |
| | 0 | 1071 | | ProxyOperationRuntime operation = UnhandledProxyOperation; |
| | 0 | 1072 | | return CallAsync(message.Headers.Action, true, operation, new object[] { message }, Array.Empty<object>(), t |
| | | 1073 | | } |
| | | 1074 | | |
| | | 1075 | | public Task<Message> RequestAsync(Message message) |
| | | 1076 | | { |
| | 0 | 1077 | | var helper = new TimeoutHelper(OperationTimeout); |
| | 0 | 1078 | | return RequestAsync(message, helper.GetCancellationToken()); |
| | | 1079 | | } |
| | | 1080 | | |
| | | 1081 | | public async Task<Message> RequestAsync(Message message, CancellationToken token) |
| | | 1082 | | { |
| | 0 | 1083 | | ProxyOperationRuntime operation = UnhandledProxyOperation; |
| | 0 | 1084 | | return (Message)await CallAsync(message.Headers.Action, false, operation, new object[] { message }, Array.Em |
| | 0 | 1085 | | } |
| | | 1086 | | |
| | | 1087 | | protected override void OnAbort() |
| | | 1088 | | { |
| | 1 | 1089 | | if (_idleManager != null) |
| | | 1090 | | { |
| | 1 | 1091 | | _idleManager.CancelTimer(); |
| | | 1092 | | } |
| | | 1093 | | |
| | 1 | 1094 | | Binder.Abort(); |
| | | 1095 | | |
| | 1 | 1096 | | CleanupChannelCollections(); |
| | | 1097 | | |
| | | 1098 | | //ServiceThrottle serviceThrottle = this.serviceThrottle; |
| | | 1099 | | //if (serviceThrottle != null) |
| | | 1100 | | // serviceThrottle.DeactivateChannel(); |
| | | 1101 | | |
| | | 1102 | | //rollback the attached transaction if one is present |
| | | 1103 | | //if ((this.instanceContext != null) && this.HasSession) |
| | | 1104 | | //{ |
| | | 1105 | | // if (instanceContext.HasTransaction) |
| | | 1106 | | // { |
| | | 1107 | | // instanceContext.Transaction.CompletePendingTransaction(instanceContext.Transaction.Attached, new E |
| | | 1108 | | // } |
| | | 1109 | | //} |
| | 1 | 1110 | | } |
| | | 1111 | | |
| | | 1112 | | protected override async Task OnCloseAsync(CancellationToken token) |
| | | 1113 | | { |
| | 78 | 1114 | | if (_idleManager != null) |
| | | 1115 | | { |
| | 78 | 1116 | | _idleManager.CancelTimer(); |
| | | 1117 | | } |
| | | 1118 | | |
| | | 1119 | | //if (this.InstanceContext != null && this.InstanceContext.HasTransaction) |
| | | 1120 | | //{ |
| | | 1121 | | // this.InstanceContext.CompleteAttachedTransaction(); |
| | | 1122 | | //} |
| | | 1123 | | |
| | 78 | 1124 | | if (_closeBinder) |
| | | 1125 | | { |
| | 78 | 1126 | | await InnerChannel.CloseAsync(token); |
| | | 1127 | | } |
| | | 1128 | | |
| | 78 | 1129 | | CleanupChannelCollections(); |
| | | 1130 | | |
| | | 1131 | | //ServiceThrottle serviceThrottle = this.serviceThrottle; |
| | | 1132 | | //if (serviceThrottle != null) |
| | | 1133 | | //{ |
| | | 1134 | | // serviceThrottle.DeactivateChannel(); |
| | | 1135 | | //} |
| | 78 | 1136 | | } |
| | | 1137 | | |
| | | 1138 | | protected override async Task OnOpenAsync(CancellationToken token) |
| | | 1139 | | { |
| | 516 | 1140 | | if (_autoOpenManager == null) |
| | | 1141 | | { |
| | 516 | 1142 | | _explicitlyOpened = true; |
| | | 1143 | | } |
| | | 1144 | | |
| | | 1145 | | //this.TraceChannelOpenStarted(); |
| | | 1146 | | |
| | 516 | 1147 | | if (_openBinder) |
| | | 1148 | | { |
| | 15 | 1149 | | await InnerChannel.OpenAsync(token); |
| | | 1150 | | } |
| | | 1151 | | |
| | 516 | 1152 | | CompletedIOOperation(); |
| | | 1153 | | |
| | | 1154 | | //this.TraceChannelOpenCompleted(); |
| | 516 | 1155 | | } |
| | | 1156 | | |
| | | 1157 | | private void CleanupChannelCollections() |
| | | 1158 | | { |
| | 79 | 1159 | | if (!_hasCleanedUpChannelCollections) |
| | | 1160 | | { |
| | 79 | 1161 | | lock (ThisLock) |
| | | 1162 | | { |
| | 79 | 1163 | | if (!_hasCleanedUpChannelCollections) |
| | | 1164 | | { |
| | 79 | 1165 | | if (InstanceContext != null) |
| | | 1166 | | { |
| | 79 | 1167 | | InstanceContext.OutgoingChannels.Remove((IChannel)_proxy); |
| | | 1168 | | } |
| | | 1169 | | |
| | 79 | 1170 | | _hasCleanedUpChannelCollections = true; |
| | | 1171 | | } |
| | 79 | 1172 | | } |
| | | 1173 | | } |
| | 79 | 1174 | | } |
| | | 1175 | | #endregion |
| | | 1176 | | |
| | | 1177 | | #region IClientChannel Members |
| | | 1178 | | |
| | | 1179 | | bool IDuplexContextChannel.AutomaticInputSessionShutdown |
| | | 1180 | | { |
| | 0 | 1181 | | get { return _autoClose; } |
| | 0 | 1182 | | set { _autoClose = value; } |
| | | 1183 | | } |
| | | 1184 | | |
| | | 1185 | | //bool IContextChannel.AllowOutputBatching |
| | | 1186 | | //{ |
| | | 1187 | | // get { return this.allowOutputBatching; } |
| | | 1188 | | // set { this.allowOutputBatching = value; } |
| | | 1189 | | //} |
| | | 1190 | | |
| | | 1191 | | Task IDuplexContextChannel.CloseOutputSessionAsync(CancellationToken token) |
| | | 1192 | | { |
| | 0 | 1193 | | return GetDuplexSessionOrThrow().CloseOutputSessionAsync(token); |
| | | 1194 | | } |
| | | 1195 | | |
| | | 1196 | | private IDuplexSession GetDuplexSessionOrThrow() |
| | | 1197 | | { |
| | 0 | 1198 | | if (InnerChannel == null) |
| | | 1199 | | { |
| | 0 | 1200 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.channelIsNotA |
| | | 1201 | | } |
| | | 1202 | | |
| | 0 | 1203 | | if (!(InnerChannel is ISessionChannel<IDuplexSession> duplexSessionChannel)) |
| | | 1204 | | { |
| | 0 | 1205 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.channelDoesNo |
| | | 1206 | | } |
| | | 1207 | | |
| | 0 | 1208 | | return duplexSessionChannel.Session; |
| | | 1209 | | } |
| | | 1210 | | |
| | | 1211 | | IExtensionCollection<IContextChannel> IExtensibleObject<IContextChannel>.Extensions |
| | | 1212 | | { |
| | | 1213 | | get |
| | | 1214 | | { |
| | 0 | 1215 | | lock (ThisLock) |
| | | 1216 | | { |
| | 0 | 1217 | | if (_extensions == null) |
| | | 1218 | | { |
| | 0 | 1219 | | _extensions = new ExtensionCollection<IContextChannel>((IContextChannel)Proxy, ThisLock); |
| | | 1220 | | } |
| | | 1221 | | |
| | 0 | 1222 | | return _extensions; |
| | | 1223 | | } |
| | 0 | 1224 | | } |
| | | 1225 | | } |
| | | 1226 | | |
| | | 1227 | | InstanceContext IDuplexContextChannel.CallbackInstance |
| | | 1228 | | { |
| | 0 | 1229 | | get { return InstanceContext; } |
| | | 1230 | | set |
| | | 1231 | | { |
| | 0 | 1232 | | lock (ThisLock) |
| | | 1233 | | { |
| | 0 | 1234 | | if (InstanceContext != null) |
| | | 1235 | | { |
| | 0 | 1236 | | InstanceContext.OutgoingChannels.Remove((IChannel)_proxy); |
| | | 1237 | | } |
| | | 1238 | | |
| | 0 | 1239 | | InstanceContext = value; |
| | | 1240 | | |
| | 0 | 1241 | | if (InstanceContext != null) |
| | | 1242 | | { |
| | 0 | 1243 | | InstanceContext.OutgoingChannels.Add((IChannel)_proxy); |
| | | 1244 | | } |
| | 0 | 1245 | | } |
| | 0 | 1246 | | } |
| | | 1247 | | } |
| | | 1248 | | |
| | | 1249 | | IInputSession IContextChannel.InputSession |
| | | 1250 | | { |
| | | 1251 | | get |
| | | 1252 | | { |
| | 0 | 1253 | | if (InnerChannel != null) |
| | | 1254 | | { |
| | 0 | 1255 | | if (InnerChannel is ISessionChannel<IInputSession> inputSession) |
| | | 1256 | | { |
| | 0 | 1257 | | return inputSession.Session; |
| | | 1258 | | } |
| | | 1259 | | |
| | 0 | 1260 | | if (InnerChannel is ISessionChannel<IDuplexSession> duplexSession) |
| | | 1261 | | { |
| | 0 | 1262 | | return duplexSession.Session; |
| | | 1263 | | } |
| | | 1264 | | } |
| | | 1265 | | |
| | 0 | 1266 | | return null; |
| | | 1267 | | } |
| | | 1268 | | } |
| | | 1269 | | |
| | | 1270 | | IOutputSession IContextChannel.OutputSession |
| | | 1271 | | { |
| | | 1272 | | get |
| | | 1273 | | { |
| | 0 | 1274 | | if (InnerChannel != null) |
| | | 1275 | | { |
| | 0 | 1276 | | if (InnerChannel is ISessionChannel<IOutputSession> outputSession) |
| | | 1277 | | { |
| | 0 | 1278 | | return outputSession.Session; |
| | | 1279 | | } |
| | | 1280 | | |
| | 0 | 1281 | | if (InnerChannel is ISessionChannel<IDuplexSession> duplexSession) |
| | | 1282 | | { |
| | 0 | 1283 | | return duplexSession.Session; |
| | | 1284 | | } |
| | | 1285 | | } |
| | | 1286 | | |
| | 0 | 1287 | | return null; |
| | | 1288 | | } |
| | | 1289 | | } |
| | | 1290 | | |
| | | 1291 | | string IContextChannel.SessionId |
| | | 1292 | | { |
| | | 1293 | | get |
| | | 1294 | | { |
| | 0 | 1295 | | if (InnerChannel != null) |
| | | 1296 | | { |
| | 0 | 1297 | | if (InnerChannel is ISessionChannel<IInputSession> inputSession) |
| | | 1298 | | { |
| | 0 | 1299 | | return inputSession.Session.Id; |
| | | 1300 | | } |
| | | 1301 | | |
| | 0 | 1302 | | if (InnerChannel is ISessionChannel<IOutputSession> outputSession) |
| | | 1303 | | { |
| | 0 | 1304 | | return outputSession.Session.Id; |
| | | 1305 | | } |
| | | 1306 | | |
| | 0 | 1307 | | if (InnerChannel is ISessionChannel<IDuplexSession> duplexSession) |
| | | 1308 | | { |
| | 0 | 1309 | | return duplexSession.Session.Id; |
| | | 1310 | | } |
| | | 1311 | | } |
| | | 1312 | | |
| | 0 | 1313 | | return null; |
| | | 1314 | | } |
| | | 1315 | | } |
| | | 1316 | | |
| | | 1317 | | IServiceChannelDispatcher IChannel.ChannelDispatcher |
| | | 1318 | | { |
| | 0 | 1319 | | get => throw new NotSupportedException(); |
| | 0 | 1320 | | set => throw new NotSupportedException(); |
| | | 1321 | | } |
| | | 1322 | | |
| | | 1323 | | event EventHandler<UnknownMessageReceivedEventArgs> IClientChannel.UnknownMessageReceived |
| | | 1324 | | { |
| | | 1325 | | add |
| | | 1326 | | { |
| | 0 | 1327 | | lock (ThisLock) |
| | | 1328 | | { |
| | 0 | 1329 | | _unknownMessageReceived += value; |
| | 0 | 1330 | | } |
| | 0 | 1331 | | } |
| | | 1332 | | remove |
| | | 1333 | | { |
| | 0 | 1334 | | lock (ThisLock) |
| | | 1335 | | { |
| | 0 | 1336 | | _unknownMessageReceived -= value; |
| | 0 | 1337 | | } |
| | 0 | 1338 | | } |
| | | 1339 | | } |
| | | 1340 | | |
| | | 1341 | | void IDisposable.Dispose() |
| | | 1342 | | { |
| | 0 | 1343 | | CloseAsync().GetAwaiter().GetResult(); |
| | 0 | 1344 | | } |
| | | 1345 | | |
| | | 1346 | | #endregion |
| | | 1347 | | |
| | | 1348 | | //void TraceChannelOpenStarted() |
| | | 1349 | | //{ |
| | | 1350 | | // if (TD.ClientChannelOpenStartIsEnabled() && this.endpointDispatcher == null) |
| | | 1351 | | // { |
| | | 1352 | | // TD.ClientChannelOpenStart(this.EventActivity); |
| | | 1353 | | // } |
| | | 1354 | | // else if (TD.ServiceChannelOpenStartIsEnabled()) |
| | | 1355 | | // { |
| | | 1356 | | // TD.ServiceChannelOpenStart(this.EventActivity); |
| | | 1357 | | // } |
| | | 1358 | | |
| | | 1359 | | // //if (DiagnosticUtility.ShouldTraceInformation) |
| | | 1360 | | // //{ |
| | | 1361 | | // // Dictionary<string, string> values = new Dictionary<string, string>(4); |
| | | 1362 | | // // bool traceNeeded = false; |
| | | 1363 | | // // DispatchRuntime behavior = this.DispatchRuntime; |
| | | 1364 | | // // if (behavior != null) |
| | | 1365 | | // // { |
| | | 1366 | | // // if (behavior.Type != null) |
| | | 1367 | | // // { |
| | | 1368 | | // // values["ServiceType"] = behavior.Type.AssemblyQualifiedName; |
| | | 1369 | | // // } |
| | | 1370 | | // // values["ContractNamespace"] = this.clientRuntime.ContractNamespace; |
| | | 1371 | | // // values["ContractName"] = this.clientRuntime.ContractName; |
| | | 1372 | | // // traceNeeded = true; |
| | | 1373 | | // // } |
| | | 1374 | | // // if ((this.endpointDispatcher != null) && (this.endpointDispatcher.ListenUri != null)) |
| | | 1375 | | // // { |
| | | 1376 | | // // values["Uri"] = this.endpointDispatcher.ListenUri.ToString(); |
| | | 1377 | | // // traceNeeded = true; |
| | | 1378 | | // // } |
| | | 1379 | | // // if (traceNeeded) |
| | | 1380 | | // // { |
| | | 1381 | | // // TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.ServiceChannelLifetime, |
| | | 1382 | | // // SR.Format(SR.TraceCodeServiceChannelLifetime), |
| | | 1383 | | // // new DictionaryTraceRecord(values), this, null); |
| | | 1384 | | // // } |
| | | 1385 | | // //} |
| | | 1386 | | //} |
| | | 1387 | | |
| | | 1388 | | //void TraceChannelOpenCompleted() |
| | | 1389 | | //{ |
| | | 1390 | | // if (this.endpointDispatcher == null && TD.ClientChannelOpenStopIsEnabled()) |
| | | 1391 | | // { |
| | | 1392 | | // TD.ClientChannelOpenStop(this.EventActivity); |
| | | 1393 | | // } |
| | | 1394 | | // else if (TD.ServiceChannelOpenStopIsEnabled()) |
| | | 1395 | | // { |
| | | 1396 | | // TD.ServiceChannelOpenStop(this.EventActivity); |
| | | 1397 | | // } |
| | | 1398 | | //} |
| | | 1399 | | |
| | | 1400 | | //static void TraceServiceChannelCallStart(EventTraceActivity eventTraceActivity, bool isSynchronous) |
| | | 1401 | | //{ |
| | | 1402 | | // if (TD.ServiceChannelCallStartIsEnabled()) |
| | | 1403 | | // { |
| | | 1404 | | // if (isSynchronous) |
| | | 1405 | | // { |
| | | 1406 | | // TD.ServiceChannelCallStart(eventTraceActivity); |
| | | 1407 | | // } |
| | | 1408 | | // else |
| | | 1409 | | // { |
| | | 1410 | | // TD.ServiceChannelBeginCallStart(eventTraceActivity); |
| | | 1411 | | // } |
| | | 1412 | | // } |
| | | 1413 | | //} |
| | | 1414 | | |
| | | 1415 | | // Invariants for signalling the CallOnce manager. |
| | | 1416 | | // |
| | | 1417 | | // 1) If a Call, BeginCall, or EndCall on the channel throws, |
| | | 1418 | | // the manager will SignalNext itself. |
| | | 1419 | | // 2) If a Waiter times out, it will SignalNext its manager |
| | | 1420 | | // once it is both timed out and signalled. |
| | | 1421 | | // 3) Once Call or EndCall returns successfully, it guarantees |
| | | 1422 | | // that SignalNext will be called once the // next stage |
| | | 1423 | | // has sufficiently completed. |
| | | 1424 | | private interface ICallOnce |
| | | 1425 | | { |
| | | 1426 | | Task CallAsync(ServiceChannel channel, CancellationToken token); |
| | | 1427 | | } |
| | | 1428 | | |
| | | 1429 | | private class CallOpenOnce : ICallOnce |
| | | 1430 | | { |
| | | 1431 | | private static CallOpenOnce s_instance; |
| | | 1432 | | |
| | | 1433 | | internal static CallOpenOnce Instance |
| | | 1434 | | { |
| | | 1435 | | get |
| | | 1436 | | { |
| | 0 | 1437 | | if (s_instance == null) |
| | | 1438 | | { |
| | 0 | 1439 | | s_instance = new CallOpenOnce(); |
| | | 1440 | | } |
| | 0 | 1441 | | return s_instance; |
| | | 1442 | | } |
| | | 1443 | | } |
| | | 1444 | | |
| | | 1445 | | Task ICallOnce.CallAsync(ServiceChannel channel, CancellationToken token) |
| | | 1446 | | { |
| | 0 | 1447 | | return channel.OpenAsync(token); |
| | | 1448 | | } |
| | | 1449 | | } |
| | | 1450 | | |
| | | 1451 | | private class CallOnceManager |
| | | 1452 | | { |
| | | 1453 | | private readonly ICallOnce _callOnce; |
| | | 1454 | | private readonly ServiceChannel _channel; |
| | 0 | 1455 | | private bool _isFirst = true; |
| | | 1456 | | private Queue<IWaiter> _queue; |
| | 0 | 1457 | | private static readonly Action<object> s_signalWaiter = SignalWaiter; |
| | | 1458 | | |
| | 0 | 1459 | | internal CallOnceManager(ServiceChannel channel, ICallOnce callOnce) |
| | | 1460 | | { |
| | 0 | 1461 | | _callOnce = callOnce; |
| | 0 | 1462 | | _channel = channel; |
| | 0 | 1463 | | _queue = new Queue<IWaiter>(); |
| | 0 | 1464 | | } |
| | | 1465 | | |
| | | 1466 | | private object ThisLock |
| | | 1467 | | { |
| | 0 | 1468 | | get { return this; } |
| | | 1469 | | } |
| | | 1470 | | |
| | | 1471 | | internal async Task CallOnceAsync(CancellationToken token) |
| | | 1472 | | { |
| | 0 | 1473 | | AsyncWaiter waiter = null; |
| | 0 | 1474 | | bool first = false; |
| | | 1475 | | |
| | 0 | 1476 | | if (_queue != null) |
| | | 1477 | | { |
| | 0 | 1478 | | lock (ThisLock) |
| | | 1479 | | { |
| | 0 | 1480 | | if (_queue != null) |
| | | 1481 | | { |
| | 0 | 1482 | | if (_isFirst) |
| | | 1483 | | { |
| | 0 | 1484 | | first = true; |
| | 0 | 1485 | | _isFirst = false; |
| | | 1486 | | } |
| | | 1487 | | else |
| | | 1488 | | { |
| | 0 | 1489 | | waiter = new AsyncWaiter(this); |
| | 0 | 1490 | | _queue.Enqueue(waiter); |
| | | 1491 | | } |
| | | 1492 | | } |
| | 0 | 1493 | | } |
| | | 1494 | | } |
| | | 1495 | | |
| | 0 | 1496 | | if (first) |
| | | 1497 | | { |
| | 0 | 1498 | | bool throwing = true; |
| | | 1499 | | try |
| | | 1500 | | { |
| | 0 | 1501 | | await _callOnce.CallAsync(_channel, token); |
| | 0 | 1502 | | throwing = false; |
| | 0 | 1503 | | } |
| | | 1504 | | finally |
| | | 1505 | | { |
| | 0 | 1506 | | if (throwing) |
| | | 1507 | | { |
| | 0 | 1508 | | SignalNext(); |
| | | 1509 | | } |
| | | 1510 | | } |
| | | 1511 | | } |
| | 0 | 1512 | | else if (waiter != null) |
| | | 1513 | | { |
| | 0 | 1514 | | await waiter.WaitAsync(token); |
| | | 1515 | | } |
| | 0 | 1516 | | } |
| | | 1517 | | |
| | | 1518 | | internal static void SignalNextIfNonNull(CallOnceManager manager) |
| | | 1519 | | { |
| | 1 | 1520 | | if (manager != null) |
| | | 1521 | | { |
| | 0 | 1522 | | manager.SignalNext(); |
| | | 1523 | | } |
| | 1 | 1524 | | } |
| | | 1525 | | |
| | | 1526 | | internal void SignalNext() |
| | | 1527 | | { |
| | 0 | 1528 | | if (_queue == null) |
| | | 1529 | | { |
| | 0 | 1530 | | return; |
| | | 1531 | | } |
| | | 1532 | | |
| | 0 | 1533 | | IWaiter waiter = null; |
| | | 1534 | | |
| | 0 | 1535 | | lock (ThisLock) |
| | | 1536 | | { |
| | 0 | 1537 | | if (_queue != null) |
| | | 1538 | | { |
| | 0 | 1539 | | if (_queue.Count > 0) |
| | | 1540 | | { |
| | 0 | 1541 | | waiter = _queue.Dequeue(); |
| | | 1542 | | } |
| | | 1543 | | else |
| | | 1544 | | { |
| | 0 | 1545 | | _queue = null; |
| | | 1546 | | } |
| | | 1547 | | } |
| | 0 | 1548 | | } |
| | | 1549 | | |
| | 0 | 1550 | | if (waiter != null) |
| | | 1551 | | { |
| | 0 | 1552 | | ActionItem.Schedule(s_signalWaiter, waiter); |
| | | 1553 | | } |
| | 0 | 1554 | | } |
| | | 1555 | | |
| | | 1556 | | private static void SignalWaiter(object state) |
| | | 1557 | | { |
| | 0 | 1558 | | ((IWaiter)state).Signal(); |
| | 0 | 1559 | | } |
| | | 1560 | | |
| | | 1561 | | private interface IWaiter |
| | | 1562 | | { |
| | | 1563 | | void Signal(); |
| | | 1564 | | } |
| | | 1565 | | |
| | | 1566 | | private class AsyncWaiter : IWaiter |
| | | 1567 | | { |
| | 0 | 1568 | | private readonly AsyncManualResetEvent _wait = new AsyncManualResetEvent(); |
| | | 1569 | | private readonly CallOnceManager _manager; |
| | | 1570 | | private bool _isTimedOut = false; |
| | | 1571 | | private bool _isSignaled = false; |
| | | 1572 | | private int _waitCount = 0; |
| | | 1573 | | |
| | 0 | 1574 | | internal AsyncWaiter(CallOnceManager manager) |
| | | 1575 | | { |
| | 0 | 1576 | | _manager = manager; |
| | 0 | 1577 | | } |
| | | 1578 | | |
| | | 1579 | | private bool ShouldSignalNext |
| | | 1580 | | { |
| | 0 | 1581 | | get { return _isTimedOut && _isSignaled; } |
| | | 1582 | | } |
| | | 1583 | | |
| | | 1584 | | void IWaiter.Signal() |
| | | 1585 | | { |
| | 0 | 1586 | | _wait.Set(); |
| | 0 | 1587 | | CloseWaitHandle(); |
| | | 1588 | | |
| | | 1589 | | bool signalNext; |
| | 0 | 1590 | | lock (_manager.ThisLock) |
| | | 1591 | | { |
| | 0 | 1592 | | _isSignaled = true; |
| | 0 | 1593 | | signalNext = ShouldSignalNext; |
| | 0 | 1594 | | } |
| | 0 | 1595 | | if (signalNext) |
| | | 1596 | | { |
| | 0 | 1597 | | _manager.SignalNext(); |
| | | 1598 | | } |
| | 0 | 1599 | | } |
| | | 1600 | | |
| | | 1601 | | internal async Task<bool> WaitAsync(CancellationToken token) |
| | | 1602 | | { |
| | | 1603 | | try |
| | | 1604 | | { |
| | 0 | 1605 | | if (!await _wait.WaitAsync(token)) |
| | | 1606 | | { |
| | | 1607 | | bool signalNext; |
| | 0 | 1608 | | lock (_manager.ThisLock) |
| | | 1609 | | { |
| | 0 | 1610 | | _isTimedOut = true; |
| | 0 | 1611 | | signalNext = ShouldSignalNext; |
| | 0 | 1612 | | } |
| | 0 | 1613 | | if (signalNext) |
| | | 1614 | | { |
| | 0 | 1615 | | _manager.SignalNext(); |
| | | 1616 | | } |
| | | 1617 | | } |
| | 0 | 1618 | | } |
| | | 1619 | | finally |
| | | 1620 | | { |
| | 0 | 1621 | | CloseWaitHandle(); |
| | | 1622 | | } |
| | | 1623 | | |
| | 0 | 1624 | | return !_isTimedOut; |
| | 0 | 1625 | | } |
| | | 1626 | | |
| | | 1627 | | private void CloseWaitHandle() |
| | | 1628 | | { |
| | 0 | 1629 | | if (Interlocked.Increment(ref _waitCount) == 2) |
| | | 1630 | | { |
| | 0 | 1631 | | _wait.Dispose(); |
| | | 1632 | | } |
| | 0 | 1633 | | } |
| | | 1634 | | } |
| | | 1635 | | } |
| | | 1636 | | |
| | | 1637 | | internal class SessionIdleManager |
| | | 1638 | | { |
| | | 1639 | | private IChannelBinder _binder; |
| | | 1640 | | private ServiceChannel _channel; |
| | | 1641 | | private long _idleTicks; |
| | | 1642 | | private long _lastActivity; |
| | | 1643 | | private IOThreadTimer _timer; |
| | | 1644 | | private static Action<object> s_timerCallback; |
| | | 1645 | | private bool _didIdleAbort; |
| | | 1646 | | private bool _isTimerCancelled; |
| | | 1647 | | private object _thisLock; |
| | | 1648 | | private bool? _isNeeded = null; |
| | | 1649 | | |
| | 2292 | 1650 | | public SessionIdleManager() |
| | | 1651 | | { |
| | 2292 | 1652 | | _thisLock = new object(); |
| | 2292 | 1653 | | } |
| | | 1654 | | |
| | | 1655 | | internal SessionIdleManager UseIfNeeded(IChannelBinder binder, TimeSpan idle) |
| | | 1656 | | { |
| | 516 | 1657 | | if (_isNeeded.HasValue) |
| | | 1658 | | { |
| | 0 | 1659 | | return _isNeeded.Value ? this : null; |
| | | 1660 | | } |
| | | 1661 | | |
| | 516 | 1662 | | if (binder.HasSession && (idle != TimeSpan.MaxValue)) |
| | | 1663 | | { |
| | 83 | 1664 | | _binder = binder; |
| | 83 | 1665 | | _timer = new IOThreadTimer(GetTimerCallback(), this, false); |
| | 83 | 1666 | | _idleTicks = Ticks.FromTimeSpan(idle); |
| | 83 | 1667 | | _timer.SetAt(Ticks.Now + _idleTicks); |
| | 83 | 1668 | | _isNeeded = true; |
| | 83 | 1669 | | return this; |
| | | 1670 | | } |
| | | 1671 | | else |
| | | 1672 | | { |
| | 433 | 1673 | | _isNeeded = false; |
| | 433 | 1674 | | return null; |
| | | 1675 | | } |
| | | 1676 | | } |
| | | 1677 | | |
| | | 1678 | | internal bool DidIdleAbort |
| | | 1679 | | { |
| | | 1680 | | get |
| | | 1681 | | { |
| | 1 | 1682 | | lock (_thisLock) |
| | | 1683 | | { |
| | 1 | 1684 | | return _didIdleAbort; |
| | | 1685 | | } |
| | 1 | 1686 | | } |
| | | 1687 | | } |
| | | 1688 | | |
| | | 1689 | | internal void CancelTimer() |
| | | 1690 | | { |
| | 82 | 1691 | | lock (_thisLock) |
| | | 1692 | | { |
| | 82 | 1693 | | _isTimerCancelled = true; |
| | 82 | 1694 | | _timer?.Cancel(); |
| | 79 | 1695 | | } |
| | 82 | 1696 | | } |
| | | 1697 | | |
| | | 1698 | | internal void CompletedActivity() |
| | | 1699 | | { |
| | 190 | 1700 | | Interlocked.Exchange(ref _lastActivity, Ticks.Now); |
| | 190 | 1701 | | } |
| | | 1702 | | |
| | | 1703 | | internal void RegisterChannel(ServiceChannel channel, out bool didIdleAbort) |
| | | 1704 | | { |
| | 83 | 1705 | | lock (_thisLock) |
| | | 1706 | | { |
| | 83 | 1707 | | _channel = channel; |
| | 83 | 1708 | | didIdleAbort = _didIdleAbort; |
| | 83 | 1709 | | } |
| | 83 | 1710 | | } |
| | | 1711 | | |
| | | 1712 | | private static Action<object> GetTimerCallback() |
| | | 1713 | | { |
| | 83 | 1714 | | if (s_timerCallback == null) |
| | | 1715 | | { |
| | 4 | 1716 | | s_timerCallback = TimerCallback; |
| | | 1717 | | } |
| | 83 | 1718 | | return s_timerCallback; |
| | | 1719 | | } |
| | | 1720 | | |
| | | 1721 | | private static void TimerCallback(object state) |
| | | 1722 | | { |
| | 2 | 1723 | | ((SessionIdleManager)state).TimerCallback(); |
| | 2 | 1724 | | } |
| | | 1725 | | |
| | | 1726 | | private void TimerCallback() |
| | | 1727 | | { |
| | | 1728 | | // This reads lastActivity atomically without changing its value. |
| | | 1729 | | // (it only sets if it is zero, and then it sets it to zero). |
| | 2 | 1730 | | long last = Interlocked.CompareExchange(ref _lastActivity, 0, 0); |
| | 2 | 1731 | | long abortTime = last + _idleTicks; |
| | | 1732 | | |
| | 2 | 1733 | | lock (_thisLock) |
| | | 1734 | | { |
| | 2 | 1735 | | long ticksNow = Ticks.Now; |
| | 2 | 1736 | | if (ticksNow > abortTime) |
| | | 1737 | | { |
| | 1 | 1738 | | _didIdleAbort = true; |
| | 1 | 1739 | | if (_channel != null) |
| | | 1740 | | { |
| | 1 | 1741 | | _channel.Abort(); |
| | | 1742 | | } |
| | | 1743 | | else |
| | | 1744 | | { |
| | 0 | 1745 | | _binder.Abort(); |
| | | 1746 | | } |
| | | 1747 | | } |
| | | 1748 | | else |
| | | 1749 | | { |
| | 1 | 1750 | | if (!_isTimerCancelled && _binder.Channel.State != CommunicationState.Faulted && _binder.Channel |
| | | 1751 | | { |
| | 1 | 1752 | | _timer.SetAt(abortTime); |
| | | 1753 | | } |
| | | 1754 | | } |
| | 1 | 1755 | | } |
| | 2 | 1756 | | } |
| | | 1757 | | } |
| | | 1758 | | } |
| | | 1759 | | } |