5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005 Novell, Inc. http://www.novell.com
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections.Generic;
30 using System.ServiceModel.Channels;
31 using System.ServiceModel.Description;
33 namespace System.ServiceModel.Channels
35 public abstract class Binding : IDefaultCommunicationTimeouts
38 TimeSpan open_timeout, close_timeout;
39 TimeSpan receive_timeout, send_timeout;
44 name = GetType ().Name;
45 ns = "http://tempuri.org/";
48 protected Binding (string name, string ns)
55 public TimeSpan CloseTimeout {
56 get { return close_timeout; }
57 set { close_timeout = value; }
60 public TimeSpan OpenTimeout {
61 get { return open_timeout; }
62 set { open_timeout = value; }
65 public TimeSpan ReceiveTimeout {
66 get { return receive_timeout; }
67 set { receive_timeout = value; }
70 public TimeSpan SendTimeout {
71 get { return send_timeout; }
72 set { send_timeout = value; }
80 public string Namespace {
85 public abstract string Scheme { get; }
87 public MessageVersion MessageVersion {
88 get { return GetProperty<MessageVersion> (new BindingParameterCollection ()); }
91 BindingContext CreateContext (
92 BindingParameterCollection parameters)
94 // FIXME: it seems that binding elements are
95 // "validated" so that the last item is a transport.
96 return new BindingContext (
97 new CustomBinding (this), parameters);
100 BindingContext CreateContext (
101 Uri listenUriBaseAddress,
102 string listenUriRelativeAddress,
103 ListenUriMode listenUriMode,
104 BindingParameterCollection parameters)
106 // FIXME: it seems that binding elements are
107 // "validated" so that the last item is a transport.
108 return new BindingContext (
109 new CustomBinding (this),
111 listenUriBaseAddress,
112 listenUriRelativeAddress,
116 public IChannelFactory<TChannel>
117 BuildChannelFactory<TChannel> (
118 params object [] parameters)
120 BindingParameterCollection pl =
121 new BindingParameterCollection ();
122 foreach (object o in parameters)
124 return BuildChannelFactory<TChannel> (pl);
127 public virtual IChannelFactory<TChannel>
128 BuildChannelFactory<TChannel> (
129 BindingParameterCollection parameters)
131 if (parameters == null)
132 throw new ArgumentNullException ("parameters");
133 return CreateContext (parameters).BuildInnerChannelFactory<TChannel> ();
137 public virtual IChannelListener<TChannel>
138 BuildChannelListener<TChannel> (
139 Uri listenUriBaseAddress,
140 string listenUriRelativeAddress,
141 ListenUriMode listenUriMode,
142 params object [] parameters)
143 where TChannel : class, IChannel
145 BindingParameterCollection pl =
146 new BindingParameterCollection ();
147 foreach (object o in parameters)
149 return BuildChannelListener<TChannel> (
150 listenUriBaseAddress,
151 listenUriRelativeAddress,
156 public virtual IChannelListener<TChannel>
157 BuildChannelListener<TChannel> (
158 Uri listenUriBaseAddress,
159 string listenUriRelativeAddress,
160 ListenUriMode listenUriMode,
161 BindingParameterCollection parameters)
162 where TChannel : class, IChannel
164 if (listenUriBaseAddress == null)
165 throw new ArgumentNullException ("listenUriBaseAddress");
166 if (listenUriRelativeAddress == null)
167 throw new ArgumentNullException ("listenUriRelativeAddress");
168 if (parameters == null)
169 throw new ArgumentNullException ("parameters");
170 BindingContext ctx = CreateContext (listenUriBaseAddress,
171 listenUriRelativeAddress,
174 return ctx.BuildInnerChannelListener<TChannel> ();
177 public virtual IChannelListener<TChannel>
178 BuildChannelListener<TChannel> (
179 Uri listenUriBaseAddress,
180 params object [] parameters)
181 where TChannel : class, IChannel
183 BindingParameterCollection pl =
184 new BindingParameterCollection ();
185 foreach (object o in parameters)
187 return BuildChannelListener<TChannel> (listenUriBaseAddress, pl);
190 public virtual IChannelListener<TChannel>
191 BuildChannelListener<TChannel> (
192 Uri listenUriBaseAddress,
193 BindingParameterCollection parameters)
194 where TChannel : class, IChannel
196 return BuildChannelListener<TChannel> (listenUriBaseAddress,
197 String.Empty, parameters);
200 public virtual IChannelListener<TChannel>
201 BuildChannelListener<TChannel> (
202 Uri listenUriBaseAddress,
203 string listenUriRelativeAddress,
204 params object [] parameters)
205 where TChannel : class, IChannel
207 BindingParameterCollection pl =
208 new BindingParameterCollection ();
209 foreach (object o in parameters)
211 return BuildChannelListener<TChannel> (
212 listenUriBaseAddress, listenUriRelativeAddress, pl);
215 public virtual IChannelListener<TChannel>
216 BuildChannelListener<TChannel> (
217 Uri listenUriBaseAddress,
218 string listenUriRelativeAddress,
219 BindingParameterCollection parameters)
220 where TChannel : class, IChannel
222 return BuildChannelListener<TChannel> (
223 listenUriBaseAddress,
224 listenUriRelativeAddress,
225 ListenUriMode.Explicit,
229 public virtual IChannelListener<TChannel>
230 BuildChannelListener<TChannel> (
231 params object [] parameters)
232 where TChannel : class, IChannel
234 BindingParameterCollection pl =
235 new BindingParameterCollection ();
236 foreach (object o in parameters)
238 return BuildChannelListener<TChannel> (pl);
241 public virtual IChannelListener<TChannel>
242 BuildChannelListener<TChannel> (
243 BindingParameterCollection parameters)
244 where TChannel : class, IChannel
246 if (parameters == null)
247 throw new ArgumentNullException ("parameters");
248 return CreateContext (parameters).BuildInnerChannelListener<TChannel> ();
252 public bool CanBuildChannelFactory<TChannel> (
253 params object [] parameters)
255 BindingParameterCollection pl =
256 new BindingParameterCollection ();
257 foreach (object o in parameters)
259 return CanBuildChannelFactory<TChannel> (pl);
262 public virtual bool CanBuildChannelFactory<TChannel> (
263 BindingParameterCollection parameters)
265 if (parameters == null)
266 throw new ArgumentNullException ("parameters");
267 return CreateContext (parameters).CanBuildInnerChannelFactory<TChannel> ();
271 public bool CanBuildChannelListener<TChannel> (
272 params object [] parameters)
273 where TChannel : class, IChannel
275 BindingParameterCollection pl =
276 new BindingParameterCollection ();
277 foreach (object o in parameters)
279 return CanBuildChannelListener<TChannel> (pl);
282 public virtual bool CanBuildChannelListener<TChannel> (
283 BindingParameterCollection parameters)
284 where TChannel : class, IChannel
286 if (parameters == null)
287 throw new ArgumentNullException ("parameters");
288 return CreateContext (parameters).CanBuildInnerChannelListener<TChannel> ();
292 public abstract BindingElementCollection CreateBindingElements ();
294 public T GetProperty<T> (BindingParameterCollection parameters)
297 if (parameters == null)
298 throw new ArgumentNullException ("parameters");
299 return CreateContext (parameters).GetInnerProperty<T> ();
302 private void Initialize ()
304 IDefaultCommunicationTimeouts t =
305 DefaultCommunicationTimeouts.Instance;
306 open_timeout = t.OpenTimeout;
307 close_timeout = t.CloseTimeout;
308 receive_timeout = t.ReceiveTimeout;
309 send_timeout = t.SendTimeout;