2009-12-04 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / Binding.cs
1 //
2 // Binding.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
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
33 namespace System.ServiceModel.Channels
34 {
35         public abstract class Binding : IDefaultCommunicationTimeouts
36         {
37                 string name, ns;
38                 TimeSpan open_timeout, close_timeout;
39                 TimeSpan receive_timeout, send_timeout;
40
41                 protected Binding ()
42                 {
43                         Initialize ();
44                         name = GetType ().Name;
45                         ns = "http://tempuri.org/";
46                 }
47
48                 protected Binding (string name, string ns)
49                 {
50                         this.name = name;
51                         this.ns = ns;
52                         Initialize ();
53                 }
54
55                 public TimeSpan CloseTimeout {
56                         get { return close_timeout; }
57                         set { close_timeout = value; }
58                 }
59
60                 public TimeSpan OpenTimeout {
61                         get { return open_timeout; }
62                         set { open_timeout = value; }
63                 }
64
65                 public TimeSpan ReceiveTimeout {
66                         get { return receive_timeout; }
67                         set { receive_timeout = value; }
68                 }
69
70                 public TimeSpan SendTimeout {
71                         get { return send_timeout; }
72                         set { send_timeout = value; }
73                 }
74
75                 public string Name {
76                         get { return name; }
77                         set { name = value; }
78                 }
79
80                 public string Namespace {
81                         get { return ns; }
82                         set { ns = value; }
83                 }
84
85                 public abstract string Scheme { get; }
86
87                 public MessageVersion MessageVersion {
88                         get {
89                                 foreach (BindingElement e in CreateBindingElements ()) {
90                                         MessageEncodingBindingElement me = e as MessageEncodingBindingElement;
91                                         if (me != null)
92                                                 return me.MessageVersion;
93                                 }
94                                 return null;
95                         }
96                 }
97
98                 BindingContext CreateContext (
99                         BindingParameterCollection parameters)
100                 {
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);
105                 }
106
107                 BindingContext CreateContext (
108                         Uri listenUriBaseAddress,
109                         string listenUriRelativeAddress,
110                         ListenUriMode listenUriMode,
111                         BindingParameterCollection parameters)
112                 {
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),
117                                 parameters,
118                                 listenUriBaseAddress,
119                                 listenUriRelativeAddress,
120                                 listenUriMode);
121                 }
122
123                 public IChannelFactory<TChannel>
124                         BuildChannelFactory<TChannel> (
125                         params object [] parameters)
126                 {
127                         BindingParameterCollection pl =
128                                 new BindingParameterCollection ();
129                         foreach (object o in parameters)
130                                 pl.Add (o);
131                         return BuildChannelFactory<TChannel> (pl);
132                 }
133
134                 public virtual IChannelFactory<TChannel>
135                         BuildChannelFactory<TChannel> (
136                         BindingParameterCollection parameters)
137                 {
138                         if (parameters == null)
139                                 throw new ArgumentNullException ("parameters");
140                         return CreateContext (parameters).BuildInnerChannelFactory<TChannel> ();
141                 }
142
143 #if !NET_2_1
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
151                 {
152                         BindingParameterCollection pl =
153                                 new BindingParameterCollection ();
154                         foreach (object o in parameters)
155                                 pl.Add (o);
156                         return BuildChannelListener<TChannel> (
157                                 listenUriBaseAddress,
158                                 listenUriRelativeAddress,
159                                 listenUriMode,
160                                 pl);
161                 }
162
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
170                 {
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,
179                                 listenUriMode,
180                                 parameters);
181                         return ctx.BuildInnerChannelListener<TChannel> ();
182                 }
183
184                 public virtual IChannelListener<TChannel>
185                         BuildChannelListener<TChannel> (
186                         Uri listenUri,
187                         params object [] parameters)
188                         where TChannel : class, IChannel
189                 {
190                         BindingParameterCollection pl =
191                                 new BindingParameterCollection ();
192                         foreach (object o in parameters)
193                                 pl.Add (o);
194                         return BuildChannelListener<TChannel> (listenUri, pl);
195                 }
196
197                 public virtual IChannelListener<TChannel>
198                         BuildChannelListener<TChannel> (
199                         Uri listenUri,
200                         BindingParameterCollection parameters)
201                         where TChannel : class, IChannel
202                 {
203                         return BuildChannelListener<TChannel> (listenUri,
204                                 String.Empty, parameters);
205                 }
206
207                 public virtual IChannelListener<TChannel>
208                         BuildChannelListener<TChannel> (
209                         Uri listenUriBaseAddress,
210                         string listenUriRelativeAddress,
211                         params object [] parameters)
212                         where TChannel : class, IChannel
213                 {
214                         BindingParameterCollection pl =
215                                 new BindingParameterCollection ();
216                         foreach (object o in parameters)
217                                 pl.Add (o);
218                         return BuildChannelListener<TChannel> (
219                                 listenUriBaseAddress, listenUriRelativeAddress, pl);
220                 }
221
222                 public virtual IChannelListener<TChannel>
223                         BuildChannelListener<TChannel> (
224                         Uri listenUriBaseAddress,
225                         string listenUriRelativeAddress,
226                         BindingParameterCollection parameters)
227                         where TChannel : class, IChannel
228                 {
229                         return BuildChannelListener<TChannel> (
230                                 listenUriBaseAddress,
231                                 listenUriRelativeAddress,
232                                 ListenUriMode.Explicit,
233                                 parameters);
234                 }
235
236                 public virtual IChannelListener<TChannel>
237                         BuildChannelListener<TChannel> (
238                         params object [] parameters)
239                         where TChannel : class, IChannel
240                 {
241                         BindingParameterCollection pl =
242                                 new BindingParameterCollection ();
243                         foreach (object o in parameters)
244                                 pl.Add (o);
245                         return BuildChannelListener<TChannel> (pl);
246                 }
247
248                 public virtual IChannelListener<TChannel>
249                         BuildChannelListener<TChannel> (
250                         BindingParameterCollection parameters)
251                         where TChannel : class, IChannel
252                 {
253                         if (parameters == null)
254                                 throw new ArgumentNullException ("parameters");
255                         return CreateContext (parameters).BuildInnerChannelListener<TChannel> ();
256                 }
257 #endif
258
259                 public bool CanBuildChannelFactory<TChannel> (
260                         params object [] parameters)
261                 {
262                         BindingParameterCollection pl =
263                                 new BindingParameterCollection ();
264                         foreach (object o in parameters)
265                                 pl.Add (o);
266                         return CanBuildChannelFactory<TChannel> (pl);
267                 }
268
269                 public virtual bool CanBuildChannelFactory<TChannel> (
270                         BindingParameterCollection parameters)
271                 {
272                         if (parameters == null)
273                                 throw new ArgumentNullException ("parameters");
274                         return CreateContext (parameters).CanBuildInnerChannelFactory<TChannel> ();
275                 }
276
277 #if !NET_2_1
278                 public bool CanBuildChannelListener<TChannel> (
279                         params object [] parameters)
280                         where TChannel : class, IChannel
281                 {
282                         BindingParameterCollection pl =
283                                 new BindingParameterCollection ();
284                         foreach (object o in parameters)
285                                 pl.Add (o);
286                         return CanBuildChannelListener<TChannel> (pl);
287                 }
288
289                 public virtual bool CanBuildChannelListener<TChannel> (
290                         BindingParameterCollection parameters)
291                         where TChannel : class, IChannel
292                 {
293                         if (parameters == null)
294                                 throw new ArgumentNullException ("parameters");
295                         return CreateContext (parameters).CanBuildInnerChannelListener<TChannel> ();
296                 }
297 #endif
298
299                 public abstract BindingElementCollection CreateBindingElements ();
300
301                 public T GetProperty<T> (BindingParameterCollection parameters)
302                         where T : class
303                 {
304                         if (parameters == null)
305                                 throw new ArgumentNullException ("parameters");
306                         return CreateContext (parameters).GetInnerProperty<T> ();
307                 }
308
309                 private void Initialize ()
310                 {
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;
317                 }
318         }
319 }