SynchronizedChannelCollection.cs 2.4 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.ServiceModel.Channels;
  9. class SynchronizedChannelCollection<TChannel> : SynchronizedCollection<TChannel>
  10. where TChannel : IChannel
  11. {
  12. EventHandler onChannelClosed;
  13. EventHandler onChannelFaulted;
  14. internal SynchronizedChannelCollection(object syncRoot)
  15. : base(syncRoot)
  16. {
  17. this.onChannelClosed = new EventHandler(OnChannelClosed);
  18. this.onChannelFaulted = new EventHandler(OnChannelFaulted);
  19. }
  20. void AddingChannel(TChannel channel)
  21. {
  22. channel.Faulted += this.onChannelFaulted;
  23. channel.Closed += this.onChannelClosed;
  24. }
  25. void RemovingChannel(TChannel channel)
  26. {
  27. channel.Faulted -= this.onChannelFaulted;
  28. channel.Closed -= this.onChannelClosed;
  29. }
  30. void OnChannelClosed(object sender, EventArgs args)
  31. {
  32. TChannel channel = (TChannel)sender;
  33. this.Remove(channel);
  34. }
  35. void OnChannelFaulted(object sender, EventArgs args)
  36. {
  37. TChannel channel = (TChannel)sender;
  38. this.Remove(channel);
  39. }
  40. protected override void ClearItems()
  41. {
  42. List<TChannel> items = this.Items;
  43. for (int i = 0; i < items.Count; i++)
  44. {
  45. this.RemovingChannel(items[i]);
  46. }
  47. base.ClearItems();
  48. }
  49. protected override void InsertItem(int index, TChannel item)
  50. {
  51. this.AddingChannel(item);
  52. base.InsertItem(index, item);
  53. }
  54. protected override void RemoveItem(int index)
  55. {
  56. TChannel oldItem = this.Items[index];
  57. base.RemoveItem(index);
  58. this.RemovingChannel(oldItem);
  59. }
  60. protected override void SetItem(int index, TChannel item)
  61. {
  62. TChannel oldItem = this.Items[index];
  63. this.AddingChannel(item);
  64. base.SetItem(index, item);
  65. this.RemovingChannel(oldItem);
  66. }
  67. }
  68. }