2008-11-01 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel / ClientBase.cs
1 //
2 // generic ClientBase.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005-2006 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 using System.ServiceModel.Dispatcher;
33
34 namespace System.ServiceModel
35 {
36         [MonoTODO ("It somehow rejects classes, but dunno how we can do that besides our code level.")]
37         public abstract class ClientBase<TChannel>
38                 : IDisposable, ICommunicationObject
39         {
40                 static InstanceContext initialContxt = new InstanceContext (null);
41
42                 ChannelFactory<TChannel> factory;
43                 ClientRuntimeChannel inner_channel;
44                 CommunicationState state;
45
46                 protected ClientBase ()
47                         : this (initialContxt)
48                 {
49                 }
50
51                 protected ClientBase (string configname)
52                         : this (initialContxt, configname)
53                 {
54                 }
55
56                 protected ClientBase (Binding binding, EndpointAddress remoteAddress)
57                         : this (initialContxt, binding, remoteAddress)
58                 {
59                 }
60
61                 protected ClientBase (string configname, EndpointAddress remoteAddress)
62                         : this (initialContxt, configname, remoteAddress)
63                 {
64                 }
65
66                 protected ClientBase (string configname, string remoteAddress)
67                         : this (initialContxt, configname, remoteAddress)
68                 {
69                 }
70
71                 protected ClientBase (InstanceContext instance)
72                         : this (instance, "*")
73                 {
74                 }
75
76                 protected ClientBase (InstanceContext instance, string configname)
77                 {
78                         if (instance == null)
79                                 throw new ArgumentNullException ("instanceContext");
80                         if (configname == null)
81                                 throw new ArgumentNullException ("configurationName");
82
83                         Initialize (instance, configname, null);
84                 }
85
86                 protected ClientBase (InstanceContext instance,
87                         string configname, EndpointAddress remoteAddress)
88                 {
89                         if (instance == null)
90                                 throw new ArgumentNullException ("instanceContext");
91                         if (configname == null)
92                                 throw new ArgumentNullException ("configurationName");
93                         if (remoteAddress == null)
94                                 throw new ArgumentNullException ("remoteAddress");
95
96                         Initialize (instance, configname, remoteAddress);
97                 }
98
99                 protected ClientBase (InstanceContext instance,
100                         string configname, string remoteAddress)
101                 {
102                         if (instance == null)
103                                 throw new ArgumentNullException ("instanceContext");
104                         if (remoteAddress == null)
105                                 throw new ArgumentNullException ("endpointAddress");
106                         if (configname == null)
107                                 throw new ArgumentNullException ("configurationname");
108
109                         Initialize (instance, configname, new EndpointAddress (remoteAddress));
110                 }
111
112                 protected ClientBase (InstanceContext instance,
113                         Binding binding, EndpointAddress remoteAddress)
114                 {
115                         if (instance == null)
116                                 throw new ArgumentNullException ("instanceContext");
117                         if (binding == null)
118                                 throw new ArgumentNullException ("binding");
119                         if (remoteAddress == null)
120                                 throw new ArgumentNullException ("remoteAddress");
121
122                         Initialize (instance, binding, remoteAddress);
123                 }
124
125                 void Initialize (InstanceContext instance,
126                         string configName, EndpointAddress remoteAddress)
127                 {
128                         factory = new ChannelFactory<TChannel> (configName, remoteAddress);
129                 }
130
131                 void Initialize (InstanceContext instance,
132                         Binding binding, EndpointAddress remoteAddress)
133                 {
134                         factory = new ChannelFactory<TChannel> (binding, remoteAddress);
135                 }
136
137                 public ChannelFactory<TChannel> ChannelFactory {
138                         get { return factory; }
139                 }
140
141 #if !NET_2_1
142                 public ClientCredentials ClientCredentials {
143                         get { return ChannelFactory.Credentials; }
144                 }
145 #endif
146
147                 public ServiceEndpoint Endpoint {
148                         get { return factory.Endpoint; }
149                 }
150
151                 public IClientChannel InnerChannel {
152                         get {
153                                 if (inner_channel == null)
154                                         inner_channel = (ClientRuntimeChannel) (object) factory.CreateChannel ();
155                                 return inner_channel;
156                         }
157                 }
158
159                 protected TChannel Channel {
160                         get { return (TChannel) (object) InnerChannel; }
161                 }
162
163                 public CommunicationState State {
164                         get { return InnerChannel.State; }
165                 }
166
167                 [MonoTODO]
168                 public void Abort ()
169                 {
170                         InnerChannel.Abort ();
171                 }
172
173                 [MonoTODO]
174                 public void Close ()
175                 {
176                         InnerChannel.Close ();
177                 }
178
179                 [MonoTODO]
180                 public void DisplayInitializationUI ()
181                 {
182                 }
183
184                 [MonoTODO]
185                 void IDisposable.Dispose ()
186                 {
187                         Close ();
188                 }
189
190                 protected virtual TChannel CreateChannel ()
191                 {
192                         return ChannelFactory.CreateChannel ();
193                 }
194
195                 public void Open ()
196                 {
197                         InnerChannel.Open ();
198                 }
199
200                 #region ICommunicationObject implementation
201
202                 [MonoTODO]
203                 IAsyncResult ICommunicationObject.BeginOpen (
204                         AsyncCallback callback, object state)
205                 {
206                         return InnerChannel.BeginOpen (callback, state);
207                 }
208
209                 [MonoTODO]
210                 IAsyncResult ICommunicationObject.BeginOpen (
211                         TimeSpan timeout, AsyncCallback callback, object state)
212                 {
213                         return InnerChannel.BeginOpen (timeout, callback, state);
214                 }
215
216                 [MonoTODO]
217                 void ICommunicationObject.EndOpen (IAsyncResult result)
218                 {
219                         InnerChannel.EndOpen (result);
220                 }
221
222                 [MonoTODO]
223                 IAsyncResult ICommunicationObject.BeginClose (
224                         AsyncCallback callback, object state)
225                 {
226                         return InnerChannel.BeginClose (callback, state);
227                 }
228
229                 [MonoTODO]
230                 IAsyncResult ICommunicationObject.BeginClose (
231                         TimeSpan timeout, AsyncCallback callback, object state)
232                 {
233                         return InnerChannel.BeginClose (timeout, callback, state);
234                 }
235
236                 [MonoTODO]
237                 void ICommunicationObject.EndClose (IAsyncResult result)
238                 {
239                         InnerChannel.EndClose (result);
240                 }
241
242                 [MonoTODO]
243                 void ICommunicationObject.Close (TimeSpan timeout)
244                 {
245                         InnerChannel.Close (timeout);
246                 }
247
248                 [MonoTODO]
249                 void ICommunicationObject.Open (TimeSpan timeout)
250                 {
251                         InnerChannel.Open (timeout);
252                 }
253
254                 event EventHandler ICommunicationObject.Opening {
255                         add { InnerChannel.Opening += value; }
256                         remove { InnerChannel.Opening -= value; }
257                 }
258                 event EventHandler ICommunicationObject.Opened {
259                         add { InnerChannel.Opened += value; }
260                         remove { InnerChannel.Opened -= value; }
261                 }
262                 event EventHandler ICommunicationObject.Closing {
263                         add { InnerChannel.Closing += value; }
264                         remove { InnerChannel.Closing -= value; }
265                 }
266                 event EventHandler ICommunicationObject.Closed {
267                         add { InnerChannel.Closed += value; }
268                         remove { InnerChannel.Closed -= value; }
269                 }
270                 event EventHandler ICommunicationObject.Faulted {
271                         add { InnerChannel.Faulted += value; }
272                         remove { InnerChannel.Faulted -= value; }
273                 }
274
275                 #endregion
276         }
277 }