BindingContext.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. CustomBinding binding;
  38. BindingParameterCollection parameters;
  39. Uri listen_uri_base;
  40. string listen_uri_relative;
  41. ListenUriMode listen_uri_mode;
  42. BindingElementCollection elements; // for internal use
  43. public BindingContext (CustomBinding binding,
  44. BindingParameterCollection parms)
  45. {
  46. if (binding == null)
  47. throw new ArgumentNullException ("binding");
  48. if (parms == null)
  49. throw new ArgumentNullException ("parms");
  50. this.binding = binding;
  51. parameters = new BindingParameterCollection ();
  52. foreach (var item in parms)
  53. parameters.Add (item);
  54. this.elements = new BindingElementCollection ();
  55. foreach (var item in binding.Elements)
  56. this.elements.Add (item);
  57. }
  58. public BindingContext (CustomBinding binding,
  59. BindingParameterCollection parameters,
  60. Uri listenUriBaseAddress,
  61. string listenUriRelativeAddress,
  62. ListenUriMode listenUriMode)
  63. : this (binding, parameters)
  64. {
  65. listen_uri_base = listenUriBaseAddress;
  66. listen_uri_relative = listenUriRelativeAddress;
  67. listen_uri_mode = listenUriMode;
  68. }
  69. // deep clone .ctor().
  70. BindingContext (CustomBinding binding,
  71. BindingParameterCollection parms,
  72. BindingElementCollection elems,
  73. Uri listenUriBaseAddress,
  74. string listenUriRelativeAddress,
  75. ListenUriMode listenUriMode)
  76. {
  77. this.binding = new CustomBinding (binding);
  78. parameters = new BindingParameterCollection ();
  79. foreach (var item in parms)
  80. parameters.Add (item);
  81. this.elements = new BindingElementCollection ();
  82. foreach (var item in elems)
  83. this.elements.Add (item);
  84. listen_uri_base = listenUriBaseAddress;
  85. listen_uri_relative = listenUriRelativeAddress;
  86. listen_uri_mode = listenUriMode;
  87. }
  88. public CustomBinding Binding {
  89. get { return binding; }
  90. }
  91. public BindingParameterCollection BindingParameters {
  92. get { return parameters; }
  93. }
  94. public Uri ListenUriBaseAddress {
  95. get { return listen_uri_base; }
  96. set { listen_uri_base = value; }
  97. }
  98. public string ListenUriRelativeAddress {
  99. get { return listen_uri_relative; }
  100. set { listen_uri_relative = value; }
  101. }
  102. public ListenUriMode ListenUriMode {
  103. get { return listen_uri_mode; }
  104. set { listen_uri_mode = value; }
  105. }
  106. public BindingElementCollection RemainingBindingElements {
  107. get {
  108. if (elements == null)
  109. elements = binding.CreateBindingElements ();
  110. return elements;
  111. }
  112. }
  113. internal BindingElement DequeueBindingElement ()
  114. {
  115. return DequeueBindingElement (true);
  116. }
  117. BindingElement DequeueBindingElement (bool raiseError)
  118. {
  119. if (RemainingBindingElements.Count == 0) {
  120. if (raiseError)
  121. throw new InvalidOperationException ("There is no more available binding element.");
  122. else
  123. return null;
  124. }
  125. BindingElement el = RemainingBindingElements [0];
  126. RemainingBindingElements.RemoveAt (0);
  127. return el;
  128. }
  129. public IChannelFactory<TChannel>
  130. BuildInnerChannelFactory<TChannel> ()
  131. {
  132. BindingContext ctx = this.Clone ();
  133. return ctx.DequeueBindingElement ().BuildChannelFactory<TChannel> (ctx);
  134. }
  135. #if !MOBILE
  136. public IChannelListener<TChannel>
  137. BuildInnerChannelListener<TChannel> ()
  138. where TChannel : class, IChannel
  139. {
  140. BindingContext ctx = this.Clone ();
  141. var be = ctx.DequeueBindingElement (false);
  142. if (be == null)
  143. throw new InvalidOperationException ("There is likely no TransportBindingElement that can build a channel listener in this binding context");
  144. return be.BuildChannelListener<TChannel> (ctx);
  145. }
  146. #endif
  147. public bool CanBuildInnerChannelFactory<TChannel> ()
  148. {
  149. BindingContext ctx = this.Clone ();
  150. return ctx.DequeueBindingElement ().CanBuildChannelFactory<TChannel> (ctx);
  151. }
  152. #if !MOBILE
  153. public bool CanBuildInnerChannelListener<TChannel> ()
  154. where TChannel : class, IChannel
  155. {
  156. BindingContext ctx = this.Clone ();
  157. var be = ctx.DequeueBindingElement (false);
  158. if (be == null)
  159. throw new InvalidOperationException ("There is likely no TransportBindingElement that can build a channel listener in this binding context");
  160. return be.CanBuildChannelListener<TChannel> (ctx);
  161. }
  162. #endif
  163. public T GetInnerProperty<T> () where T : class
  164. {
  165. BindingContext ctx = this.Clone ();
  166. BindingElement e = ctx.DequeueBindingElement (false);
  167. return e == null ? default (T) : e.GetProperty<T> (ctx);
  168. }
  169. public BindingContext Clone ()
  170. {
  171. return new BindingContext (this.binding, this.parameters, this.elements, this.listen_uri_base, this.listen_uri_relative, this.listen_uri_mode);
  172. }
  173. }
  174. }