2 // generic ClientBase.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005-2006 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;
32 using System.ServiceModel.Dispatcher;
34 namespace System.ServiceModel
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
40 static InstanceContext initialContxt = new InstanceContext (null);
42 ChannelFactory<TChannel> factory;
43 ClientRuntimeChannel inner_channel;
44 CommunicationState state;
46 protected ClientBase ()
47 : this (initialContxt)
51 protected ClientBase (string configname)
52 : this (initialContxt, configname)
56 protected ClientBase (Binding binding, EndpointAddress remoteAddress)
57 : this (initialContxt, binding, remoteAddress)
61 protected ClientBase (string configname, EndpointAddress remoteAddress)
62 : this (initialContxt, configname, remoteAddress)
66 protected ClientBase (string configname, string remoteAddress)
67 : this (initialContxt, configname, remoteAddress)
71 protected ClientBase (InstanceContext instance)
72 : this (instance, "*")
76 protected ClientBase (InstanceContext instance, string configname)
79 throw new ArgumentNullException ("instanceContext");
80 if (configname == null)
81 throw new ArgumentNullException ("configurationName");
83 Initialize (instance, configname, null);
86 protected ClientBase (InstanceContext instance,
87 string configname, EndpointAddress remoteAddress)
90 throw new ArgumentNullException ("instanceContext");
91 if (configname == null)
92 throw new ArgumentNullException ("configurationName");
93 if (remoteAddress == null)
94 throw new ArgumentNullException ("remoteAddress");
96 Initialize (instance, configname, remoteAddress);
99 protected ClientBase (InstanceContext instance,
100 string configname, string remoteAddress)
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");
109 Initialize (instance, configname, new EndpointAddress (remoteAddress));
112 protected ClientBase (InstanceContext instance,
113 Binding binding, EndpointAddress remoteAddress)
115 if (instance == null)
116 throw new ArgumentNullException ("instanceContext");
118 throw new ArgumentNullException ("binding");
119 if (remoteAddress == null)
120 throw new ArgumentNullException ("remoteAddress");
122 Initialize (instance, binding, remoteAddress);
125 void Initialize (InstanceContext instance,
126 string configName, EndpointAddress remoteAddress)
128 factory = new ChannelFactory<TChannel> (configName, remoteAddress);
131 void Initialize (InstanceContext instance,
132 Binding binding, EndpointAddress remoteAddress)
134 factory = new ChannelFactory<TChannel> (binding, remoteAddress);
137 public ChannelFactory<TChannel> ChannelFactory {
138 get { return factory; }
142 public ClientCredentials ClientCredentials {
143 get { return ChannelFactory.Credentials; }
147 public ServiceEndpoint Endpoint {
148 get { return factory.Endpoint; }
151 public IClientChannel InnerChannel {
153 if (inner_channel == null)
154 inner_channel = (ClientRuntimeChannel) (object) factory.CreateChannel ();
155 return inner_channel;
159 protected TChannel Channel {
160 get { return (TChannel) (object) InnerChannel; }
163 public CommunicationState State {
164 get { return InnerChannel.State; }
170 InnerChannel.Abort ();
176 InnerChannel.Close ();
180 public void DisplayInitializationUI ()
185 void IDisposable.Dispose ()
190 protected virtual TChannel CreateChannel ()
192 return ChannelFactory.CreateChannel ();
197 InnerChannel.Open ();
200 #region ICommunicationObject implementation
203 IAsyncResult ICommunicationObject.BeginOpen (
204 AsyncCallback callback, object state)
206 return InnerChannel.BeginOpen (callback, state);
210 IAsyncResult ICommunicationObject.BeginOpen (
211 TimeSpan timeout, AsyncCallback callback, object state)
213 return InnerChannel.BeginOpen (timeout, callback, state);
217 void ICommunicationObject.EndOpen (IAsyncResult result)
219 InnerChannel.EndOpen (result);
223 IAsyncResult ICommunicationObject.BeginClose (
224 AsyncCallback callback, object state)
226 return InnerChannel.BeginClose (callback, state);
230 IAsyncResult ICommunicationObject.BeginClose (
231 TimeSpan timeout, AsyncCallback callback, object state)
233 return InnerChannel.BeginClose (timeout, callback, state);
237 void ICommunicationObject.EndClose (IAsyncResult result)
239 InnerChannel.EndClose (result);
243 void ICommunicationObject.Close (TimeSpan timeout)
245 InnerChannel.Close (timeout);
249 void ICommunicationObject.Open (TimeSpan timeout)
251 InnerChannel.Open (timeout);
254 event EventHandler ICommunicationObject.Opening {
255 add { InnerChannel.Opening += value; }
256 remove { InnerChannel.Opening -= value; }
258 event EventHandler ICommunicationObject.Opened {
259 add { InnerChannel.Opened += value; }
260 remove { InnerChannel.Opened -= value; }
262 event EventHandler ICommunicationObject.Closing {
263 add { InnerChannel.Closing += value; }
264 remove { InnerChannel.Closing -= value; }
266 event EventHandler ICommunicationObject.Closed {
267 add { InnerChannel.Closed += value; }
268 remove { InnerChannel.Closed -= value; }
270 event EventHandler ICommunicationObject.Faulted {
271 add { InnerChannel.Faulted += value; }
272 remove { InnerChannel.Faulted -= value; }