| | | 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.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using CoreWCF.Channels; |
| | | 10 | | using CoreWCF.Diagnostics; |
| | | 11 | | using CoreWCF.Dispatcher; |
| | | 12 | | using CoreWCF.Runtime; |
| | | 13 | | |
| | | 14 | | namespace CoreWCF |
| | | 15 | | { |
| | | 16 | | public sealed class InstanceContext : CommunicationObject, IExtensibleObject<InstanceContext> |
| | | 17 | | { |
| | 7 | 18 | | internal static Action<InstanceContext> NotifyEmptyCallback = NotifyEmpty; |
| | 7 | 19 | | internal static Action<InstanceContext> NotifyIdleCallback = NotifyIdle; |
| | | 20 | | private InstanceBehavior _behavior; |
| | | 21 | | private readonly ServiceChannelManager _channels; |
| | | 22 | | private ConcurrencyInstanceContextFacet _concurrency; |
| | | 23 | | private ExtensionCollection<InstanceContext> _extensions; |
| | | 24 | | private readonly ServiceHostBase _host; |
| | | 25 | | private ServiceThrottle _serviceThrottle; |
| | 2493 | 26 | | private readonly object _serviceInstanceLock = new object(); |
| | | 27 | | private SynchronizationContext _synchronizationContext; |
| | | 28 | | private object _userObject; |
| | | 29 | | |
| | | 30 | | public InstanceContext(object implementation) |
| | 0 | 31 | | : this(null, implementation) |
| | | 32 | | { |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | public InstanceContext(ServiceHostBase host, object implementation) |
| | 0 | 36 | | : this(host, implementation, true) |
| | | 37 | | { |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | internal InstanceContext(ServiceHostBase host, object implementation, bool isUserCreated) |
| | 23 | 41 | | : this(host, implementation, true, isUserCreated) |
| | | 42 | | { |
| | 23 | 43 | | } |
| | | 44 | | |
| | 102 | 45 | | internal InstanceContext(ServiceHostBase host, object implementation, bool wellKnown, bool isUserCreated) |
| | | 46 | | { |
| | 102 | 47 | | _host = host; |
| | 102 | 48 | | if (implementation != null) |
| | | 49 | | { |
| | 102 | 50 | | _userObject = implementation; |
| | 102 | 51 | | IsWellKnown = wellKnown; |
| | | 52 | | } |
| | 102 | 53 | | AutoClose = false; |
| | 102 | 54 | | _channels = new ServiceChannelManager(this); |
| | 102 | 55 | | IsUserCreated = isUserCreated; |
| | 102 | 56 | | } |
| | | 57 | | |
| | 2391 | 58 | | internal InstanceContext(ServiceHostBase host, bool isUserCreated) |
| | | 59 | | { |
| | 2391 | 60 | | _host = host ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(host)); |
| | 2391 | 61 | | AutoClose = true; |
| | 2391 | 62 | | _channels = new ServiceChannelManager(this, NotifyEmptyCallback); |
| | 2391 | 63 | | IsUserCreated = isUserCreated; |
| | 2391 | 64 | | } |
| | | 65 | | |
| | 2575 | 66 | | internal bool IsUserCreated { get; set; } |
| | | 67 | | |
| | 4803 | 68 | | internal bool IsWellKnown { get; } |
| | | 69 | | |
| | 4904 | 70 | | internal bool AutoClose { get; set; } |
| | | 71 | | |
| | | 72 | | internal InstanceBehavior Behavior |
| | | 73 | | { |
| | 0 | 74 | | get { return _behavior; } |
| | | 75 | | set |
| | | 76 | | { |
| | 2661 | 77 | | if (_behavior == null) |
| | | 78 | | { |
| | 2493 | 79 | | _behavior = value; |
| | | 80 | | } |
| | 2661 | 81 | | } |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | internal ConcurrencyInstanceContextFacet Concurrency |
| | | 85 | | { |
| | | 86 | | get |
| | | 87 | | { |
| | 4654 | 88 | | if (_concurrency == null) |
| | | 89 | | { |
| | 2300 | 90 | | lock (ThisLock) |
| | | 91 | | { |
| | 2300 | 92 | | if (_concurrency == null) |
| | | 93 | | { |
| | 2300 | 94 | | _concurrency = new ConcurrencyInstanceContextFacet(); |
| | | 95 | | } |
| | 2300 | 96 | | } |
| | | 97 | | } |
| | | 98 | | |
| | 4654 | 99 | | return _concurrency; |
| | | 100 | | } |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | protected override TimeSpan DefaultCloseTimeout |
| | | 104 | | { |
| | | 105 | | get |
| | | 106 | | { |
| | 2332 | 107 | | if (_host != null) |
| | | 108 | | { |
| | 2332 | 109 | | return _host.CloseTimeout; |
| | | 110 | | } |
| | 0 | 111 | | return ServiceDefaults.CloseTimeout; |
| | | 112 | | } |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | protected override TimeSpan DefaultOpenTimeout |
| | | 116 | | { |
| | | 117 | | get |
| | | 118 | | { |
| | 82 | 119 | | if (_host != null) |
| | | 120 | | { |
| | 72 | 121 | | return _host.OpenTimeout; |
| | | 122 | | } |
| | 10 | 123 | | return ServiceDefaults.OpenTimeout; |
| | | 124 | | } |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | public IExtensionCollection<InstanceContext> Extensions |
| | | 128 | | { |
| | | 129 | | get |
| | | 130 | | { |
| | 7586 | 131 | | ThrowIfClosed(); |
| | 7586 | 132 | | lock (ThisLock) |
| | | 133 | | { |
| | 7586 | 134 | | if (_extensions == null) |
| | | 135 | | { |
| | 1985 | 136 | | _extensions = new ExtensionCollection<InstanceContext>(this, ThisLock); |
| | | 137 | | } |
| | | 138 | | |
| | 7586 | 139 | | return _extensions; |
| | | 140 | | } |
| | 7586 | 141 | | } |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | internal ICollection<IChannel> IncomingChannels |
| | | 145 | | { |
| | | 146 | | get |
| | | 147 | | { |
| | 13 | 148 | | ThrowIfClosed(); |
| | 13 | 149 | | return _channels.IncomingChannels; |
| | | 150 | | } |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | private bool IsBusy |
| | | 154 | | { |
| | | 155 | | get |
| | | 156 | | { |
| | 2332 | 157 | | if (State == CommunicationState.Closed) |
| | | 158 | | { |
| | 0 | 159 | | return false; |
| | | 160 | | } |
| | | 161 | | |
| | 2332 | 162 | | return _channels.IsBusy; |
| | | 163 | | } |
| | | 164 | | } |
| | | 165 | | |
| | 99 | 166 | | public bool IsSingleton { get; internal set; } |
| | | 167 | | |
| | | 168 | | internal ICollection<IChannel> OutgoingChannels |
| | | 169 | | { |
| | | 170 | | get |
| | | 171 | | { |
| | 79 | 172 | | ThrowIfClosed(); |
| | 79 | 173 | | return _channels.OutgoingChannels; |
| | | 174 | | } |
| | | 175 | | } |
| | | 176 | | |
| | | 177 | | public ServiceHostBase Host |
| | | 178 | | { |
| | | 179 | | get |
| | | 180 | | { |
| | 0 | 181 | | ThrowIfClosed(); |
| | 0 | 182 | | return _host; |
| | | 183 | | } |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | public int ManualFlowControlLimit |
| | | 187 | | { |
| | 0 | 188 | | get => EnsureQuotaThrottle().Limit; |
| | 0 | 189 | | set => EnsureQuotaThrottle().SetLimit(value); |
| | | 190 | | } |
| | | 191 | | |
| | 2536 | 192 | | internal QuotaThrottle QuotaThrottle { get; private set; } |
| | | 193 | | |
| | | 194 | | internal ServiceThrottle ServiceThrottle |
| | | 195 | | { |
| | 0 | 196 | | get { return _serviceThrottle; } |
| | | 197 | | set |
| | | 198 | | { |
| | 2391 | 199 | | ThrowIfDisposed(); |
| | 2391 | 200 | | _serviceThrottle = value; |
| | 2391 | 201 | | } |
| | | 202 | | } |
| | | 203 | | |
| | 9891 | 204 | | internal int InstanceContextManagerIndex { get; set; } |
| | | 205 | | |
| | | 206 | | public SynchronizationContext SynchronizationContext |
| | | 207 | | { |
| | 2536 | 208 | | get { return _synchronizationContext; } |
| | | 209 | | set |
| | | 210 | | { |
| | 0 | 211 | | ThrowIfClosedOrOpened(); |
| | 0 | 212 | | _synchronizationContext = value; |
| | 0 | 213 | | } |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | internal new object ThisLock |
| | | 217 | | { |
| | 21491 | 218 | | get { return base.ThisLock; } |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | protected override void OnAbort() |
| | | 222 | | { |
| | 58 | 223 | | _channels.Abort(); |
| | 58 | 224 | | Unload(); |
| | 58 | 225 | | } |
| | | 226 | | |
| | | 227 | | internal Task CloseInputAsync(CancellationToken token) |
| | | 228 | | { |
| | 0 | 229 | | return _channels.CloseInputAsync(token); |
| | | 230 | | } |
| | | 231 | | |
| | | 232 | | internal void BindRpc(MessageRpc rpc) |
| | | 233 | | { |
| | 2536 | 234 | | ThrowIfClosed(); |
| | 2536 | 235 | | _channels.IncrementActivityCount(); |
| | 2536 | 236 | | rpc.SuccessfullyBoundInstance = true; |
| | 2536 | 237 | | } |
| | | 238 | | |
| | | 239 | | internal void BindIncomingChannel(ServiceChannel channel) |
| | | 240 | | { |
| | 90 | 241 | | ThrowIfDisposed(); |
| | | 242 | | |
| | 90 | 243 | | channel.InstanceContext = this; |
| | 90 | 244 | | IChannel proxy = (IChannel)channel.Proxy; |
| | 90 | 245 | | _channels.AddIncomingChannel(proxy); |
| | | 246 | | |
| | | 247 | | // There's a race condition while on one thread we received a new request from underlying sessionful channel |
| | | 248 | | // and on another thread we just aborted the channel. So the channel will be added to the IncomingChannels l |
| | | 249 | | // ServiceChannelManager and never get a chance to be removed. |
| | 90 | 250 | | if (proxy != null) |
| | | 251 | | { |
| | 90 | 252 | | CommunicationState state = channel.State; |
| | 90 | 253 | | if (state == CommunicationState.Closing |
| | 90 | 254 | | || state == CommunicationState.Closed |
| | 90 | 255 | | || state == CommunicationState.Faulted) |
| | | 256 | | { |
| | 0 | 257 | | _channels.RemoveChannel(proxy); |
| | | 258 | | } |
| | | 259 | | } |
| | 90 | 260 | | } |
| | | 261 | | |
| | | 262 | | private void CloseIfNotBusy() |
| | | 263 | | { |
| | 2332 | 264 | | if (!(State != CommunicationState.Created && State != CommunicationState.Opening)) |
| | | 265 | | { |
| | | 266 | | Fx.Assert( |
| | | 267 | | "InstanceContext.CloseIfNotBusy: (this.State != CommunicationState.Created && this.State != Communic |
| | | 268 | | } |
| | | 269 | | |
| | 2332 | 270 | | if (State != CommunicationState.Opened) |
| | | 271 | | { |
| | 0 | 272 | | return; |
| | | 273 | | } |
| | | 274 | | |
| | 2332 | 275 | | if (IsBusy) |
| | | 276 | | { |
| | 0 | 277 | | return; |
| | | 278 | | } |
| | | 279 | | |
| | 2332 | 280 | | if (_behavior.CanUnload(this) == false) |
| | | 281 | | { |
| | 0 | 282 | | return; |
| | | 283 | | } |
| | | 284 | | |
| | | 285 | | try |
| | | 286 | | { |
| | | 287 | | // TODO: Make this call and it's chain async |
| | 2332 | 288 | | if (State == CommunicationState.Opened) |
| | | 289 | | { |
| | 2332 | 290 | | CloseAsync().GetAwaiter().GetResult(); |
| | | 291 | | } |
| | 2332 | 292 | | } |
| | | 293 | | catch (ObjectDisposedException e) |
| | | 294 | | { |
| | 0 | 295 | | DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); |
| | 0 | 296 | | } |
| | | 297 | | catch (InvalidOperationException e) |
| | | 298 | | { |
| | 0 | 299 | | DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); |
| | 0 | 300 | | } |
| | | 301 | | catch (CommunicationException e) |
| | | 302 | | { |
| | 0 | 303 | | DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); |
| | 0 | 304 | | } |
| | | 305 | | catch (TimeoutException e) |
| | | 306 | | { |
| | | 307 | | //if (TD.CloseTimeoutIsEnabled()) |
| | | 308 | | //{ |
| | | 309 | | // TD.CloseTimeout(e.Message); |
| | | 310 | | //} |
| | 0 | 311 | | DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); |
| | 0 | 312 | | } |
| | 2332 | 313 | | } |
| | | 314 | | |
| | | 315 | | private QuotaThrottle EnsureQuotaThrottle() |
| | | 316 | | { |
| | 0 | 317 | | lock (ThisLock) |
| | | 318 | | { |
| | 0 | 319 | | if (QuotaThrottle == null) |
| | | 320 | | { |
| | 0 | 321 | | QuotaThrottle = new QuotaThrottle(ThisLock) |
| | 0 | 322 | | { |
| | 0 | 323 | | Owner = "InstanceContext" |
| | 0 | 324 | | }; |
| | | 325 | | } |
| | 0 | 326 | | return QuotaThrottle; |
| | | 327 | | } |
| | 0 | 328 | | } |
| | | 329 | | |
| | | 330 | | internal void FaultInternal() |
| | | 331 | | { |
| | 0 | 332 | | Fault(); |
| | 0 | 333 | | } |
| | | 334 | | |
| | | 335 | | public object GetServiceInstance() |
| | | 336 | | { |
| | 55 | 337 | | return GetServiceInstance(null); |
| | | 338 | | } |
| | | 339 | | |
| | | 340 | | public object GetServiceInstance(Message message) |
| | | 341 | | { |
| | 2591 | 342 | | lock (_serviceInstanceLock) |
| | | 343 | | { |
| | 2591 | 344 | | ThrowIfClosedOrNotOpen(); |
| | | 345 | | |
| | 2591 | 346 | | object current = _userObject; |
| | | 347 | | |
| | 2591 | 348 | | if (current != null) |
| | | 349 | | { |
| | 200 | 350 | | return current; |
| | | 351 | | } |
| | | 352 | | |
| | 2391 | 353 | | if (_behavior == null) |
| | | 354 | | { |
| | 0 | 355 | | Exception error = new InvalidOperationException(SR.SFxInstanceNotInitialized); |
| | 0 | 356 | | if (message != null) |
| | | 357 | | { |
| | 0 | 358 | | throw TraceUtility.ThrowHelperError(error, message); |
| | | 359 | | } |
| | 0 | 360 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(error); |
| | | 361 | | } |
| | | 362 | | |
| | | 363 | | object newUserObject; |
| | 2391 | 364 | | if (message != null) |
| | | 365 | | { |
| | 2391 | 366 | | newUserObject = _behavior.GetInstance(this, message); |
| | | 367 | | } |
| | | 368 | | else |
| | | 369 | | { |
| | 0 | 370 | | newUserObject = _behavior.GetInstance(this); |
| | | 371 | | } |
| | 2390 | 372 | | if (newUserObject != null) |
| | | 373 | | { |
| | 2390 | 374 | | SetUserObject(newUserObject); |
| | | 375 | | } |
| | | 376 | | |
| | 2390 | 377 | | return newUserObject; |
| | | 378 | | } |
| | 2590 | 379 | | } |
| | | 380 | | |
| | | 381 | | private void Load() |
| | | 382 | | { |
| | 2473 | 383 | | if (_behavior != null) |
| | | 384 | | { |
| | 2473 | 385 | | _behavior.Initialize(this); |
| | | 386 | | } |
| | | 387 | | |
| | | 388 | | // TODO: Re-home InstanceContextManager and have a reference to it here. |
| | 2473 | 389 | | if (_host != null) |
| | | 390 | | { |
| | 2463 | 391 | | _host.BindInstance(this); |
| | | 392 | | } |
| | 2473 | 393 | | } |
| | | 394 | | |
| | | 395 | | private static void NotifyEmpty(InstanceContext instanceContext) |
| | | 396 | | { |
| | 2332 | 397 | | if (instanceContext.AutoClose) |
| | | 398 | | { |
| | 2332 | 399 | | instanceContext.CloseIfNotBusy(); |
| | | 400 | | } |
| | 2332 | 401 | | } |
| | | 402 | | |
| | | 403 | | private static void NotifyIdle(InstanceContext instanceContext) |
| | | 404 | | { |
| | 0 | 405 | | instanceContext.CloseIfNotBusy(); |
| | 0 | 406 | | } |
| | | 407 | | |
| | | 408 | | protected override async Task OnCloseAsync(CancellationToken token) |
| | | 409 | | { |
| | 2332 | 410 | | await _channels.CloseAsync(token); |
| | 2332 | 411 | | Unload(); |
| | 2332 | 412 | | } |
| | | 413 | | |
| | | 414 | | protected override void OnClosed() |
| | | 415 | | { |
| | 2390 | 416 | | base.OnClosed(); |
| | 2390 | 417 | | _serviceThrottle?.DeactivateInstanceContext(); |
| | 0 | 418 | | } |
| | | 419 | | |
| | | 420 | | protected override void OnFaulted() |
| | | 421 | | { |
| | 0 | 422 | | base.OnFaulted(); |
| | | 423 | | |
| | 0 | 424 | | if (IsSingleton && (_host != null)) |
| | | 425 | | { |
| | | 426 | | // TODO: Create new mechanism to register fault event handlers |
| | | 427 | | //_host.FaultInternal(); |
| | | 428 | | } |
| | 0 | 429 | | } |
| | | 430 | | |
| | | 431 | | protected override Task OnOpenAsync(CancellationToken token) |
| | | 432 | | { |
| | 2473 | 433 | | return Task.CompletedTask; |
| | | 434 | | } |
| | | 435 | | |
| | | 436 | | protected override void OnOpened() |
| | | 437 | | { |
| | 2473 | 438 | | base.OnOpened(); |
| | 2473 | 439 | | } |
| | | 440 | | |
| | | 441 | | protected override void OnOpening() |
| | | 442 | | { |
| | 2473 | 443 | | Load(); |
| | 2473 | 444 | | base.OnOpening(); |
| | 2473 | 445 | | } |
| | | 446 | | |
| | | 447 | | |
| | | 448 | | internal void ReleaseServiceInstance() |
| | | 449 | | { |
| | 0 | 450 | | ThrowIfDisposedOrNotOpen(); |
| | 0 | 451 | | SetUserObject(null); |
| | 0 | 452 | | } |
| | | 453 | | |
| | | 454 | | private void SetUserObject(object newUserObject) |
| | | 455 | | { |
| | 4780 | 456 | | if (_behavior != null && !IsWellKnown) |
| | | 457 | | { |
| | 4780 | 458 | | object oldUserObject = Interlocked.Exchange(ref _userObject, newUserObject); |
| | | 459 | | |
| | 4780 | 460 | | if ((oldUserObject != null) && (_host != null) && !Equals(oldUserObject, _host.DisposableInstance)) |
| | | 461 | | { |
| | 2389 | 462 | | _behavior.ReleaseInstance(this, oldUserObject); |
| | | 463 | | } |
| | | 464 | | } |
| | 4780 | 465 | | } |
| | | 466 | | |
| | | 467 | | internal void UnbindRpc(MessageRpc rpc) |
| | | 468 | | { |
| | 2536 | 469 | | if (rpc.InstanceContext == this && rpc.SuccessfullyBoundInstance) |
| | | 470 | | { |
| | 2536 | 471 | | _channels.DecrementActivityCount(); |
| | | 472 | | } |
| | 2536 | 473 | | } |
| | | 474 | | |
| | | 475 | | private void Unload() |
| | | 476 | | { |
| | 2390 | 477 | | SetUserObject(null); |
| | | 478 | | |
| | | 479 | | // TODO: Re-home InstanceContextManager and have a reference to it here. |
| | 2390 | 480 | | if (_host != null) |
| | | 481 | | { |
| | 2390 | 482 | | _host.UnbindInstance(this); |
| | | 483 | | } |
| | 2390 | 484 | | } |
| | | 485 | | } |
| | | 486 | | } |