Binding.cs 9.0 KB

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