ChannelDispatcherCollection.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Dispatcher
  5. {
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Diagnostics;
  10. using System.Runtime.Serialization;
  11. public class ChannelDispatcherCollection : SynchronizedCollection<ChannelDispatcherBase>
  12. {
  13. ServiceHostBase service;
  14. internal ChannelDispatcherCollection(ServiceHostBase service, object syncRoot)
  15. : base(syncRoot)
  16. {
  17. if (service == null)
  18. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("service");
  19. this.service = service;
  20. }
  21. protected override void ClearItems()
  22. {
  23. ChannelDispatcherBase[] array = new ChannelDispatcherBase[this.Count];
  24. this.CopyTo(array, 0);
  25. base.ClearItems();
  26. if (this.service != null)
  27. {
  28. foreach (ChannelDispatcherBase channelDispatcher in array)
  29. this.service.OnRemoveChannelDispatcher(channelDispatcher);
  30. }
  31. }
  32. protected override void InsertItem(int index, ChannelDispatcherBase item)
  33. {
  34. if (this.service != null)
  35. {
  36. if (this.service.State == CommunicationState.Closed)
  37. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(this.service.GetType().ToString()));
  38. this.service.OnAddChannelDispatcher(item);
  39. }
  40. base.InsertItem(index, item);
  41. }
  42. protected override void RemoveItem(int index)
  43. {
  44. ChannelDispatcherBase channelDispatcher = this.Items[index];
  45. base.RemoveItem(index);
  46. if (this.service != null)
  47. this.service.OnRemoveChannelDispatcher(channelDispatcher);
  48. }
  49. protected override void SetItem(int index, ChannelDispatcherBase item)
  50. {
  51. if (this.service != null)
  52. {
  53. if (this.service.State == CommunicationState.Closed)
  54. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(this.service.GetType().ToString()));
  55. }
  56. if (this.service != null)
  57. this.service.OnAddChannelDispatcher(item);
  58. ChannelDispatcherBase old;
  59. lock (this.SyncRoot)
  60. {
  61. old = this.Items[index];
  62. base.SetItem(index, item);
  63. }
  64. if (this.service != null)
  65. this.service.OnRemoveChannelDispatcher(old);
  66. }
  67. }
  68. }