Binding.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. #if !NET_2_1
  128. public virtual IChannelListener<TChannel>
  129. BuildChannelListener<TChannel> (
  130. Uri listenUriBaseAddress,
  131. string listenUriRelativeAddress,
  132. ListenUriMode listenUriMode,
  133. params object [] parameters)
  134. where TChannel : class, IChannel
  135. {
  136. BindingParameterCollection pl =
  137. new BindingParameterCollection ();
  138. foreach (object o in parameters)
  139. pl.Add (o);
  140. return BuildChannelListener<TChannel> (
  141. listenUriBaseAddress,
  142. listenUriRelativeAddress,
  143. listenUriMode,
  144. pl);
  145. }
  146. public virtual IChannelListener<TChannel>
  147. BuildChannelListener<TChannel> (
  148. Uri listenUriBaseAddress,
  149. string listenUriRelativeAddress,
  150. ListenUriMode listenUriMode,
  151. BindingParameterCollection parameters)
  152. where TChannel : class, IChannel
  153. {
  154. if (listenUriBaseAddress == null)
  155. throw new ArgumentNullException ("listenUriBaseAddress");
  156. if (listenUriRelativeAddress == null)
  157. throw new ArgumentNullException ("listenUriRelativeAddress");
  158. if (parameters == null)
  159. throw new ArgumentNullException ("parameters");
  160. BindingContext ctx = CreateContext (listenUriBaseAddress,
  161. listenUriRelativeAddress,
  162. listenUriMode,
  163. parameters);
  164. return ctx.BuildInnerChannelListener<TChannel> ();
  165. }
  166. public virtual IChannelListener<TChannel>
  167. BuildChannelListener<TChannel> (
  168. Uri listenUri,
  169. params object [] parameters)
  170. where TChannel : class, IChannel
  171. {
  172. BindingParameterCollection pl =
  173. new BindingParameterCollection ();
  174. foreach (object o in parameters)
  175. pl.Add (o);
  176. return BuildChannelListener<TChannel> (listenUri, pl);
  177. }
  178. public virtual IChannelListener<TChannel>
  179. BuildChannelListener<TChannel> (
  180. Uri listenUri,
  181. BindingParameterCollection parameters)
  182. where TChannel : class, IChannel
  183. {
  184. return BuildChannelListener<TChannel> (listenUri,
  185. String.Empty, parameters);
  186. }
  187. public virtual IChannelListener<TChannel>
  188. BuildChannelListener<TChannel> (
  189. Uri listenUriBaseAddress,
  190. string listenUriRelativeAddress,
  191. params object [] parameters)
  192. where TChannel : class, IChannel
  193. {
  194. BindingParameterCollection pl =
  195. new BindingParameterCollection ();
  196. foreach (object o in parameters)
  197. pl.Add (o);
  198. return BuildChannelListener<TChannel> (
  199. listenUriBaseAddress, listenUriRelativeAddress, pl);
  200. }
  201. public virtual IChannelListener<TChannel>
  202. BuildChannelListener<TChannel> (
  203. Uri listenUriBaseAddress,
  204. string listenUriRelativeAddress,
  205. BindingParameterCollection parameters)
  206. where TChannel : class, IChannel
  207. {
  208. return BuildChannelListener<TChannel> (
  209. listenUriBaseAddress,
  210. listenUriRelativeAddress,
  211. ListenUriMode.Explicit,
  212. parameters);
  213. }
  214. public virtual IChannelListener<TChannel>
  215. BuildChannelListener<TChannel> (
  216. params object [] parameters)
  217. where TChannel : class, IChannel
  218. {
  219. BindingParameterCollection pl =
  220. new BindingParameterCollection ();
  221. foreach (object o in parameters)
  222. pl.Add (o);
  223. return BuildChannelListener<TChannel> (pl);
  224. }
  225. public virtual IChannelListener<TChannel>
  226. BuildChannelListener<TChannel> (
  227. BindingParameterCollection parameters)
  228. where TChannel : class, IChannel
  229. {
  230. if (parameters == null)
  231. throw new ArgumentNullException ("parameters");
  232. return CreateContext (parameters).BuildInnerChannelListener<TChannel> ();
  233. }
  234. #endif
  235. public bool CanBuildChannelFactory<TChannel> (
  236. params object [] parameters)
  237. {
  238. BindingParameterCollection pl =
  239. new BindingParameterCollection ();
  240. foreach (object o in parameters)
  241. pl.Add (o);
  242. return CanBuildChannelFactory<TChannel> (pl);
  243. }
  244. public virtual bool CanBuildChannelFactory<TChannel> (
  245. BindingParameterCollection parameters)
  246. {
  247. if (parameters == null)
  248. throw new ArgumentNullException ("parameters");
  249. return CreateContext (parameters).CanBuildInnerChannelFactory<TChannel> ();
  250. }
  251. #if !NET_2_1
  252. public bool CanBuildChannelListener<TChannel> (
  253. params object [] parameters)
  254. where TChannel : class, IChannel
  255. {
  256. BindingParameterCollection pl =
  257. new BindingParameterCollection ();
  258. foreach (object o in parameters)
  259. pl.Add (o);
  260. return CanBuildChannelListener<TChannel> (pl);
  261. }
  262. public virtual bool CanBuildChannelListener<TChannel> (
  263. BindingParameterCollection parameters)
  264. where TChannel : class, IChannel
  265. {
  266. if (parameters == null)
  267. throw new ArgumentNullException ("parameters");
  268. return CreateContext (parameters).CanBuildInnerChannelListener<TChannel> ();
  269. }
  270. #endif
  271. public abstract BindingElementCollection CreateBindingElements ();
  272. public T GetProperty<T> (BindingParameterCollection parameters)
  273. where T : class
  274. {
  275. if (parameters == null)
  276. throw new ArgumentNullException ("parameters");
  277. return CreateContext (parameters).GetInnerProperty<T> ();
  278. }
  279. private void Initialize ()
  280. {
  281. IDefaultCommunicationTimeouts t =
  282. DefaultCommunicationTimeouts.Instance;
  283. open_timeout = t.OpenTimeout;
  284. close_timeout = t.CloseTimeout;
  285. receive_timeout = t.ReceiveTimeout;
  286. send_timeout = t.SendTimeout;
  287. }
  288. }
  289. }