Binding.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 {
  78. foreach (BindingElement e in CreateBindingElements ()) {
  79. MessageEncodingBindingElement me = e as MessageEncodingBindingElement;
  80. if (me != null)
  81. return me.MessageVersion;
  82. }
  83. return null;
  84. }
  85. }
  86. BindingContext CreateContext (
  87. BindingParameterCollection parameters)
  88. {
  89. // FIXME: it seems that binding elements are
  90. // "validated" so that the last item is a transport.
  91. return new BindingContext (
  92. new CustomBinding (this), parameters);
  93. }
  94. BindingContext CreateContext (
  95. Uri listenUriBaseAddress,
  96. string listenUriRelativeAddress,
  97. ListenUriMode listenUriMode,
  98. BindingParameterCollection parameters)
  99. {
  100. // FIXME: it seems that binding elements are
  101. // "validated" so that the last item is a transport.
  102. return new BindingContext (
  103. new CustomBinding (this),
  104. parameters,
  105. listenUriBaseAddress,
  106. listenUriRelativeAddress,
  107. listenUriMode);
  108. }
  109. public IChannelFactory<TChannel>
  110. BuildChannelFactory<TChannel> (
  111. params object [] parameters)
  112. {
  113. BindingParameterCollection pl =
  114. new BindingParameterCollection ();
  115. foreach (object o in parameters)
  116. pl.Add (o);
  117. return BuildChannelFactory<TChannel> (pl);
  118. }
  119. public virtual IChannelFactory<TChannel>
  120. BuildChannelFactory<TChannel> (
  121. BindingParameterCollection parameters)
  122. {
  123. if (parameters == null)
  124. throw new ArgumentNullException ("parameters");
  125. return CreateContext (parameters).BuildInnerChannelFactory<TChannel> ();
  126. }
  127. public virtual IChannelListener<TChannel>
  128. BuildChannelListener<TChannel> (
  129. Uri listenUriBaseAddress,
  130. string listenUriRelativeAddress,
  131. ListenUriMode listenUriMode,
  132. params object [] parameters)
  133. where TChannel : class, IChannel
  134. {
  135. BindingParameterCollection pl =
  136. new BindingParameterCollection ();
  137. foreach (object o in parameters)
  138. pl.Add (o);
  139. return BuildChannelListener<TChannel> (
  140. listenUriBaseAddress,
  141. listenUriRelativeAddress,
  142. listenUriMode,
  143. pl);
  144. }
  145. public virtual IChannelListener<TChannel>
  146. BuildChannelListener<TChannel> (
  147. Uri listenUriBaseAddress,
  148. string listenUriRelativeAddress,
  149. ListenUriMode listenUriMode,
  150. BindingParameterCollection parameters)
  151. where TChannel : class, IChannel
  152. {
  153. if (listenUriBaseAddress == null)
  154. throw new ArgumentNullException ("listenUriBaseAddress");
  155. if (listenUriRelativeAddress == null)
  156. throw new ArgumentNullException ("listenUriRelativeAddress");
  157. if (parameters == null)
  158. throw new ArgumentNullException ("parameters");
  159. BindingContext ctx = CreateContext (listenUriBaseAddress,
  160. listenUriRelativeAddress,
  161. listenUriMode,
  162. parameters);
  163. return ctx.BuildInnerChannelListener<TChannel> ();
  164. }
  165. public virtual IChannelListener<TChannel>
  166. BuildChannelListener<TChannel> (
  167. Uri listenUri,
  168. params object [] parameters)
  169. where TChannel : class, IChannel
  170. {
  171. BindingParameterCollection pl =
  172. new BindingParameterCollection ();
  173. foreach (object o in parameters)
  174. pl.Add (o);
  175. return BuildChannelListener<TChannel> (listenUri, pl);
  176. }
  177. public virtual IChannelListener<TChannel>
  178. BuildChannelListener<TChannel> (
  179. Uri listenUri,
  180. BindingParameterCollection parameters)
  181. where TChannel : class, IChannel
  182. {
  183. return BuildChannelListener<TChannel> (listenUri,
  184. String.Empty, parameters);
  185. }
  186. public virtual IChannelListener<TChannel>
  187. BuildChannelListener<TChannel> (
  188. Uri listenUriBaseAddress,
  189. string listenUriRelativeAddress,
  190. params object [] parameters)
  191. where TChannel : class, IChannel
  192. {
  193. BindingParameterCollection pl =
  194. new BindingParameterCollection ();
  195. foreach (object o in parameters)
  196. pl.Add (o);
  197. return BuildChannelListener<TChannel> (
  198. listenUriBaseAddress, listenUriRelativeAddress, pl);
  199. }
  200. public virtual IChannelListener<TChannel>
  201. BuildChannelListener<TChannel> (
  202. Uri listenUriBaseAddress,
  203. string listenUriRelativeAddress,
  204. BindingParameterCollection parameters)
  205. where TChannel : class, IChannel
  206. {
  207. return BuildChannelListener<TChannel> (
  208. listenUriBaseAddress,
  209. listenUriRelativeAddress,
  210. ListenUriMode.Explicit,
  211. parameters);
  212. }
  213. public virtual IChannelListener<TChannel>
  214. BuildChannelListener<TChannel> (
  215. params object [] parameters)
  216. where TChannel : class, IChannel
  217. {
  218. BindingParameterCollection pl =
  219. new BindingParameterCollection ();
  220. foreach (object o in parameters)
  221. pl.Add (o);
  222. return BuildChannelListener<TChannel> (pl);
  223. }
  224. public virtual IChannelListener<TChannel>
  225. BuildChannelListener<TChannel> (
  226. BindingParameterCollection parameters)
  227. where TChannel : class, IChannel
  228. {
  229. if (parameters == null)
  230. throw new ArgumentNullException ("parameters");
  231. return CreateContext (parameters).BuildInnerChannelListener<TChannel> ();
  232. }
  233. public bool CanBuildChannelFactory<TChannel> (
  234. params object [] parameters)
  235. {
  236. BindingParameterCollection pl =
  237. new BindingParameterCollection ();
  238. foreach (object o in parameters)
  239. pl.Add (o);
  240. return CanBuildChannelFactory<TChannel> (pl);
  241. }
  242. public virtual bool CanBuildChannelFactory<TChannel> (
  243. BindingParameterCollection parameters)
  244. {
  245. if (parameters == null)
  246. throw new ArgumentNullException ("parameters");
  247. return CreateContext (parameters).CanBuildInnerChannelFactory<TChannel> ();
  248. }
  249. public bool CanBuildChannelListener<TChannel> (
  250. params object [] parameters)
  251. where TChannel : class, IChannel
  252. {
  253. BindingParameterCollection pl =
  254. new BindingParameterCollection ();
  255. foreach (object o in parameters)
  256. pl.Add (o);
  257. return CanBuildChannelListener<TChannel> (pl);
  258. }
  259. public virtual bool CanBuildChannelListener<TChannel> (
  260. BindingParameterCollection parameters)
  261. where TChannel : class, IChannel
  262. {
  263. if (parameters == null)
  264. throw new ArgumentNullException ("parameters");
  265. return CreateContext (parameters).CanBuildInnerChannelListener<TChannel> ();
  266. }
  267. public abstract BindingElementCollection CreateBindingElements ();
  268. public T GetProperty<T> (BindingParameterCollection parameters)
  269. where T : class
  270. {
  271. if (parameters == null)
  272. throw new ArgumentNullException ("parameters");
  273. return CreateContext (parameters).GetInnerProperty<T> ();
  274. }
  275. private void Initialize ()
  276. {
  277. IDefaultCommunicationTimeouts t =
  278. DefaultCommunicationTimeouts.Instance;
  279. open_timeout = t.OpenTimeout;
  280. close_timeout = t.CloseTimeout;
  281. receive_timeout = t.ReceiveTimeout;
  282. send_timeout = t.SendTimeout;
  283. }
  284. }
  285. }