1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.Dispatcher
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.Runtime;
using System.Runtime.Diagnostics;
using System.ServiceModel.Channels;
using System.ServiceModel.Diagnostics;
using System.ServiceModel.Diagnostics.Application;
using System.Text;
using System.Transactions;
public class ChannelDispatcher : ChannelDispatcherBase
{
ThreadSafeMessageFilterTable<EndpointAddress> addressTable;
string bindingName;
SynchronizedCollection<IChannelInitializer> channelInitializers;
CommunicationObjectManager<IChannel> channels;
EndpointDispatcherCollection endpointDispatchers;
Collection<IErrorHandler> errorHandlers;
EndpointDispatcherTable filterTable;
ServiceHostBase host;
bool isTransactedReceive;
bool asynchronousTransactedAcceptEnabled;
bool receiveContextEnabled;
readonly IChannelListener listener;
ListenerHandler listenerHandler;
int maxTransactedBatchSize;
MessageVersion messageVersion;
SynchronizedChannelCollection<IChannel> pendingChannels; // app has not yet seen these.
bool receiveSynchronously;
bool sendAsynchronously;
int maxPendingReceives;
bool includeExceptionDetailInFaults;
ServiceThrottle serviceThrottle;
bool session;
SharedRuntimeState shared;
IDefaultCommunicationTimeouts timeouts;
IsolationLevel transactionIsolationLevel = ServiceBehaviorAttribute.DefaultIsolationLevel;
bool transactionIsolationLevelSet;
TimeSpan transactionTimeout;
bool performDefaultCloseInput;
EventTraceActivity eventTraceActivity;
ErrorBehavior errorBehavior;
internal ChannelDispatcher(SharedRuntimeState shared)
{
this.Initialize(shared);
}
public ChannelDispatcher(IChannelListener listener)
: this(listener, null, null)
{
}
public ChannelDispatcher(IChannelListener listener, string bindingName)
: this(listener, bindingName, null)
{
}
public ChannelDispatcher(IChannelListener listener, string bindingName, IDefaultCommunicationTimeouts timeouts)
{
if (listener == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("listener");
}
this.listener = listener;
this.bindingName = bindingName;
this.timeouts = new ImmutableCommunicationTimeouts(timeouts);
this.session = ((listener is IChannelListener<IInputSessionChannel>) ||
(listener is IChannelListener<IReplySessionChannel>) ||
(listener is IChannelListener<IDuplexSessionChannel>));
this.Initialize(new SharedRuntimeState(true));
}
void Initialize(SharedRuntimeState shared)
{
this.shared = shared;
this.endpointDispatchers = new EndpointDispatcherCollection(this);
this.channelInitializers = this.NewBehaviorCollection<IChannelInitializer>();
this.channels = new CommunicationObjectManager<IChannel>(this.ThisLock);
this.pendingChannels = new SynchronizedChannelCollection<IChannel>(this.ThisLock);
this.errorHandlers = new Collection<IErrorHandler>();
this.isTransactedReceive = false;
this.asynchronousTransactedAcceptEnabled = false;
this.receiveSynchronously = false;
this.serviceThrottle = null;
this.transactionTimeout = TimeSpan.Zero;
this.maxPendingReceives = MultipleReceiveBinder.MultipleReceiveDefaults.MaxPendingReceives;
if (this.listener != null)
{
this.listener.Faulted += new EventHandler(OnListenerFaulted);
}
}
public string BindingName
{
get { return this.bindingName; }
}
public SynchronizedCollection<IChannelInitializer> ChannelInitializers
{
get { return this.channelInitializers; }
}
protected override TimeSpan DefaultCloseTimeout
{
get
{
if (this.timeouts != null)
{
return this.timeouts.CloseTimeout;
}
else
{
return ServiceDefaults.CloseTimeout;
}
}
}
protected override TimeSpan DefaultOpenTimeout
{
get
{
if (this.timeouts != null)
{
return this.timeouts.OpenTimeout;
}
else
{
return ServiceDefaults.OpenTimeout;
}
}
}
internal EndpointDispatcherTable EndpointDispatcherTable
{
get { return this.filterTable; }
}
internal CommunicationObjectManager<IChannel> Channels
{
get { return this.channels; }
}
public SynchronizedCollection<EndpointDispatcher> Endpoints
{
get { return this.endpointDispatchers; }
}
public Collection<IErrorHandler> ErrorHandlers
{
get { return this.errorHandlers; }
}
public MessageVersion MessageVersion
{
get { return this.messageVersion; }
set
{
this.messageVersion = value;
this.ThrowIfDisposedOrImmutable();
}
}
internal bool Session
{
get { return this.session; }
}
public override ServiceHostBase Host
{
get { return this.host; }
}
internal bool EnableFaults
{
get { return this.shared.EnableFaults; }
set
{
this.ThrowIfDisposedOrImmutable();
this.shared.EnableFaults = value;
}
}
internal bool IsOnServer
{
get { return this.shared.IsOnServer; }
}
public bool IsTransactedAccept
{
get { return this.isTransactedReceive && this.session; }
}
public bool IsTransactedReceive
{
get
{
return this.isTransactedReceive;
}
set
{
this.ThrowIfDisposedOrImmutable();
this.isTransactedReceive = value;
}
}
public bool AsynchronousTransactedAcceptEnabled
{
get
{
return this.asynchronousTransactedAcceptEnabled;
}
set
{
this.ThrowIfDisposedOrImmutable();
this.asynchronousTransactedAcceptEnabled = value;
}
}
public bool ReceiveContextEnabled
{
get
{
return this.receiveContextEnabled;
}
set
{
this.ThrowIfDisposedOrImmutable();
this.receiveContextEnabled = value;
}
}
internal bool BufferedReceiveEnabled
{
get;
set;
}
public override IChannelListener Listener
{
get { return this.listener; }
}
public int MaxTransactedBatchSize
{
get
{
return this.maxTransactedBatchSize;
}
set
{
if (value < 0)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", value,
SR.GetString(SR.ValueMustBeNonNegative)));
}
this.ThrowIfDisposedOrImmutable();
this.maxTransactedBatchSize = value;
}
}
public ServiceThrottle ServiceThrottle
{
get
{
return this.serviceThrottle;
}
set
{
this.ThrowIfDisposedOrImmutable();
this.serviceThrottle = value;
}
}
public bool ManualAddressing
{
get { return this.shared.ManualAddressing; }
set
{
this.ThrowIfDisposedOrImmutable();
this.shared.ManualAddressing = value;
}
}
internal SynchronizedChannelCollection<IChannel> PendingChannels
{
get { return this.pendingChannels; }
}
public bool ReceiveSynchronously
{
get
{
return this.receiveSynchronously;
}
set
{
this.ThrowIfDisposedOrImmutable();
this.receiveSynchronously = value;
}
}
public bool SendAsynchronously
{
get
{
return this.sendAsynchronously;
}
set
{
this.ThrowIfDisposedOrImmutable();
this.sendAsynchronously = value;
}
}
public int MaxPendingReceives
{
get
{
return this.maxPendingReceives;
}
set
{
this.ThrowIfDisposedOrImmutable();
this.maxPendingReceives = value;
}
}
public bool IncludeExceptionDetailInFaults
{
get { return this.includeExceptionDetailInFaults; }
set
{
lock (this.ThisLock)
{
this.ThrowIfDisposedOrImmutable();
this.includeExceptionDetailInFaults = value;
}
}
}
internal IDefaultCommunicationTimeouts DefaultCommunicationTimeouts
{
get { return this.timeouts; }
}
public IsolationLevel TransactionIsolationLevel
{
get { return this.transactionIsolationLevel; }
set
{
switch (value)
{
case IsolationLevel.Serializable:
case IsolationLevel.RepeatableRead:
case IsolationLevel.ReadCommitted:
case IsolationLevel.ReadUncommitted:
case IsolationLevel.Unspecified:
case IsolationLevel.Chaos:
case IsolationLevel.Snapshot:
break;
default:
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
}
this.ThrowIfDisposedOrImmutable();
this.transactionIsolationLevel = value;
this.transactionIsolationLevelSet = true;
}
}
internal bool TransactionIsolationLevelSet
{
get { return this.transactionIsolationLevelSet; }
}
public TimeSpan TransactionTimeout
{
get
{
return this.transactionTimeout;
}
set
{
if (value < TimeSpan.Zero)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", value,
SR.GetString(SR.SFxTimeoutOutOfRange0)));
}
if (TimeoutHelper.IsTooLarge(value))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", value,
SR.GetString(SR.SFxTimeoutOutOfRangeTooBig)));
}
this.ThrowIfDisposedOrImmutable();
this.transactionTimeout = value;
}
}
void AbortPendingChannels()
{
lock (this.ThisLock)
{
for (int i = this.pendingChannels.Count - 1; i >= 0; i--)
{
this.pendingChannels[i].Abort();
}
}
}
internal override void CloseInput(TimeSpan timeout)
{
// we have to perform some slightly convoluted logic here due to
// backwards compat. We probably need an IAsyncChannelDispatcher
// interface that has timeouts and async
this.CloseInput();
if (this.performDefaultCloseInput)
{
TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
lock (this.ThisLock)
{
if (DiagnosticUtility.ShouldTraceInformation)
{
for (int i = 0; i < this.endpointDispatchers.Count; i++)
{
EndpointDispatcher endpointDispatcher = this.endpointDispatchers[i];
this.TraceEndpointLifetime(endpointDispatcher, TraceCode.EndpointListenerClose, SR.GetString(SR.TraceCodeEndpointListenerClose));
}
}
ListenerHandler handler = this.listenerHandler;
if (handler != null)
{
handler.CloseInput(timeoutHelper.RemainingTime());
}
}
if (!this.session)
{
ListenerHandler handler = this.listenerHandler;
if (handler != null)
{
handler.Close(timeoutHelper.RemainingTime());
}
}
}
}
internal void ReleasePerformanceCounters()
{
if (PerformanceCounters.PerformanceCountersEnabled)
{
for (int i = 0; i < this.endpointDispatchers.Count; i++)
{
if (null != this.endpointDispatchers[i])
{
this.endpointDispatchers[i].ReleasePerformanceCounters();
}
}
}
}
public override void CloseInput()
{
this.performDefaultCloseInput = true;
}
void OnListenerFaulted(object sender, EventArgs e)
{
this.Fault();
}
internal bool HandleError(Exception error)
{
ErrorHandlerFaultInfo dummy = new ErrorHandlerFaultInfo();
return this.HandleError(error, ref dummy);
}
internal bool HandleError(Exception error, ref ErrorHandlerFaultInfo faultInfo)
{
ErrorBehavior behavior;
lock (this.ThisLock)
{
if (this.errorBehavior != null)
{
behavior = this.errorBehavior;
}
else
{
behavior = new ErrorBehavior(this);
}
}
if (behavior != null)
{
return behavior.HandleError(error, ref faultInfo);
}
else
{
return false;
}
}
internal void InitializeChannel(IClientChannel channel)
{
this.ThrowIfDisposedOrNotOpen();
try
{
for (int i = 0; i < this.channelInitializers.Count; ++i)
{
this.channelInitializers[i].Initialize(channel);
}
}
catch (Exception e)
{
if (Fx.IsFatal(e))
{
throw;
}
throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e);
}
}
internal EndpointDispatcher Match(Message message, out bool addressMatched)
{
lock (this.ThisLock)
{
return this.filterTable.Lookup(message, out addressMatched);
}
}
internal SynchronizedCollection<T> NewBehaviorCollection<T>()
{
return new ChannelDispatcherBehaviorCollection<T>(this);
}
internal bool HasApplicationEndpoints()
{
foreach (EndpointDispatcher endpointDispatcher in this.Endpoints)
{
if (!endpointDispatcher.IsSystemEndpoint)
{
return true;
}
}
return false;
}
void OnAddEndpoint(EndpointDispatcher endpoint)
{
lock (this.ThisLock)
{
endpoint.Attach(this);
if (this.State == CommunicationState.Opened)
{
if (this.addressTable != null)
{
this.addressTable.Add(endpoint.AddressFilter, endpoint.EndpointAddress, endpoint.FilterPriority);
}
this.filterTable.AddEndpoint(endpoint);
}
}
}
void OnRemoveEndpoint(EndpointDispatcher endpoint)
{
lock (this.ThisLock)
{
if (this.State == CommunicationState.Opened)
{
this.filterTable.RemoveEndpoint(endpoint);
if (this.addressTable != null)
{
this.addressTable.Remove(endpoint.AddressFilter);
}
}
endpoint.Detach(this);
}
}
protected override void OnAbort()
{
if (this.listener != null)
{
this.listener.Abort();
}
ListenerHandler handler = this.listenerHandler;
if (handler != null)
{
handler.Abort();
}
this.AbortPendingChannels();
}
protected override void OnClose(TimeSpan timeout)
{
TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
if (this.listener != null)
{
this.listener.Close(timeoutHelper.RemainingTime());
}
ListenerHandler handler = this.listenerHandler;
if (handler != null)
{
handler.Close(timeoutHelper.RemainingTime());
}
this.AbortPendingChannels();
}
protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
{
List<ICommunicationObject> list = new List<ICommunicationObject>();
if (this.listener != null)
{
list.Add(this.listener);
}
ListenerHandler handler = this.listenerHandler;
if (handler != null)
{
list.Add(handler);
}
return new CloseCollectionAsyncResult(timeout, callback, state, list);
}
protected override void OnEndClose(IAsyncResult result)
{
try
{
CloseCollectionAsyncResult.End(result);
}
finally
{
this.AbortPendingChannels();
}
}
protected override void OnClosed()
{
base.OnClosed();
if (DiagnosticUtility.ShouldTraceInformation)
{
for (int i = 0; i < this.endpointDispatchers.Count; i++)
{
EndpointDispatcher endpointDispatcher = this.endpointDispatchers[i];
this.TraceEndpointLifetime(endpointDispatcher, TraceCode.EndpointListenerClose, SR.GetString(SR.TraceCodeEndpointListenerClose));
}
}
}
protected override void OnOpen(TimeSpan timeout)
{
ThrowIfNotAttachedToHost();
ThrowIfNoMessageVersion();
if (this.listener != null)
{
try
{
this.listener.Open(timeout);
}
catch (InvalidOperationException e)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateOuterExceptionWithEndpointsInformation(e));
}
}
}
InvalidOperationException CreateOuterExceptionWithEndpointsInformation(InvalidOperationException e)
{
string endpointContractNames = CreateContractListString();
if (String.IsNullOrEmpty(endpointContractNames))
{
return new InvalidOperationException(SR.GetString(SR.SFxChannelDispatcherUnableToOpen1, this.listener.Uri), e);
}
else
{
return new InvalidOperationException(SR.GetString(SR.SFxChannelDispatcherUnableToOpen2, this.listener.Uri, endpointContractNames), e);
}
}
internal string CreateContractListString()
{
const string OpenQuote = "\"";
const string CloseQuote = "\"";
const string Space = " ";
Collection<string> namesSeen = new Collection<string>();
StringBuilder endpointContractNames = new StringBuilder();
lock (this.ThisLock)
{
foreach (EndpointDispatcher ed in this.Endpoints)
{
if (!namesSeen.Contains(ed.ContractName))
{
if (endpointContractNames.Length > 0)
{
endpointContractNames.Append(CultureInfo.CurrentCulture.TextInfo.ListSeparator);
endpointContractNames.Append(Space);
}
endpointContractNames.Append(OpenQuote);
endpointContractNames.Append(ed.ContractName);
endpointContractNames.Append(CloseQuote);
namesSeen.Add(ed.ContractName);
}
}
}
return endpointContractNames.ToString();
}
protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
{
ThrowIfNotAttachedToHost();
ThrowIfNoMessageVersion();
if (this.listener != null)
{
try
{
return this.listener.BeginOpen(timeout, callback, state);
}
catch (InvalidOperationException e)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateOuterExceptionWithEndpointsInformation(e));
}
}
else
{
return new CompletedAsyncResult(callback, state);
}
}
protected override void OnEndOpen(IAsyncResult result)
{
if (this.listener != null)
{
try
{
this.listener.EndOpen(result);
}
catch (InvalidOperationException e)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateOuterExceptionWithEndpointsInformation(e));
}
}
else
{
CompletedAsyncResult.End(result);
}
}
protected override void OnOpening()
{
ThrowIfNotAttachedToHost();
if (TD.ListenerOpenStartIsEnabled())
{
this.eventTraceActivity = EventTraceActivity.GetFromThreadOrCreate();
TD.ListenerOpenStart(this.eventTraceActivity,
(this.Listener != null) ? this.Listener.Uri.ToString() : string.Empty,
(this.host != null && host.EventTraceActivity != null) ? this.host.EventTraceActivity.ActivityId : Guid.Empty);
}
base.OnOpening();
}
protected override void OnOpened()
{
ThrowIfNotAttachedToHost();
base.OnOpened();
if (TD.ListenerOpenStopIsEnabled())
{
TD.ListenerOpenStop(this.eventTraceActivity);
this.eventTraceActivity = null; // clear this since we don't need this anymore.
}
this.errorBehavior = new ErrorBehavior(this);
this.filterTable = new EndpointDispatcherTable(this.ThisLock);
for (int i = 0; i < this.endpointDispatchers.Count; i++)
{
EndpointDispatcher endpoint = this.endpointDispatchers[i];
// Force a build of the runtime to catch any unexpected errors before we are done opening.
endpoint.DispatchRuntime.GetRuntime();
// Lock down the DispatchRuntime.
endpoint.DispatchRuntime.LockDownProperties();
this.filterTable.AddEndpoint(endpoint);
if ((this.addressTable != null) && (endpoint.OriginalAddress != null))
{
this.addressTable.Add(endpoint.AddressFilter, endpoint.OriginalAddress, endpoint.FilterPriority);
}
if (DiagnosticUtility.ShouldTraceInformation)
{
this.TraceEndpointLifetime(endpoint, TraceCode.EndpointListenerOpen, SR.GetString(SR.TraceCodeEndpointListenerOpen));
}
}
ServiceThrottle throttle = this.serviceThrottle;
if (throttle == null)
{
throttle = this.host.ServiceThrottle;
}
IListenerBinder binder = ListenerBinder.GetBinder(this.listener, this.messageVersion);
this.listenerHandler = new ListenerHandler(binder, this, this.host, throttle, this.timeouts);
this.listenerHandler.Open(); // This never throws, which is why it's ok for it to happen in OnOpened
}
internal void ProvideFault(Exception e, FaultConverter faultConverter, ref ErrorHandlerFaultInfo faultInfo)
{
ErrorBehavior behavior;
lock (this.ThisLock)
{
if (this.errorBehavior != null)
{
behavior = this.errorBehavior;
}
else
{
behavior = new ErrorBehavior(this);
}
}
behavior.ProvideFault(e, faultConverter, ref faultInfo);
}
internal void SetEndpointAddressTable(ThreadSafeMessageFilterTable<EndpointAddress> table)
{
if (table == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("table");
}
this.ThrowIfDisposedOrImmutable();
this.addressTable = table;
}
internal new void ThrowIfDisposedOrImmutable()
{
base.ThrowIfDisposedOrImmutable();
this.shared.ThrowIfImmutable();
}
void ThrowIfNotAttachedToHost()
{
// if we are on the server, we need a host
// if we are on the client, we never call Open(), so this method is not invoked
if (this.host == null)
{
Exception error = new InvalidOperationException(SR.GetString(SR.SFxChannelDispatcherNoHost0));
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(error);
}
}
void ThrowIfNoMessageVersion()
{
if (this.messageVersion == null)
{
Exception error = new InvalidOperationException(SR.GetString(SR.SFxChannelDispatcherNoMessageVersion));
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(error);
}
}
void TraceEndpointLifetime(EndpointDispatcher endpoint, int traceCode, string traceDescription)
{
if (DiagnosticUtility.ShouldTraceInformation)
{
Dictionary<string, object> values = new Dictionary<string, object>(3)
{
{ "ContractNamespace", endpoint.ContractNamespace },
{ "ContractName", endpoint.ContractName },
{ "Endpoint", endpoint.ListenUri }
};
TraceUtility.TraceEvent(TraceEventType.Information, traceCode,
traceDescription, new DictionaryTraceRecord(values), endpoint, null);
}
}
protected override void Attach(ServiceHostBase host)
{
if (host == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("host");
}
ServiceHostBase serviceHost = host;
this.ThrowIfDisposedOrImmutable();
if (this.host != null)
{
Exception error = new InvalidOperationException(SR.GetString(SR.SFxChannelDispatcherMultipleHost0));
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(error);
}
this.host = serviceHost;
}
protected override void Detach(ServiceHostBase host)
{
if (host == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("host");
}
if (this.host != host)
{
Exception error = new InvalidOperationException(SR.GetString(SR.SFxChannelDispatcherDifferentHost0));
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(error);
}
this.ThrowIfDisposedOrImmutable();
this.host = null;
}
class EndpointDispatcherCollection : SynchronizedCollection<EndpointDispatcher>
{
ChannelDispatcher owner;
internal EndpointDispatcherCollection(ChannelDispatcher owner)
: base(owner.ThisLock)
{
this.owner = owner;
}
protected override void ClearItems()
{
foreach (EndpointDispatcher item in this.Items)
{
this.owner.OnRemoveEndpoint(item);
}
base.ClearItems();
}
protected override void InsertItem(int index, EndpointDispatcher item)
{
if (item == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("item");
this.owner.OnAddEndpoint(item);
base.InsertItem(index, item);
}
protected override void RemoveItem(int index)
{
EndpointDispatcher item = this.Items[index];
base.RemoveItem(index);
this.owner.OnRemoveEndpoint(item);
}
protected override void SetItem(int index, EndpointDispatcher item)
{
Exception error = new InvalidOperationException(SR.GetString(SR.SFxCollectionDoesNotSupportSet0));
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(error);
}
}
class ChannelDispatcherBehaviorCollection<T> : SynchronizedCollection<T>
{
ChannelDispatcher outer;
internal ChannelDispatcherBehaviorCollection(ChannelDispatcher outer)
: base(outer.ThisLock)
{
this.outer = outer;
}
protected override void ClearItems()
{
this.outer.ThrowIfDisposedOrImmutable();
base.ClearItems();
}
protected override void InsertItem(int index, T item)
{
if (item == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("item");
}
this.outer.ThrowIfDisposedOrImmutable();
base.InsertItem(index, item);
}
protected override void RemoveItem(int index)
{
this.outer.ThrowIfDisposedOrImmutable();
base.RemoveItem(index);
}
protected override void SetItem(int index, T item)
{
if (item == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("item");
}
this.outer.ThrowIfDisposedOrImmutable();
base.SetItem(index, item);
}
}
}
}
|