BindingContext.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // BindingContext.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005-2006 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.ServiceModel;
  31. using System.ServiceModel.Description;
  32. using System.ServiceModel.Security;
  33. namespace System.ServiceModel.Channels
  34. {
  35. public class BindingContext
  36. {
  37. static BindingElementCollection empty_collection =
  38. new BindingElementCollection ();
  39. CustomBinding binding;
  40. BindingParameterCollection parameters;
  41. BindingElementCollection elements = empty_collection;
  42. BindingElementCollection remaining_elements;
  43. Uri listen_uri_base;
  44. string listen_uri_relative;
  45. ListenUriMode listen_uri_mode;
  46. public BindingContext (CustomBinding binding,
  47. BindingParameterCollection parameters)
  48. {
  49. if (binding == null)
  50. throw new ArgumentNullException ("binding");
  51. if (parameters == null)
  52. throw new ArgumentNullException ("parameters");
  53. this.binding = binding;
  54. this.parameters = parameters;
  55. }
  56. public BindingContext (CustomBinding binding,
  57. BindingParameterCollection parameters,
  58. Uri listenUriBaseAddress,
  59. string listenUriRelativeAddress,
  60. ListenUriMode listenUriMode)
  61. : this (binding, parameters)
  62. {
  63. listen_uri_base = listenUriBaseAddress;
  64. listen_uri_relative = listenUriRelativeAddress;
  65. listen_uri_mode = listenUriMode;
  66. }
  67. // copy .ctor().
  68. private BindingContext (BindingContext other)
  69. {
  70. binding = other.binding;
  71. parameters = other.parameters;
  72. elements = other.elements == empty_collection ?
  73. empty_collection : other.elements.Clone ();
  74. }
  75. public CustomBinding Binding {
  76. get { return binding; }
  77. }
  78. public BindingParameterCollection BindingParameters {
  79. get { return parameters; }
  80. }
  81. public Uri ListenUriBaseAddress {
  82. get { return listen_uri_base; }
  83. set { listen_uri_base = value; }
  84. }
  85. public string ListenUriRelativeAddress {
  86. get { return listen_uri_relative; }
  87. set { listen_uri_relative = value; }
  88. }
  89. public ListenUriMode ListenUriMode {
  90. get { return listen_uri_mode; }
  91. set { listen_uri_mode = value; }
  92. }
  93. public BindingElementCollection RemainingBindingElements {
  94. get {
  95. if (remaining_elements == null)
  96. remaining_elements = new BindingElementCollection ();
  97. return remaining_elements;
  98. }
  99. }
  100. internal BindingElement DequeueBindingElement ()
  101. {
  102. return DequeueBindingElement (true);
  103. }
  104. BindingElement DequeueBindingElement (bool raiseError)
  105. {
  106. if (elements.Count == 0) {
  107. if (raiseError)
  108. throw new InvalidOperationException ("There is no more available binding element.");
  109. else
  110. return null;
  111. }
  112. BindingElement el = elements [0];
  113. elements.RemoveAt (0);
  114. return el;
  115. }
  116. private bool PrepareElements ()
  117. {
  118. if (elements != empty_collection)
  119. return false;
  120. elements = binding.CreateBindingElements ();
  121. return true;
  122. }
  123. public IChannelFactory<TChannel>
  124. BuildInnerChannelFactory<TChannel> ()
  125. {
  126. bool restore = PrepareElements ();
  127. try {
  128. return DequeueBindingElement ().BuildChannelFactory<TChannel> (this);
  129. } finally {
  130. if (restore)
  131. elements = empty_collection;
  132. }
  133. }
  134. #if !NET_2_1
  135. public IChannelListener<TChannel>
  136. BuildInnerChannelListener<TChannel> ()
  137. where TChannel : class, IChannel
  138. {
  139. bool restore = PrepareElements ();
  140. try {
  141. return DequeueBindingElement ().BuildChannelListener<TChannel> (this);
  142. } finally {
  143. if (restore)
  144. elements = empty_collection;
  145. }
  146. }
  147. #endif
  148. public bool CanBuildInnerChannelFactory<TChannel> ()
  149. {
  150. bool restore = PrepareElements ();
  151. try {
  152. return elements.Count > 0 &&
  153. DequeueBindingElement ().CanBuildChannelFactory<TChannel> (this);
  154. } finally {
  155. if (restore)
  156. elements = empty_collection;
  157. }
  158. }
  159. #if !NET_2_1
  160. public bool CanBuildInnerChannelListener<TChannel> ()
  161. where TChannel : class, IChannel
  162. {
  163. bool restore = PrepareElements ();
  164. try {
  165. return elements.Count > 0 &&
  166. DequeueBindingElement ().CanBuildChannelListener<TChannel> (this);
  167. } finally {
  168. if (restore)
  169. elements = empty_collection;
  170. }
  171. }
  172. #endif
  173. public T GetInnerProperty<T> () where T : class
  174. {
  175. bool restore = PrepareElements ();
  176. try {
  177. BindingElement e = DequeueBindingElement (false);
  178. return e == null ? default (T) : e.GetProperty<T> (this);
  179. } finally {
  180. if (restore)
  181. elements = empty_collection;
  182. }
  183. }
  184. public BindingContext Clone ()
  185. {
  186. return new BindingContext (this);
  187. }
  188. }
  189. }