| | | 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.ObjectModel; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using CoreWCF.Channels; |
| | | 9 | | using CoreWCF.Collections.Generic; |
| | | 10 | | using CoreWCF.Description; |
| | | 11 | | using CoreWCF.Diagnostics; |
| | | 12 | | using CoreWCF.IdentityModel.Policy; |
| | | 13 | | using CoreWCF.Runtime; |
| | | 14 | | using Microsoft.AspNetCore.Authorization; |
| | | 15 | | |
| | | 16 | | namespace CoreWCF.Dispatcher |
| | | 17 | | { |
| | | 18 | | public sealed class DispatchRuntime |
| | | 19 | | { |
| | | 20 | | private ServiceAuthenticationManager _serviceAuthenticationManager; |
| | | 21 | | private ServiceAuthorizationManager _serviceAuthorizationManager; |
| | | 22 | | private ReadOnlyCollection<IAuthorizationPolicy> _externalAuthorizationPolicies; |
| | | 23 | | private IAuthorizationService _authorizationService; |
| | | 24 | | |
| | | 25 | | //AuditLogLocation securityAuditLogLocation; |
| | | 26 | | private ConcurrencyMode _concurrencyMode; |
| | | 27 | | private bool _ensureOrderedDispatch; |
| | | 28 | | |
| | | 29 | | //bool suppressAuditFailure; |
| | | 30 | | //AuditLevel serviceAuthorizationAuditLevel; |
| | | 31 | | //AuditLevel messageAuthenticationAuditLevel; |
| | | 32 | | private bool _automaticInputSessionShutdown; |
| | | 33 | | private readonly ChannelDispatcher _channelDispatcher; |
| | | 34 | | private IInstanceProvider _instanceProvider; |
| | | 35 | | private IInstanceContextProvider _instanceContextProvider; |
| | | 36 | | private InstanceContext _singleton; |
| | | 37 | | private bool _ignoreTransactionMessageProperty; |
| | | 38 | | private readonly OperationCollection _operations; |
| | | 39 | | private IDispatchOperationSelector _operationSelector; |
| | | 40 | | private ImmutableDispatchRuntime _runtime; |
| | | 41 | | private bool _isExternalPoliciesSet; |
| | | 42 | | private bool _isAuthorizationManagerSet; |
| | | 43 | | private SynchronizationContext _synchronizationContext; |
| | | 44 | | private PrincipalPermissionMode _principalPermissionMode; |
| | | 45 | | |
| | | 46 | | //object roleProvider; |
| | | 47 | | private Type _type; |
| | | 48 | | private DispatchOperation _unhandled; |
| | | 49 | | private bool _impersonateCallerForAllOperations; |
| | | 50 | | private bool _impersonateOnSerializingReply; |
| | | 51 | | private readonly SharedRuntimeState _shared; |
| | | 52 | | private bool _requireClaimsPrincipalOnOperationContext; |
| | | 53 | | private bool _supportsAuthorizationData; |
| | | 54 | | |
| | | 55 | | internal DispatchRuntime(EndpointDispatcher endpointDispatcher) |
| | 664 | 56 | | : this(new SharedRuntimeState(true)) |
| | | 57 | | { |
| | 664 | 58 | | EndpointDispatcher = endpointDispatcher ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( |
| | | 59 | | Fx.Assert(_shared.IsOnServer, "Server constructor called on client?"); |
| | 664 | 60 | | } |
| | | 61 | | |
| | | 62 | | internal DispatchRuntime(ClientRuntime proxyRuntime, SharedRuntimeState shared) |
| | 0 | 63 | | : this(shared) |
| | | 64 | | { |
| | 0 | 65 | | ClientRuntime = proxyRuntime ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(prox |
| | 0 | 66 | | _instanceProvider = new CallbackInstanceProvider(); |
| | 0 | 67 | | _channelDispatcher = new ChannelDispatcher(shared); |
| | 0 | 68 | | _instanceContextProvider = InstanceContextProviderBase.GetProviderForMode(InstanceContextMode.PerSession, th |
| | | 69 | | |
| | | 70 | | Fx.Assert(!shared.IsOnServer, "Client constructor called on server?"); |
| | 0 | 71 | | } |
| | | 72 | | |
| | 664 | 73 | | private DispatchRuntime(SharedRuntimeState shared) |
| | | 74 | | { |
| | 664 | 75 | | _shared = shared; |
| | | 76 | | |
| | 664 | 77 | | _operations = new OperationCollection(this); |
| | | 78 | | |
| | 664 | 79 | | InputSessionShutdownHandlers = NewBehaviorCollection<IInputSessionShutdown>(); |
| | 664 | 80 | | MessageInspectors = NewBehaviorCollection<IDispatchMessageInspector>(); |
| | 664 | 81 | | InstanceContextInitializers = NewBehaviorCollection<IInstanceContextInitializer>(); |
| | 664 | 82 | | _synchronizationContext = ThreadBehavior.GetCurrentSynchronizationContext(); |
| | 664 | 83 | | _automaticInputSessionShutdown = true; |
| | 664 | 84 | | _principalPermissionMode = ServiceAuthorizationBehavior.DefaultPrincipalPermissionMode; |
| | 664 | 85 | | _unhandled = new DispatchOperation(this, "*", MessageHeaders.WildcardAction, MessageHeaders.WildcardAction) |
| | 664 | 86 | | { |
| | 664 | 87 | | InternalFormatter = MessageOperationFormatter.Instance, |
| | 664 | 88 | | InternalInvoker = new UnhandledActionInvoker(this) |
| | 664 | 89 | | }; |
| | 664 | 90 | | } |
| | | 91 | | |
| | | 92 | | public IInstanceContextProvider InstanceContextProvider |
| | | 93 | | { |
| | | 94 | | get |
| | | 95 | | { |
| | 6379 | 96 | | return _instanceContextProvider; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | set |
| | | 100 | | { |
| | 664 | 101 | | if (value == null) |
| | | 102 | | { |
| | 0 | 103 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value)); |
| | | 104 | | } |
| | | 105 | | |
| | 664 | 106 | | lock (ThisLock) |
| | | 107 | | { |
| | 664 | 108 | | InvalidateRuntime(); |
| | 664 | 109 | | _instanceContextProvider = value; |
| | 664 | 110 | | } |
| | 664 | 111 | | } |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | public InstanceContext SingletonInstanceContext |
| | | 115 | | { |
| | 845 | 116 | | get { return _singleton; } |
| | | 117 | | set |
| | | 118 | | { |
| | 125 | 119 | | if (value == null) |
| | | 120 | | { |
| | 0 | 121 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value)); |
| | | 122 | | } |
| | | 123 | | |
| | 125 | 124 | | lock (ThisLock) |
| | | 125 | | { |
| | 125 | 126 | | InvalidateRuntime(); |
| | 125 | 127 | | _singleton = value; |
| | 125 | 128 | | } |
| | 125 | 129 | | } |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | public ConcurrencyMode ConcurrencyMode |
| | | 133 | | { |
| | | 134 | | get |
| | | 135 | | { |
| | 2981 | 136 | | return _concurrencyMode; |
| | | 137 | | } |
| | | 138 | | set |
| | | 139 | | { |
| | 664 | 140 | | lock (ThisLock) |
| | | 141 | | { |
| | 664 | 142 | | InvalidateRuntime(); |
| | 664 | 143 | | _concurrencyMode = value; |
| | 664 | 144 | | } |
| | 664 | 145 | | } |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | public bool EnsureOrderedDispatch |
| | | 149 | | { |
| | | 150 | | get |
| | | 151 | | { |
| | 2980 | 152 | | return _ensureOrderedDispatch; |
| | | 153 | | } |
| | | 154 | | set |
| | | 155 | | { |
| | 641 | 156 | | lock (ThisLock) |
| | | 157 | | { |
| | 641 | 158 | | InvalidateRuntime(); |
| | 641 | 159 | | _ensureOrderedDispatch = value; |
| | 641 | 160 | | } |
| | 641 | 161 | | } |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | internal ReadOnlyCollection<IAuthorizationPolicy> ExternalAuthorizationPolicies |
| | | 165 | | { |
| | | 166 | | get |
| | | 167 | | { |
| | 133 | 168 | | return _externalAuthorizationPolicies; |
| | | 169 | | } |
| | | 170 | | set |
| | | 171 | | { |
| | 26 | 172 | | lock (ThisLock) |
| | | 173 | | { |
| | 26 | 174 | | InvalidateRuntime(); |
| | 26 | 175 | | _externalAuthorizationPolicies = value; |
| | 26 | 176 | | _isExternalPoliciesSet = true; |
| | 26 | 177 | | } |
| | 26 | 178 | | } |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | public ServiceAuthenticationManager ServiceAuthenticationManager |
| | | 182 | | { |
| | | 183 | | get |
| | | 184 | | { |
| | 0 | 185 | | return _serviceAuthenticationManager; |
| | | 186 | | } |
| | | 187 | | set |
| | | 188 | | { |
| | 0 | 189 | | lock (ThisLock) |
| | | 190 | | { |
| | 0 | 191 | | InvalidateRuntime(); |
| | 0 | 192 | | _serviceAuthenticationManager = value; |
| | 0 | 193 | | RequiresAuthentication = true; |
| | 0 | 194 | | } |
| | 0 | 195 | | } |
| | | 196 | | } |
| | | 197 | | |
| | | 198 | | public ServiceAuthorizationManager ServiceAuthorizationManager |
| | | 199 | | { |
| | | 200 | | get |
| | | 201 | | { |
| | 133 | 202 | | return _serviceAuthorizationManager; |
| | | 203 | | } |
| | | 204 | | set |
| | | 205 | | { |
| | 5 | 206 | | lock (ThisLock) |
| | | 207 | | { |
| | 5 | 208 | | InvalidateRuntime(); |
| | 5 | 209 | | _serviceAuthorizationManager = value; |
| | 5 | 210 | | _isAuthorizationManagerSet = true; |
| | 5 | 211 | | } |
| | 5 | 212 | | } |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | [Obsolete("DispatchRuntime.AuthorizationService will be made internal in next major release.")] |
| | | 216 | | public IAuthorizationService AuthorizationService |
| | | 217 | | { |
| | 0 | 218 | | get => throw new NotSupportedException(); |
| | 0 | 219 | | set => throw new NotSupportedException(); |
| | | 220 | | } |
| | | 221 | | |
| | | 222 | | // TODO: Make AuthorizationService property internal and replace get/set implementations with the 2 methods belo |
| | 133 | 223 | | internal IAuthorizationService GetAuthorizationService() => _authorizationService; |
| | | 224 | | |
| | | 225 | | internal void SetAuthorizationService(IAuthorizationService authorizationService) |
| | | 226 | | { |
| | 110 | 227 | | lock (ThisLock) |
| | | 228 | | { |
| | 110 | 229 | | InvalidateRuntime(); |
| | 110 | 230 | | _authorizationService = authorizationService; |
| | 110 | 231 | | } |
| | 110 | 232 | | } |
| | | 233 | | |
| | | 234 | | public bool AutomaticInputSessionShutdown |
| | | 235 | | { |
| | 516 | 236 | | get { return _automaticInputSessionShutdown; } |
| | | 237 | | set |
| | | 238 | | { |
| | 641 | 239 | | lock (ThisLock) |
| | | 240 | | { |
| | 641 | 241 | | InvalidateRuntime(); |
| | 641 | 242 | | _automaticInputSessionShutdown = value; |
| | 641 | 243 | | } |
| | 641 | 244 | | } |
| | | 245 | | } |
| | | 246 | | |
| | | 247 | | public ChannelDispatcher ChannelDispatcher |
| | | 248 | | { |
| | 2058 | 249 | | get { return _channelDispatcher ?? EndpointDispatcher.ChannelDispatcher; } |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | public ClientRuntime CallbackClientRuntime |
| | | 253 | | { |
| | | 254 | | get |
| | | 255 | | { |
| | 524 | 256 | | if (ClientRuntime == null) |
| | | 257 | | { |
| | 505 | 258 | | lock (ThisLock) |
| | | 259 | | { |
| | 505 | 260 | | if (ClientRuntime == null) |
| | | 261 | | { |
| | 505 | 262 | | ClientRuntime = new ClientRuntime(this, _shared); |
| | | 263 | | } |
| | 505 | 264 | | } |
| | | 265 | | } |
| | | 266 | | |
| | 524 | 267 | | return ClientRuntime; |
| | | 268 | | } |
| | | 269 | | } |
| | | 270 | | |
| | 3068 | 271 | | public EndpointDispatcher EndpointDispatcher { get; } |
| | | 272 | | |
| | | 273 | | public bool ImpersonateCallerForAllOperations |
| | | 274 | | { |
| | | 275 | | get |
| | | 276 | | { |
| | 657 | 277 | | return _impersonateCallerForAllOperations; |
| | | 278 | | } |
| | | 279 | | set |
| | | 280 | | { |
| | 641 | 281 | | lock (ThisLock) |
| | | 282 | | { |
| | 641 | 283 | | InvalidateRuntime(); |
| | 641 | 284 | | _impersonateCallerForAllOperations = value; |
| | 641 | 285 | | } |
| | 641 | 286 | | } |
| | | 287 | | } |
| | | 288 | | |
| | | 289 | | public bool ImpersonateOnSerializingReply |
| | | 290 | | { |
| | | 291 | | get |
| | | 292 | | { |
| | 661 | 293 | | return _impersonateOnSerializingReply; |
| | | 294 | | } |
| | | 295 | | set |
| | | 296 | | { |
| | 641 | 297 | | lock (ThisLock) |
| | | 298 | | { |
| | 641 | 299 | | InvalidateRuntime(); |
| | 641 | 300 | | _impersonateOnSerializingReply = value; |
| | 641 | 301 | | } |
| | 641 | 302 | | } |
| | | 303 | | } |
| | | 304 | | |
| | | 305 | | internal bool RequireClaimsPrincipalOnOperationContext |
| | | 306 | | { |
| | | 307 | | get |
| | | 308 | | { |
| | 661 | 309 | | return _requireClaimsPrincipalOnOperationContext; |
| | | 310 | | } |
| | | 311 | | set |
| | | 312 | | { |
| | 481 | 313 | | lock (ThisLock) |
| | | 314 | | { |
| | 481 | 315 | | InvalidateRuntime(); |
| | 481 | 316 | | _requireClaimsPrincipalOnOperationContext = value; |
| | 481 | 317 | | } |
| | 481 | 318 | | } |
| | | 319 | | } |
| | | 320 | | |
| | | 321 | | internal bool SupportsAuthorizationData |
| | | 322 | | { |
| | | 323 | | get |
| | | 324 | | { |
| | 1988 | 325 | | return _supportsAuthorizationData; |
| | | 326 | | } |
| | | 327 | | set |
| | | 328 | | { |
| | 427 | 329 | | lock (ThisLock) |
| | | 330 | | { |
| | 427 | 331 | | InvalidateRuntime(); |
| | 427 | 332 | | _supportsAuthorizationData = value; |
| | 427 | 333 | | } |
| | 427 | 334 | | } |
| | | 335 | | } |
| | | 336 | | |
| | 661 | 337 | | internal SynchronizedCollection<IInputSessionShutdown> InputSessionShutdownHandlers { get; } |
| | | 338 | | |
| | | 339 | | public bool IgnoreTransactionMessageProperty |
| | | 340 | | { |
| | 0 | 341 | | get { return _ignoreTransactionMessageProperty; } |
| | | 342 | | set |
| | | 343 | | { |
| | 0 | 344 | | lock (ThisLock) |
| | | 345 | | { |
| | 0 | 346 | | InvalidateRuntime(); |
| | 0 | 347 | | _ignoreTransactionMessageProperty = value; |
| | 0 | 348 | | } |
| | 0 | 349 | | } |
| | | 350 | | } |
| | | 351 | | |
| | | 352 | | public IInstanceProvider InstanceProvider |
| | | 353 | | { |
| | 4976 | 354 | | get { return _instanceProvider; } |
| | | 355 | | set |
| | | 356 | | { |
| | 541 | 357 | | lock (ThisLock) |
| | | 358 | | { |
| | 541 | 359 | | InvalidateRuntime(); |
| | 541 | 360 | | _instanceProvider = value; |
| | 541 | 361 | | } |
| | 541 | 362 | | } |
| | | 363 | | } |
| | | 364 | | |
| | 1325 | 365 | | public SynchronizedCollection<IDispatchMessageInspector> MessageInspectors { get; } |
| | | 366 | | |
| | | 367 | | public SynchronizedKeyedCollection<string, DispatchOperation> Operations |
| | | 368 | | { |
| | 22789 | 369 | | get { return _operations; } |
| | | 370 | | } |
| | | 371 | | |
| | | 372 | | public IDispatchOperationSelector OperationSelector |
| | | 373 | | { |
| | 699 | 374 | | get { return _operationSelector; } |
| | | 375 | | set |
| | | 376 | | { |
| | 38 | 377 | | lock (ThisLock) |
| | | 378 | | { |
| | 38 | 379 | | InvalidateRuntime(); |
| | 38 | 380 | | _operationSelector = value; |
| | 38 | 381 | | } |
| | 38 | 382 | | } |
| | | 383 | | } |
| | | 384 | | |
| | 661 | 385 | | internal SynchronizedCollection<IInstanceContextInitializer> InstanceContextInitializers { get; } |
| | | 386 | | |
| | | 387 | | public SynchronizationContext SynchronizationContext |
| | | 388 | | { |
| | 661 | 389 | | get { return _synchronizationContext; } |
| | | 390 | | set |
| | | 391 | | { |
| | 27 | 392 | | lock (ThisLock) |
| | | 393 | | { |
| | 27 | 394 | | InvalidateRuntime(); |
| | 27 | 395 | | _synchronizationContext = value; |
| | 27 | 396 | | } |
| | 27 | 397 | | } |
| | | 398 | | } |
| | | 399 | | |
| | | 400 | | public PrincipalPermissionMode PrincipalPermissionMode |
| | | 401 | | { |
| | | 402 | | get |
| | | 403 | | { |
| | 1295 | 404 | | return _principalPermissionMode; |
| | | 405 | | } |
| | | 406 | | set |
| | | 407 | | { |
| | 664 | 408 | | if (!PrincipalPermissionModeHelper.IsDefined(value)) |
| | | 409 | | { |
| | 0 | 410 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 411 | | } |
| | | 412 | | |
| | 664 | 413 | | lock (ThisLock) |
| | | 414 | | { |
| | 664 | 415 | | InvalidateRuntime(); |
| | 664 | 416 | | _principalPermissionMode = value; |
| | 664 | 417 | | } |
| | 664 | 418 | | } |
| | | 419 | | } |
| | | 420 | | |
| | | 421 | | //public RoleProvider RoleProvider |
| | | 422 | | //{ |
| | | 423 | | // get { return (RoleProvider)this.roleProvider; } |
| | | 424 | | // set |
| | | 425 | | // { |
| | | 426 | | // lock (this.ThisLock) |
| | | 427 | | // { |
| | | 428 | | // this.InvalidateRuntime(); |
| | | 429 | | // this.roleProvider = value; |
| | | 430 | | // } |
| | | 431 | | // } |
| | | 432 | | //} |
| | | 433 | | |
| | | 434 | | //public bool TransactionAutoCompleteOnSessionClose |
| | | 435 | | //{ |
| | | 436 | | // get { return this.transactionAutoCompleteOnSessionClose; } |
| | | 437 | | // set |
| | | 438 | | // { |
| | | 439 | | // lock (this.ThisLock) |
| | | 440 | | // { |
| | | 441 | | // this.InvalidateRuntime(); |
| | | 442 | | // this.transactionAutoCompleteOnSessionClose = value; |
| | | 443 | | // } |
| | | 444 | | // } |
| | | 445 | | //} |
| | | 446 | | |
| | | 447 | | public Type Type |
| | | 448 | | { |
| | 1880 | 449 | | get { return _type; } |
| | | 450 | | set |
| | | 451 | | { |
| | 641 | 452 | | lock (ThisLock) |
| | | 453 | | { |
| | 641 | 454 | | InvalidateRuntime(); |
| | 641 | 455 | | _type = value; |
| | 641 | 456 | | } |
| | 641 | 457 | | } |
| | | 458 | | } |
| | | 459 | | |
| | | 460 | | public DispatchOperation UnhandledDispatchOperation |
| | | 461 | | { |
| | 802 | 462 | | get { return _unhandled; } |
| | | 463 | | set |
| | | 464 | | { |
| | 70 | 465 | | if (value == null) |
| | | 466 | | { |
| | 0 | 467 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value)); |
| | | 468 | | } |
| | | 469 | | |
| | 70 | 470 | | lock (ThisLock) |
| | | 471 | | { |
| | 70 | 472 | | InvalidateRuntime(); |
| | 70 | 473 | | _unhandled = value; |
| | 70 | 474 | | } |
| | 70 | 475 | | } |
| | | 476 | | } |
| | | 477 | | |
| | | 478 | | public bool ValidateMustUnderstand |
| | | 479 | | { |
| | 661 | 480 | | get { return _shared.ValidateMustUnderstand; } |
| | | 481 | | set |
| | | 482 | | { |
| | 641 | 483 | | lock (ThisLock) |
| | | 484 | | { |
| | 641 | 485 | | InvalidateRuntime(); |
| | 641 | 486 | | _shared.ValidateMustUnderstand = value; |
| | 641 | 487 | | } |
| | 641 | 488 | | } |
| | | 489 | | } |
| | | 490 | | |
| | 664 | 491 | | internal bool RequiresAuthentication { get; private set; } |
| | | 492 | | |
| | | 493 | | internal bool RequiresAuthorization |
| | 663 | 494 | | => _isAuthorizationManagerSet || _isExternalPoliciesSet; |
| | | 495 | | |
| | | 496 | | internal bool HasMatchAllOperation |
| | | 497 | | { |
| | | 498 | | get |
| | | 499 | | { |
| | 10 | 500 | | lock (ThisLock) |
| | | 501 | | { |
| | 10 | 502 | | return !(_unhandled.Invoker is UnhandledActionInvoker); |
| | | 503 | | } |
| | 10 | 504 | | } |
| | | 505 | | } |
| | | 506 | | |
| | | 507 | | internal bool EnableFaults |
| | | 508 | | { |
| | | 509 | | get |
| | | 510 | | { |
| | 661 | 511 | | if (IsOnServer) |
| | | 512 | | { |
| | 661 | 513 | | ChannelDispatcher channelDispatcher = ChannelDispatcher; |
| | 661 | 514 | | return (channelDispatcher != null) && channelDispatcher.EnableFaults; |
| | | 515 | | } |
| | | 516 | | else |
| | | 517 | | { |
| | 0 | 518 | | return _shared.EnableFaults; |
| | | 519 | | } |
| | | 520 | | } |
| | | 521 | | } |
| | | 522 | | |
| | | 523 | | internal bool IsOnServer |
| | | 524 | | { |
| | 1984 | 525 | | get { return _shared.IsOnServer; } |
| | | 526 | | } |
| | | 527 | | |
| | | 528 | | internal bool ManualAddressing |
| | | 529 | | { |
| | | 530 | | get |
| | | 531 | | { |
| | 662 | 532 | | if (IsOnServer) |
| | | 533 | | { |
| | 662 | 534 | | ChannelDispatcher channelDispatcher = ChannelDispatcher; |
| | 662 | 535 | | return (channelDispatcher != null) && channelDispatcher.ManualAddressing; |
| | | 536 | | } |
| | | 537 | | else |
| | | 538 | | { |
| | 0 | 539 | | return _shared.ManualAddressing; |
| | | 540 | | } |
| | | 541 | | } |
| | | 542 | | } |
| | | 543 | | |
| | | 544 | | internal int MaxCallContextInitializers |
| | | 545 | | { |
| | | 546 | | get |
| | | 547 | | { |
| | 661 | 548 | | lock (ThisLock) |
| | | 549 | | { |
| | 661 | 550 | | int max = 0; |
| | | 551 | | |
| | 7348 | 552 | | for (int i = 0; i < _operations.Count; i++) |
| | | 553 | | { |
| | 3013 | 554 | | max = Math.Max(max, _operations[i].CallContextInitializers.Count); |
| | | 555 | | } |
| | 661 | 556 | | max = Math.Max(max, _unhandled.CallContextInitializers.Count); |
| | 661 | 557 | | return max; |
| | | 558 | | } |
| | 661 | 559 | | } |
| | | 560 | | } |
| | | 561 | | |
| | | 562 | | internal int MaxParameterInspectors |
| | | 563 | | { |
| | | 564 | | get |
| | | 565 | | { |
| | 661 | 566 | | lock (ThisLock) |
| | | 567 | | { |
| | 661 | 568 | | int max = 0; |
| | | 569 | | |
| | 7348 | 570 | | for (int i = 0; i < _operations.Count; i++) |
| | | 571 | | { |
| | 3013 | 572 | | max = Math.Max(max, _operations[i].ParameterInspectors.Count); |
| | | 573 | | } |
| | 661 | 574 | | max = Math.Max(max, _unhandled.ParameterInspectors.Count); |
| | 661 | 575 | | return max; |
| | | 576 | | } |
| | 661 | 577 | | } |
| | | 578 | | } |
| | | 579 | | |
| | | 580 | | // Internal access to CallbackClientRuntime, but this one doesn't create on demand |
| | 2701 | 581 | | internal ClientRuntime ClientRuntime { get; private set; } |
| | | 582 | | |
| | | 583 | | internal object ThisLock |
| | | 584 | | { |
| | 96141 | 585 | | get { return _shared; } |
| | | 586 | | } |
| | | 587 | | |
| | | 588 | | //internal bool IsRoleProviderSet |
| | | 589 | | //{ |
| | | 590 | | // get { return this.roleProvider != null; } |
| | | 591 | | //} |
| | | 592 | | |
| | | 593 | | internal DispatchOperationRuntime GetOperation(ref Message message) |
| | | 594 | | { |
| | 2539 | 595 | | ImmutableDispatchRuntime runtime = GetRuntime(); |
| | 2539 | 596 | | return runtime.GetOperation(ref message); |
| | | 597 | | } |
| | | 598 | | |
| | | 599 | | internal ImmutableDispatchRuntime GetRuntime() |
| | | 600 | | { |
| | 3281 | 601 | | ImmutableDispatchRuntime runtime = _runtime; |
| | 3281 | 602 | | if (runtime != null) |
| | | 603 | | { |
| | 2617 | 604 | | return runtime; |
| | | 605 | | } |
| | | 606 | | else |
| | | 607 | | { |
| | 664 | 608 | | return GetRuntimeCore(); |
| | | 609 | | } |
| | | 610 | | } |
| | | 611 | | |
| | | 612 | | private ImmutableDispatchRuntime GetRuntimeCore() |
| | | 613 | | { |
| | 664 | 614 | | lock (ThisLock) |
| | | 615 | | { |
| | 664 | 616 | | if (_runtime == null) |
| | | 617 | | { |
| | 664 | 618 | | _runtime = new ImmutableDispatchRuntime(this); |
| | | 619 | | } |
| | | 620 | | |
| | 661 | 621 | | return _runtime; |
| | | 622 | | } |
| | 661 | 623 | | } |
| | | 624 | | |
| | 111 | 625 | | internal bool IsAuthorizationInfrastructureRegistered() => _authorizationService != null; |
| | | 626 | | |
| | | 627 | | internal void InvalidateRuntime() |
| | | 628 | | { |
| | 41459 | 629 | | lock (ThisLock) |
| | | 630 | | { |
| | 41459 | 631 | | _shared.ThrowIfImmutable(); |
| | 41459 | 632 | | _runtime = null; |
| | 41459 | 633 | | } |
| | 41459 | 634 | | } |
| | | 635 | | |
| | | 636 | | internal void LockDownProperties() |
| | | 637 | | { |
| | 661 | 638 | | _shared.LockDownProperties(); |
| | 661 | 639 | | if (_concurrencyMode != ConcurrencyMode.Single && _ensureOrderedDispatch) |
| | | 640 | | { |
| | 0 | 641 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.SfxDispatchRu |
| | | 642 | | } |
| | 661 | 643 | | } |
| | | 644 | | |
| | | 645 | | internal SynchronizedCollection<T> NewBehaviorCollection<T>() |
| | | 646 | | { |
| | 13242 | 647 | | return new DispatchBehaviorCollection<T>(this); |
| | | 648 | | } |
| | | 649 | | |
| | | 650 | | internal class UnhandledActionInvoker : IOperationInvoker |
| | | 651 | | { |
| | | 652 | | private readonly DispatchRuntime _dispatchRuntime; |
| | | 653 | | |
| | 664 | 654 | | public UnhandledActionInvoker(DispatchRuntime dispatchRuntime) |
| | | 655 | | { |
| | 664 | 656 | | _dispatchRuntime = dispatchRuntime; |
| | 664 | 657 | | } |
| | | 658 | | |
| | | 659 | | public object[] AllocateInputs() |
| | | 660 | | { |
| | 0 | 661 | | return new object[1]; |
| | | 662 | | } |
| | | 663 | | |
| | | 664 | | public ValueTask<(object returnValue, object[] outputs)> InvokeAsync(object instance, object[] inputs) |
| | | 665 | | { |
| | 0 | 666 | | object[] outputs = EmptyArray<object>.Allocate(0); |
| | | 667 | | |
| | 0 | 668 | | if (!(inputs[0] is Message message)) |
| | | 669 | | { |
| | 0 | 670 | | return new ValueTask<(object returnValue, object[] outputs)>(((object)null, outputs)); |
| | | 671 | | } |
| | | 672 | | |
| | 0 | 673 | | string action = message.Headers.Action; |
| | | 674 | | |
| | | 675 | | //if (DiagnosticUtility.ShouldTraceInformation) |
| | | 676 | | //{ |
| | | 677 | | // TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.UnhandledAction, |
| | | 678 | | // SR.TraceCodeUnhandledAction, |
| | | 679 | | // new StringTraceRecord("Action", action), |
| | | 680 | | // this, null, message); |
| | | 681 | | //} |
| | | 682 | | |
| | 0 | 683 | | FaultCode code = FaultCode.CreateSenderFaultCode(AddressingStrings.ActionNotSupported, |
| | 0 | 684 | | message.Version.Addressing.Namespace); |
| | 0 | 685 | | string reasonText = SR.Format(SR.SFxNoEndpointMatchingContract, action); |
| | 0 | 686 | | FaultReason reason = new FaultReason(reasonText); |
| | | 687 | | |
| | 0 | 688 | | FaultException exception = new FaultException(reason, code); |
| | 0 | 689 | | ErrorBehavior.ThrowAndCatch(exception); |
| | | 690 | | |
| | 0 | 691 | | ServiceChannel serviceChannel = OperationContext.Current.InternalServiceChannel; |
| | 0 | 692 | | OperationContext.Current.OperationCompleted += |
| | 0 | 693 | | delegate (object sender, EventArgs e) |
| | 0 | 694 | | { |
| | 0 | 695 | | ChannelDispatcher channelDispatcher = _dispatchRuntime.ChannelDispatcher; |
| | 0 | 696 | | if (!channelDispatcher.HandleError(exception) && serviceChannel.HasSession) |
| | 0 | 697 | | { |
| | 0 | 698 | | try |
| | 0 | 699 | | { |
| | 0 | 700 | | var helper = new TimeoutHelper(ChannelHandler.CloseAfterFaultTimeout); |
| | 0 | 701 | | serviceChannel.CloseAsync(helper.GetCancellationToken()).GetAwaiter().GetResult(); |
| | 0 | 702 | | } |
| | 0 | 703 | | catch (Exception ex) |
| | 0 | 704 | | { |
| | 0 | 705 | | if (Fx.IsFatal(ex)) |
| | 0 | 706 | | { |
| | 0 | 707 | | throw; |
| | 0 | 708 | | } |
| | 0 | 709 | | channelDispatcher.HandleError(ex); |
| | 0 | 710 | | } |
| | 0 | 711 | | } |
| | 0 | 712 | | }; |
| | | 713 | | |
| | 0 | 714 | | if (_dispatchRuntime._shared.EnableFaults) |
| | | 715 | | { |
| | 0 | 716 | | MessageFault fault = MessageFault.CreateFault(code, reason, action); |
| | 0 | 717 | | return new ValueTask<(object returnValue, object[] outputs)>( |
| | 0 | 718 | | (Message.CreateMessage(message.Version, fault, message.Version.Addressing.DefaultFaultAction), |
| | 0 | 719 | | outputs)); |
| | | 720 | | } |
| | | 721 | | else |
| | | 722 | | { |
| | 0 | 723 | | OperationContext.Current.RequestContext.CloseAsync().GetAwaiter().GetResult(); |
| | 0 | 724 | | OperationContext.Current.RequestContext = null; |
| | 0 | 725 | | return new ValueTask<(object returnValue, object[] outputs)>((null, outputs)); |
| | | 726 | | } |
| | | 727 | | } |
| | | 728 | | |
| | | 729 | | |
| | | 730 | | |
| | | 731 | | public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state) |
| | | 732 | | { |
| | 0 | 733 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException()); |
| | | 734 | | } |
| | | 735 | | |
| | | 736 | | public object InvokeEnd(object instance, out object[] outputs, IAsyncResult result) |
| | | 737 | | { |
| | 0 | 738 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException()); |
| | | 739 | | } |
| | | 740 | | } |
| | | 741 | | |
| | | 742 | | private class DispatchBehaviorCollection<T> : SynchronizedCollection<T> |
| | | 743 | | { |
| | | 744 | | private readonly DispatchRuntime _outer; |
| | | 745 | | |
| | | 746 | | internal DispatchBehaviorCollection(DispatchRuntime outer) |
| | 13242 | 747 | | : base(outer.ThisLock) |
| | | 748 | | { |
| | 13242 | 749 | | _outer = outer; |
| | 13242 | 750 | | } |
| | | 751 | | |
| | | 752 | | protected override void ClearItems() |
| | | 753 | | { |
| | 0 | 754 | | _outer.InvalidateRuntime(); |
| | 0 | 755 | | base.ClearItems(); |
| | 0 | 756 | | } |
| | | 757 | | |
| | | 758 | | protected override void InsertItem(int index, T item) |
| | | 759 | | { |
| | 168 | 760 | | if (item == null) |
| | | 761 | | { |
| | 0 | 762 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(item)); |
| | | 763 | | } |
| | | 764 | | |
| | 168 | 765 | | _outer.InvalidateRuntime(); |
| | 168 | 766 | | base.InsertItem(index, item); |
| | 168 | 767 | | } |
| | | 768 | | |
| | | 769 | | protected override void RemoveItem(int index) |
| | | 770 | | { |
| | 0 | 771 | | _outer.InvalidateRuntime(); |
| | 0 | 772 | | base.RemoveItem(index); |
| | 0 | 773 | | } |
| | | 774 | | |
| | | 775 | | protected override void SetItem(int index, T item) |
| | | 776 | | { |
| | 0 | 777 | | if (item == null) |
| | | 778 | | { |
| | 0 | 779 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(item)); |
| | | 780 | | } |
| | | 781 | | |
| | 0 | 782 | | _outer.InvalidateRuntime(); |
| | 0 | 783 | | base.SetItem(index, item); |
| | 0 | 784 | | } |
| | | 785 | | } |
| | | 786 | | |
| | | 787 | | private class OperationCollection : SynchronizedKeyedCollection<string, DispatchOperation> |
| | | 788 | | { |
| | | 789 | | private readonly DispatchRuntime _outer; |
| | | 790 | | |
| | | 791 | | internal OperationCollection(DispatchRuntime outer) |
| | 664 | 792 | | : base(outer.ThisLock) |
| | | 793 | | { |
| | 664 | 794 | | _outer = outer; |
| | 664 | 795 | | } |
| | | 796 | | |
| | | 797 | | protected override void ClearItems() |
| | | 798 | | { |
| | 0 | 799 | | _outer.InvalidateRuntime(); |
| | 0 | 800 | | base.ClearItems(); |
| | 0 | 801 | | } |
| | | 802 | | |
| | | 803 | | protected override string GetKeyForItem(DispatchOperation item) |
| | | 804 | | { |
| | 42731 | 805 | | return item.Name; |
| | | 806 | | } |
| | | 807 | | |
| | | 808 | | protected override void InsertItem(int index, DispatchOperation item) |
| | | 809 | | { |
| | 3016 | 810 | | if (item == null) |
| | | 811 | | { |
| | 0 | 812 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(item)); |
| | | 813 | | } |
| | 3016 | 814 | | if (item.Parent != _outer) |
| | | 815 | | { |
| | 0 | 816 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.SFxMismatchedOperationParent); |
| | | 817 | | } |
| | | 818 | | |
| | 3016 | 819 | | _outer.InvalidateRuntime(); |
| | 3016 | 820 | | base.InsertItem(index, item); |
| | 3016 | 821 | | } |
| | | 822 | | |
| | | 823 | | protected override void RemoveItem(int index) |
| | | 824 | | { |
| | 0 | 825 | | _outer.InvalidateRuntime(); |
| | 0 | 826 | | base.RemoveItem(index); |
| | 0 | 827 | | } |
| | | 828 | | |
| | | 829 | | protected override void SetItem(int index, DispatchOperation item) |
| | | 830 | | { |
| | 0 | 831 | | if (item == null) |
| | | 832 | | { |
| | 0 | 833 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(item)); |
| | | 834 | | } |
| | 0 | 835 | | if (item.Parent != _outer) |
| | | 836 | | { |
| | 0 | 837 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.SFxMismatchedOperationParent); |
| | | 838 | | } |
| | | 839 | | |
| | 0 | 840 | | _outer.InvalidateRuntime(); |
| | 0 | 841 | | base.SetItem(index, item); |
| | 0 | 842 | | } |
| | | 843 | | } |
| | | 844 | | |
| | | 845 | | private class CallbackInstanceProvider : IInstanceProvider |
| | | 846 | | { |
| | | 847 | | object IInstanceProvider.GetInstance(InstanceContext instanceContext) |
| | | 848 | | { |
| | 0 | 849 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.SFxCannotActi |
| | | 850 | | } |
| | | 851 | | |
| | | 852 | | object IInstanceProvider.GetInstance(InstanceContext instanceContext, Message message) |
| | | 853 | | { |
| | 0 | 854 | | throw TraceUtility.ThrowHelperError(new InvalidOperationException(SR.SFxCannotActivateCallbackInstace), |
| | | 855 | | } |
| | | 856 | | |
| | | 857 | | void IInstanceProvider.ReleaseInstance(InstanceContext instanceContext, object instance) |
| | | 858 | | { |
| | 0 | 859 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.SFxCannotActi |
| | | 860 | | } |
| | | 861 | | } |
| | | 862 | | } |
| | | 863 | | } |