Binding.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. //
  2. // Binding.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005 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.Channels;
  31. using System.ServiceModel.Description;
  32. namespace System.ServiceModel.Channels
  33. {
  34. public abstract class Binding : IDefaultCommunicationTimeouts
  35. {
  36. string name, ns;
  37. TimeSpan open_timeout, close_timeout;
  38. TimeSpan receive_timeout, send_timeout;
  39. protected Binding ()
  40. {
  41. Initialize ();
  42. name = GetType ().Name;
  43. ns = "http://tempuri.org/";
  44. }
  45. protected Binding (string name, string ns)
  46. {
  47. this.name = name;
  48. this.ns = ns;
  49. Initialize ();
  50. }
  51. public TimeSpan CloseTimeout {
  52. get { return close_timeout; }
  53. set { close_timeout = value; }
  54. }
  55. public TimeSpan OpenTimeout {
  56. get { return open_timeout; }
  57. set { open_timeout = value; }
  58. }
  59. public TimeSpan ReceiveTimeout {
  60. get { return receive_timeout; }
  61. set { receive_timeout = value; }
  62. }
  63. public TimeSpan SendTimeout {
  64. get { return send_timeout; }
  65. set { send_timeout = value; }
  66. }
  67. public string Name {
  68. get { return name; }
  69. set { name = value; }
  70. }
  71. public string Namespace {
  72. get { return ns; }
  73. set { ns = value; }
  74. }
  75. public abstract string Scheme { get; }
  76. public MessageVersion MessageVersion {
  77. get { return GetProperty<MessageVersion> (new BindingParameterCollection ()); }
  78. }
  79. BindingContext CreateContext (
  80. BindingParameterCollection parameters)
  81. {
  82. // FIXME: it seems that binding elements are
  83. // "validated" so that the last item is a transport.
  84. return new BindingContext (
  85. new CustomBinding (this), parameters);
  86. }
  87. BindingContext CreateContext (
  88. Uri listenUriBaseAddress,
  89. string listenUriRelativeAddress,
  90. ListenUriMode listenUriMode,
  91. BindingParameterCollection parameters)
  92. {
  93. // FIXME: it seems that binding elements are
  94. // "validated" so that the last item is a transport.
  95. return new BindingContext (
  96. new CustomBinding (this),
  97. parameters,
  98. listenUriBaseAddress,
  99. listenUriRelativeAddress,
  100. listenUriMode);
  101. }
  102. public IChannelFactory<TChannel>
  103. BuildChannelFactory<TChannel> (
  104. params object [] parameters)
  105. {
  106. BindingParameterCollection pl =
  107. new BindingParameterCollection ();
  108. foreach (object o in parameters)
  109. pl.Add (o);
  110. return BuildChannelFactory<TChannel> (pl);
  111. }
  112. public virtual IChannelFactory<TChannel>
  113. BuildChannelFactory<TChannel> (
  114. BindingParameterCollection parameters)
  115. {
  116. if (parameters == null)
  117. throw new ArgumentNullException ("parameters");
  118. return CreateContext (parameters).BuildInnerChannelFactory<TChannel> ();
  119. }
  120. #if !MOBILE
  121. public virtual IChannelListener<TChannel>
  122. BuildChannelListener<TChannel> (
  123. Uri listenUriBaseAddress,
  124. string listenUriRelativeAddress,
  125. ListenUriMode listenUriMode,
  126. params object [] parameters)
  127. where TChannel : class, IChannel
  128. {
  129. BindingParameterCollection pl =
  130. new BindingParameterCollection ();
  131. foreach (object o in parameters)
  132. pl.Add (o);
  133. return BuildChannelListener<TChannel> (
  134. listenUriBaseAddress,
  135. listenUriRelativeAddress,
  136. listenUriMode,
  137. pl);
  138. }
  139. public virtual IChannelListener<TChannel>
  140. BuildChannelListener<TChannel> (
  141. Uri listenUriBaseAddress,
  142. string listenUriRelativeAddress,
  143. ListenUriMode listenUriMode,
  144. BindingParameterCollection parameters)
  145. where TChannel : class, IChannel
  146. {
  147. if (listenUriBaseAddress == null)
  148. throw new ArgumentNullException ("listenUriBaseAddress");
  149. if (listenUriRelativeAddress == null)
  150. throw new ArgumentNullException ("listenUriRelativeAddress");
  151. if (parameters == null)
  152. throw new ArgumentNullException ("parameters");
  153. BindingContext ctx = CreateContext (listenUriBaseAddress,
  154. listenUriRelativeAddress,
  155. listenUriMode,
  156. parameters);
  157. return ctx.BuildInnerChannelListener<TChannel> ();
  158. }
  159. public virtual IChannelListener<TChannel>
  160. BuildChannelListener<TChannel> (
  161. Uri listenUri,
  162. params object [] parameters)
  163. where TChannel : class, IChannel
  164. {
  165. BindingParameterCollection pl =
  166. new BindingParameterCollection ();
  167. foreach (object o in parameters)
  168. pl.Add (o);
  169. return BuildChannelListener<TChannel> (listenUri, pl);
  170. }
  171. public virtual IChannelListener<TChannel>
  172. BuildChannelListener<TChannel> (
  173. Uri listenUri,
  174. BindingParameterCollection parameters)
  175. where TChannel : class, IChannel
  176. {
  177. return BuildChannelListener<TChannel> (listenUri,
  178. String.Empty, parameters);
  179. }
  180. public virtual IChannelListener<TChannel>
  181. BuildChannelListener<TChannel> (
  182. Uri listenUriBaseAddress,
  183. string listenUriRelativeAddress,
  184. params object [] parameters)
  185. where TChannel : class, IChannel
  186. {
  187. BindingParameterCollection pl =
  188. new BindingParameterCollection ();
  189. foreach (object o in parameters)
  190. pl.Add (o);
  191. return BuildChannelListener<TChannel> (
  192. listenUriBaseAddress, listenUriRelativeAddress, pl);
  193. }
  194. public virtual IChannelListener<TChannel>
  195. BuildChannelListener<TChannel> (
  196. Uri listenUriBaseAddress,
  197. string listenUriRelativeAddress,
  198. BindingParameterCollection parameters)
  199. where TChannel : class, IChannel
  200. {
  201. return BuildChannelListener<TChannel> (
  202. listenUriBaseAddress,
  203. listenUriRelativeAddress,
  204. ListenUriMode.Explicit,
  205. parameters);
  206. }
  207. public virtual IChannelListener<TChannel>
  208. BuildChannelListener<TChannel> (
  209. params object [] parameters)
  210. where TChannel : class, IChannel
  211. {
  212. BindingParameterCollection pl =
  213. new BindingParameterCollection ();
  214. foreach (object o in parameters)
  215. pl.Add (o);
  216. return BuildChannelListener<TChannel> (pl);
  217. }
  218. public virtual IChannelListener<TChannel>
  219. BuildChannelListener<TChannel> (
  220. BindingParameterCollection parameters)
  221. where TChannel : class, IChannel
  222. {
  223. if (parameters == null)
  224. throw new ArgumentNullException ("parameters");
  225. return CreateContext (parameters).BuildInnerChannelListener<TChannel> ();
  226. }
  227. #endif
  228. public bool CanBuildChannelFactory<TChannel> (
  229. params object [] parameters)
  230. {
  231. BindingParameterCollection pl =
  232. new BindingParameterCollection ();
  233. foreach (object o in parameters)
  234. pl.Add (o);
  235. return CanBuildChannelFactory<TChannel> (pl);
  236. }
  237. public virtual bool CanBuildChannelFactory<TChannel> (
  238. BindingParameterCollection parameters)
  239. {
  240. if (parameters == null)
  241. throw new ArgumentNullException ("parameters");
  242. return CreateContext (parameters).CanBuildInnerChannelFactory<TChannel> ();
  243. }
  244. #if !MOBILE
  245. public bool CanBuildChannelListener<TChannel> (
  246. params object [] parameters)
  247. where TChannel : class, IChannel
  248. {
  249. BindingParameterCollection pl =
  250. new BindingParameterCollection ();
  251. foreach (object o in parameters)
  252. pl.Add (o);
  253. return CanBuildChannelListener<TChannel> (pl);
  254. }
  255. public virtual bool CanBuildChannelListener<TChannel> (
  256. BindingParameterCollection parameters)
  257. where TChannel : class, IChannel
  258. {
  259. if (parameters == null)
  260. throw new ArgumentNullException ("parameters");
  261. return CreateContext (parameters).CanBuildInnerChannelListener<TChannel> ();
  262. }
  263. #endif
  264. public abstract BindingElementCollection CreateBindingElements ();
  265. public T GetProperty<T> (BindingParameterCollection parameters)
  266. where T : class
  267. {
  268. if (parameters == null)
  269. throw new ArgumentNullException ("parameters");
  270. return CreateContext (parameters).GetInnerProperty<T> ();
  271. }
  272. private void Initialize ()
  273. {
  274. IDefaultCommunicationTimeouts t =
  275. DefaultCommunicationTimeouts.Instance;
  276. open_timeout = t.OpenTimeout;
  277. close_timeout = t.CloseTimeout;
  278. receive_timeout = t.ReceiveTimeout;
  279. send_timeout = t.SendTimeout;
  280. }
  281. }
  282. }