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 {
89 foreach (BindingElement e in CreateBindingElements ()) {
90 MessageEncodingBindingElement me = e as MessageEncodingBindingElement;
92 return me.MessageVersion;
98 BindingContext CreateContext (
99 BindingParameterCollection parameters)
101 // FIXME: it seems that binding elements are
102 // "validated" so that the last item is a transport.
103 return new BindingContext (
104 new CustomBinding (this), parameters);
107 BindingContext CreateContext (
108 Uri listenUriBaseAddress,
109 string listenUriRelativeAddress,
110 ListenUriMode listenUriMode,
111 BindingParameterCollection parameters)
113 // FIXME: it seems that binding elements are
114 // "validated" so that the last item is a transport.
115 return new BindingContext (
116 new CustomBinding (this),
118 listenUriBaseAddress,
119 listenUriRelativeAddress,
123 public IChannelFactory<TChannel>
124 BuildChannelFactory<TChannel> (
125 params object [] parameters)
127 BindingParameterCollection pl =
128 new BindingParameterCollection ();
129 foreach (object o in parameters)
131 return BuildChannelFactory<TChannel> (pl);
134 public virtual IChannelFactory<TChannel>
135 BuildChannelFactory<TChannel> (
136 BindingParameterCollection parameters)
138 if (parameters == null)
139 throw new ArgumentNullException ("parameters");
140 return CreateContext (parameters).BuildInnerChannelFactory<TChannel> ();
144 public virtual IChannelListener<TChannel>
145 BuildChannelListener<TChannel> (
146 Uri listenUriBaseAddress,
147 string listenUriRelativeAddress,
148 ListenUriMode listenUriMode,
149 params object [] parameters)
150 where TChannel : class, IChannel
152 BindingParameterCollection pl =
153 new BindingParameterCollection ();
154 foreach (object o in parameters)
156 return BuildChannelListener<TChannel> (
157 listenUriBaseAddress,
158 listenUriRelativeAddress,
163 public virtual IChannelListener<TChannel>
164 BuildChannelListener<TChannel> (
165 Uri listenUriBaseAddress,
166 string listenUriRelativeAddress,
167 ListenUriMode listenUriMode,
168 BindingParameterCollection parameters)
169 where TChannel : class, IChannel
171 if (listenUriBaseAddress == null)
172 throw new ArgumentNullException ("listenUriBaseAddress");
173 if (listenUriRelativeAddress == null)
174 throw new ArgumentNullException ("listenUriRelativeAddress");
175 if (parameters == null)
176 throw new ArgumentNullException ("parameters");
177 BindingContext ctx = CreateContext (listenUriBaseAddress,
178 listenUriRelativeAddress,
181 return ctx.BuildInnerChannelListener<TChannel> ();
184 public virtual IChannelListener<TChannel>
185 BuildChannelListener<TChannel> (
187 params object [] parameters)
188 where TChannel : class, IChannel
190 BindingParameterCollection pl =
191 new BindingParameterCollection ();
192 foreach (object o in parameters)
194 return BuildChannelListener<TChannel> (listenUri, pl);
197 public virtual IChannelListener<TChannel>
198 BuildChannelListener<TChannel> (
200 BindingParameterCollection parameters)
201 where TChannel : class, IChannel
203 return BuildChannelListener<TChannel> (listenUri,
204 String.Empty, parameters);
207 public virtual IChannelListener<TChannel>
208 BuildChannelListener<TChannel> (
209 Uri listenUriBaseAddress,
210 string listenUriRelativeAddress,
211 params object [] parameters)
212 where TChannel : class, IChannel
214 BindingParameterCollection pl =
215 new BindingParameterCollection ();
216 foreach (object o in parameters)
218 return BuildChannelListener<TChannel> (
219 listenUriBaseAddress, listenUriRelativeAddress, pl);
222 public virtual IChannelListener<TChannel>
223 BuildChannelListener<TChannel> (
224 Uri listenUriBaseAddress,
225 string listenUriRelativeAddress,
226 BindingParameterCollection parameters)
227 where TChannel : class, IChannel
229 return BuildChannelListener<TChannel> (
230 listenUriBaseAddress,
231 listenUriRelativeAddress,
232 ListenUriMode.Explicit,
236 public virtual IChannelListener<TChannel>
237 BuildChannelListener<TChannel> (
238 params object [] parameters)
239 where TChannel : class, IChannel
241 BindingParameterCollection pl =
242 new BindingParameterCollection ();
243 foreach (object o in parameters)
245 return BuildChannelListener<TChannel> (pl);
248 public virtual IChannelListener<TChannel>
249 BuildChannelListener<TChannel> (
250 BindingParameterCollection parameters)
251 where TChannel : class, IChannel
253 if (parameters == null)
254 throw new ArgumentNullException ("parameters");
255 return CreateContext (parameters).BuildInnerChannelListener<TChannel> ();
259 public bool CanBuildChannelFactory<TChannel> (
260 params object [] parameters)
262 BindingParameterCollection pl =
263 new BindingParameterCollection ();
264 foreach (object o in parameters)
266 return CanBuildChannelFactory<TChannel> (pl);
269 public virtual bool CanBuildChannelFactory<TChannel> (
270 BindingParameterCollection parameters)
272 if (parameters == null)
273 throw new ArgumentNullException ("parameters");
274 return CreateContext (parameters).CanBuildInnerChannelFactory<TChannel> ();
278 public bool CanBuildChannelListener<TChannel> (
279 params object [] parameters)
280 where TChannel : class, IChannel
282 BindingParameterCollection pl =
283 new BindingParameterCollection ();
284 foreach (object o in parameters)
286 return CanBuildChannelListener<TChannel> (pl);
289 public virtual bool CanBuildChannelListener<TChannel> (
290 BindingParameterCollection parameters)
291 where TChannel : class, IChannel
293 if (parameters == null)
294 throw new ArgumentNullException ("parameters");
295 return CreateContext (parameters).CanBuildInnerChannelListener<TChannel> ();
299 public abstract BindingElementCollection CreateBindingElements ();
301 public T GetProperty<T> (BindingParameterCollection parameters)
304 if (parameters == null)
305 throw new ArgumentNullException ("parameters");
306 return CreateContext (parameters).GetInnerProperty<T> ();
309 private void Initialize ()
311 IDefaultCommunicationTimeouts t =
312 DefaultCommunicationTimeouts.Instance;
313 open_timeout = t.OpenTimeout;
314 close_timeout = t.CloseTimeout;
315 receive_timeout = t.ReceiveTimeout;
316 send_timeout = t.SendTimeout;