ChannelParameterCollection.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System;
  7. using System.Collections.ObjectModel;
  8. public class ChannelParameterCollection : Collection<object>
  9. {
  10. IChannel channel;
  11. public ChannelParameterCollection()
  12. {
  13. }
  14. public ChannelParameterCollection(IChannel channel)
  15. {
  16. this.channel = channel;
  17. }
  18. protected virtual IChannel Channel
  19. {
  20. get { return this.channel; }
  21. }
  22. public void PropagateChannelParameters(IChannel innerChannel)
  23. {
  24. if (innerChannel == null)
  25. {
  26. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("innerChannel");
  27. }
  28. this.ThrowIfMutable();
  29. ChannelParameterCollection innerCollection = innerChannel.GetProperty<ChannelParameterCollection>();
  30. if (innerCollection != null)
  31. {
  32. for (int i = 0; i < this.Count; i++)
  33. {
  34. innerCollection.Add(this[i]);
  35. }
  36. }
  37. }
  38. protected override void ClearItems()
  39. {
  40. this.ThrowIfDisposedOrImmutable();
  41. base.ClearItems();
  42. }
  43. protected override void InsertItem(int index, object item)
  44. {
  45. this.ThrowIfDisposedOrImmutable();
  46. base.InsertItem(index, item);
  47. }
  48. protected override void RemoveItem(int index)
  49. {
  50. this.ThrowIfDisposedOrImmutable();
  51. base.RemoveItem(index);
  52. }
  53. protected override void SetItem(int index, object item)
  54. {
  55. this.ThrowIfDisposedOrImmutable();
  56. base.SetItem(index, item);
  57. }
  58. void ThrowIfDisposedOrImmutable()
  59. {
  60. IChannel channel = this.Channel;
  61. if (channel != null)
  62. {
  63. CommunicationState state = channel.State;
  64. string text = null;
  65. switch (state)
  66. {
  67. case CommunicationState.Created:
  68. break;
  69. case CommunicationState.Opening:
  70. case CommunicationState.Opened:
  71. case CommunicationState.Closing:
  72. case CommunicationState.Closed:
  73. case CommunicationState.Faulted:
  74. text = SR.GetString(SR.ChannelParametersCannotBeModified,
  75. channel.GetType().ToString(), state.ToString());
  76. break;
  77. default:
  78. text = SR.GetString(SR.CommunicationObjectInInvalidState,
  79. channel.GetType().ToString(), state.ToString());
  80. break;
  81. }
  82. if (text != null)
  83. {
  84. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(text));
  85. }
  86. }
  87. }
  88. void ThrowIfMutable()
  89. {
  90. IChannel channel = this.Channel;
  91. if (channel != null)
  92. {
  93. CommunicationState state = channel.State;
  94. string text = null;
  95. switch (state)
  96. {
  97. case CommunicationState.Created:
  98. text = SR.GetString(SR.ChannelParametersCannotBePropagated,
  99. channel.GetType().ToString(), state.ToString());
  100. break;
  101. case CommunicationState.Opening:
  102. case CommunicationState.Opened:
  103. case CommunicationState.Closing:
  104. case CommunicationState.Closed:
  105. case CommunicationState.Faulted:
  106. break;
  107. default:
  108. text = SR.GetString(SR.CommunicationObjectInInvalidState,
  109. channel.GetType().ToString(), state.ToString());
  110. break;
  111. }
  112. if (text != null)
  113. {
  114. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(text));
  115. }
  116. }
  117. }
  118. }
  119. }