2 // Author: Atsushi Enomoto <atsushi@ximian.com>
4 // Copyright (C) 2009,2010 Novell, Inc (http://www.novell.com)
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sublicense, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 using System.Collections.Generic;
27 using System.Collections.ObjectModel;
28 using System.ComponentModel;
29 using System.ServiceModel;
30 using System.ServiceModel.Channels;
31 using System.ServiceModel.Description;
32 using System.ServiceModel.Dispatcher;
34 namespace System.ServiceModel.Discovery
36 public sealed class DiscoveryClient : ICommunicationObject, IDisposable
38 internal interface IDiscoveryCommon
40 IAsyncResult BeginFind (FindCriteria criteria, AsyncCallback callback, object state);
41 FindResponse EndFind (IAsyncResult result);
42 IAsyncResult BeginResolve (ResolveCriteria criteria, AsyncCallback callback, object state);
43 ResolveResponse EndResolve (IAsyncResult result);
46 public DiscoveryClient ()
51 public DiscoveryClient (DiscoveryEndpoint discoveryEndpoint)
53 if (discoveryEndpoint == null)
54 throw new ArgumentNullException ("discoveryEndpoint");
56 client = Activator.CreateInstance (discoveryEndpoint.DiscoveryVersion.DiscoveryProxyClientType, new object [] {discoveryEndpoint});
59 public DiscoveryClient (string endpointConfigurationName)
61 throw new NotImplementedException ();
64 // FIXME: make it dynamic (dmcs crashes for now)
67 public ChannelFactory ChannelFactory {
68 get { return (ChannelFactory) client.GetType ().GetProperty ("ChannelFactory").GetValue (client, null); }
71 public ClientCredentials ClientCredentials {
72 get { return ChannelFactory.Credentials; }
75 public ServiceEndpoint Endpoint {
76 get { return ChannelFactory.Endpoint; }
79 public IClientChannel InnerChannel {
80 get { return (IClientChannel) client.GetType ().GetProperty ("InnerChannel").GetValue (client, null); }
83 CommunicationState State {
84 get { return ((ICommunicationObject) this).State; }
87 CommunicationState ICommunicationObject.State {
88 get { return ((ICommunicationObject) client).State; }
91 public event EventHandler<FindCompletedEventArgs> FindCompleted;
92 public event EventHandler<FindProgressChangedEventArgs> FindProgressChanged;
93 public event EventHandler<AnnouncementEventArgs> ProxyAvailable;
94 public event EventHandler<ResolveCompletedEventArgs> ResolveCompleted;
96 event EventHandler ICommunicationObject.Closed {
97 add { InnerChannel.Closed += value; }
98 remove { InnerChannel.Closed -= value; }
100 event EventHandler ICommunicationObject.Closing {
101 add { InnerChannel.Closing += value; }
102 remove { InnerChannel.Closing -= value; }
104 event EventHandler ICommunicationObject.Faulted {
105 add { InnerChannel.Faulted += value; }
106 remove { InnerChannel.Faulted -= value; }
108 event EventHandler ICommunicationObject.Opened {
109 add { InnerChannel.Opened += value; }
110 remove { InnerChannel.Opened -= value; }
112 event EventHandler ICommunicationObject.Opening {
113 add { InnerChannel.Opening += value; }
114 remove { InnerChannel.Opening -= value; }
119 ((ICommunicationObject) this).Open ();
124 ((ICommunicationObject) this).Close ();
129 public void CancelAsync (object userState)
131 throw new NotImplementedException ();
136 public FindResponse Find (FindCriteria criteria)
138 return EndFind (BeginFind (criteria, null, null));
141 public void FindAsync (FindCriteria criteria)
143 FindAsync (criteria, null);
146 public void FindAsync (FindCriteria criteria, object userState)
148 AsyncCallback cb = delegate (IAsyncResult result) {
149 FindResponse ret = null;
150 Exception error = null;
152 ret = EndFind (result);
153 } catch (Exception ex) {
156 OnFindCompleted (new FindCompletedEventArgs (ret, error, cancelled, result.AsyncState));
159 BeginFind (criteria, cb, userState);
162 void OnFindCompleted (FindCompletedEventArgs args)
164 if (FindCompleted != null)
165 FindCompleted (this, args);
168 IAsyncResult BeginFind (FindCriteria criteria, AsyncCallback callback, object state)
170 return ((IDiscoveryCommon) client).BeginFind (criteria, callback, state);
173 FindResponse EndFind (IAsyncResult result)
175 return ((IDiscoveryCommon) client).EndFind (result);
180 public ResolveResponse Resolve (ResolveCriteria criteria)
182 return EndResolve (BeginResolve (criteria, null, null));
185 public void ResolveAsync (ResolveCriteria criteria)
187 ResolveAsync (criteria, null);
190 public void ResolveAsync (ResolveCriteria criteria, object userState)
192 AsyncCallback cb = delegate (IAsyncResult result) {
193 ResolveResponse ret = null;
194 Exception error = null;
196 ret = EndResolve (result);
197 } catch (Exception ex) {
200 OnResolveCompleted (new ResolveCompletedEventArgs (ret, error, cancelled, result.AsyncState));
203 BeginResolve (criteria, cb, userState);
206 void OnResolveCompleted (ResolveCompletedEventArgs args)
208 if (ResolveCompleted != null)
209 ResolveCompleted (this, args);
212 IAsyncResult BeginResolve (ResolveCriteria criteria, AsyncCallback callback, object state)
214 return ((IDiscoveryCommon) client).BeginResolve (criteria, callback, state);
217 ResolveResponse EndResolve (IAsyncResult result)
219 return ((IDiscoveryCommon) client).EndResolve (result);
222 // explicit interface impl.
224 void ICommunicationObject.Open ()
226 InnerChannel.Open ();
229 void ICommunicationObject.Open (TimeSpan timeout)
231 InnerChannel.Open (timeout);
234 void ICommunicationObject.Close ()
236 InnerChannel.Close ();
239 void ICommunicationObject.Close (TimeSpan timeout)
241 InnerChannel.Close (timeout);
244 IAsyncResult ICommunicationObject.BeginOpen (AsyncCallback callback, object state)
246 return InnerChannel.BeginOpen (callback, state);
249 IAsyncResult ICommunicationObject.BeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
251 return InnerChannel.BeginOpen (timeout, callback, state);
254 IAsyncResult ICommunicationObject.BeginClose (AsyncCallback callback, object state)
256 return InnerChannel.BeginClose (callback, state);
259 IAsyncResult ICommunicationObject.BeginClose (TimeSpan timeout, AsyncCallback callback, object state)
261 return InnerChannel.BeginClose (timeout, callback, state);
264 void ICommunicationObject.EndOpen (IAsyncResult result)
266 InnerChannel.EndOpen (result);
269 void ICommunicationObject.EndClose (IAsyncResult result)
271 InnerChannel.EndClose (result);
274 void ICommunicationObject.Abort ()
276 InnerChannel.Abort ();
279 void IDisposable.Dispose ()
281 InnerChannel.Dispose ();