2 // generic ChannelFactory_1.cs
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.ObjectModel;
30 using System.Reflection;
31 using System.Runtime.Remoting;
32 using System.ServiceModel.Channels;
33 using System.ServiceModel.Description;
34 using System.ServiceModel.Dispatcher;
35 using System.ServiceModel.MonoInternal;
37 namespace System.ServiceModel
39 // LAMESPEC: TChannel should have been defined as "where TChannel : IClientChannel".
40 // The returned channel is actually used as IClientChannel.
41 // (That's also likely why the type parameter name is TChannel, not TContract.)
42 public class ChannelFactory<TChannel>
43 : ChannelFactory, IChannelFactory<TChannel>
45 public ChannelFactory ()
49 protected ChannelFactory (Type type)
52 throw new ArgumentNullException ("type");
53 if (!type.IsInterface)
54 throw new InvalidOperationException ("The type argument to the generic ChannelFactory constructor must be an interface type.");
56 InitializeEndpoint (CreateDescription ());
59 public ChannelFactory (string endpointConfigurationName)
61 if (endpointConfigurationName == null)
62 throw new ArgumentNullException ("endpointConfigurationName");
64 InitializeEndpoint (endpointConfigurationName, null);
67 public ChannelFactory (string endpointConfigurationName,
68 EndpointAddress remoteAddress)
70 if (endpointConfigurationName == null)
71 throw new ArgumentNullException ("endpointConfigurationName");
73 InitializeEndpoint (endpointConfigurationName, remoteAddress);
76 public ChannelFactory (ServiceEndpoint endpoint)
79 throw new ArgumentNullException ("serviceEndpoint");
81 InitializeEndpoint (endpoint);
84 public ChannelFactory (Binding binding, string remoteAddress)
85 : this (binding, new EndpointAddress (remoteAddress))
89 public ChannelFactory (Binding binding)
90 : this (binding, (EndpointAddress) null)
94 public ChannelFactory (Binding binding, EndpointAddress remoteAddress)
95 : this (typeof (TChannel))
98 throw new ArgumentNullException ();
100 Endpoint.Binding = binding;
101 Endpoint.Address = remoteAddress;
104 internal object OwnerClientBase { get; set; }
106 public TChannel CreateChannel ()
110 return CreateChannel (Endpoint.Address);
113 public TChannel CreateChannel (EndpointAddress address)
115 return CreateChannel (address, null);
118 static TChannel CreateChannelCore (ChannelFactory<TChannel> cf, Func<ChannelFactory<TChannel>, TChannel> f)
121 ((ICommunicationObject) (object) ch).Closed += delegate {
122 if (cf.State == CommunicationState.Opened)
128 public static TChannel CreateChannel (Binding binding, EndpointAddress address)
130 return CreateChannelCore (new ChannelFactory<TChannel> (binding, address), f => f.CreateChannel ());
133 public static TChannel CreateChannel (Binding binding, EndpointAddress address, Uri via)
135 return CreateChannelCore (new ChannelFactory<TChannel> (binding), f => f.CreateChannel (address, via));
138 public virtual TChannel CreateChannel (EndpointAddress address, Uri via)
141 throw new InvalidOperationException ("MonoTouch does not support dynamic proxy code generation. Override this method or its caller to return specific client proxy instance");
143 var existing = Endpoint.Address;
146 Endpoint.Address = address;
148 Endpoint.Validate ();
149 #if DISABLE_REAL_PROXY
150 Type type = ClientProxyGenerator.CreateProxyType (typeof (TChannel), Endpoint.Contract, false);
151 // in .NET and SL2, it seems that the proxy is RealProxy.
152 // But since there is no remoting in SL2 (and we have
153 // no special magic), we have to use different approach
154 // that should work either.
155 var proxy = (IClientChannel) Activator.CreateInstance (type, new object [] {Endpoint, this, address ?? Endpoint.Address, via});
157 var proxy = (IClientChannel) new ClientRealProxy (typeof (TChannel), new ClientRuntimeChannel (Endpoint, this, address ?? Endpoint.Address, via), false).GetTransparentProxy ();
159 proxy.Opened += delegate {
160 OpenedChannels.Add (proxy);
162 proxy.Closing += delegate {
163 OpenedChannels.Remove (proxy);
166 return (TChannel) proxy;
167 } catch (TargetInvocationException ex) {
168 if (ex.InnerException != null)
169 throw ex.InnerException;
173 Endpoint.Address = existing;
178 protected static TChannel CreateChannel (string endpointConfigurationName)
180 return CreateChannelCore (new ChannelFactory<TChannel> (endpointConfigurationName), f => f.CreateChannel ());
183 protected override ServiceEndpoint CreateDescription ()
185 ContractDescription cd = ContractDescription.GetContract (typeof (TChannel));
186 ServiceEndpoint ep = new ServiceEndpoint (cd);
187 ep.Behaviors.Add (new ClientCredentials ());
192 class DummyClientBase<T> : ClientBase<T> where T : class
194 public DummyClientBase (ChannelFactory<T> factory)