Merge pull request #799 from kebby/master
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel / ChannelFactory_1.cs
1 //
2 // generic ChannelFactory_1.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.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;
36
37 namespace System.ServiceModel
38 {
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>
44         {
45                 public ChannelFactory ()
46                 {
47                 }
48
49                 protected ChannelFactory (Type type)
50                 {
51                         if (type == null)
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.");
55
56                         InitializeEndpoint (CreateDescription ());
57                 }
58
59                 public ChannelFactory (string endpointConfigurationName)
60                 {
61                         if (endpointConfigurationName == null)
62                                 throw new ArgumentNullException ("endpointConfigurationName");
63
64                         InitializeEndpoint (endpointConfigurationName, null);
65                 }
66
67                 public ChannelFactory (string endpointConfigurationName,
68                         EndpointAddress remoteAddress)
69                 {
70                         if (endpointConfigurationName == null)
71                                 throw new ArgumentNullException ("endpointConfigurationName");
72
73                         InitializeEndpoint (endpointConfigurationName, remoteAddress);
74                 }
75
76                 public ChannelFactory (ServiceEndpoint endpoint)
77                 {
78                         if (endpoint == null)
79                                 throw new ArgumentNullException ("serviceEndpoint");
80
81                         InitializeEndpoint (endpoint);
82                 }
83
84                 public ChannelFactory (Binding binding, string remoteAddress)
85                         : this (binding, new EndpointAddress (remoteAddress))
86                 {
87                 }
88
89                 public ChannelFactory (Binding binding)
90                         : this (binding, (EndpointAddress) null)
91                 {
92                 }
93
94                 public ChannelFactory (Binding binding, EndpointAddress remoteAddress)
95                         : this (typeof (TChannel))
96                 {
97                         if (binding == null)
98                                 throw new ArgumentNullException ();
99
100                         Endpoint.Binding = binding;
101                         Endpoint.Address = remoteAddress;
102                 }
103
104                 internal object OwnerClientBase { get; set; }
105
106                 public TChannel CreateChannel ()
107                 {
108                         EnsureOpened ();
109
110                         return CreateChannel (Endpoint.Address);
111                 }
112
113                 public TChannel CreateChannel (EndpointAddress address)
114                 {
115                         return CreateChannel (address, null);
116                 }
117
118                 static TChannel CreateChannelCore (ChannelFactory<TChannel> cf, Func<ChannelFactory<TChannel>, TChannel> f)
119                 {
120                         var ch = f (cf);
121                         ((ICommunicationObject) (object) ch).Closed += delegate {
122                                 if (cf.State == CommunicationState.Opened)
123                                         cf.Close ();
124                         };
125                         return ch;
126                 }
127
128                 public static TChannel CreateChannel (Binding binding, EndpointAddress address)
129                 {
130                         return CreateChannelCore (new ChannelFactory<TChannel> (binding, address), f => f.CreateChannel ());
131                 }
132
133                 public static TChannel CreateChannel (Binding binding, EndpointAddress address, Uri via)
134                 {
135                         return CreateChannelCore (new ChannelFactory<TChannel> (binding), f => f.CreateChannel (address, via));
136                 }
137
138                 public virtual TChannel CreateChannel (EndpointAddress address, Uri via)
139                 {
140 #if FULL_AOT_RUNTIME
141                         throw new InvalidOperationException ("MonoTouch does not support dynamic proxy code generation. Override this method or its caller to return specific client proxy instance");
142 #else
143                         var existing = Endpoint.Address;
144                         try {
145
146                         Endpoint.Address = address;
147                         EnsureOpened ();
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});
156 #else
157                         var proxy = (IClientChannel) new ClientRealProxy (typeof (TChannel), new ClientRuntimeChannel (Endpoint, this, address ?? Endpoint.Address, via), false).GetTransparentProxy ();
158 #endif
159                         proxy.Opened += delegate {
160                                 OpenedChannels.Add (proxy);
161                         };
162                         proxy.Closing += delegate {
163                                 OpenedChannels.Remove (proxy);
164                         };
165
166                         return (TChannel) proxy;
167                         } catch (TargetInvocationException ex) {
168                                 if (ex.InnerException != null)
169                                         throw ex.InnerException;
170                                 else
171                                         throw;
172                         } finally {
173                                 Endpoint.Address = existing;
174                         }
175 #endif
176                 }
177
178                 protected static TChannel CreateChannel (string endpointConfigurationName)
179                 {
180                         return CreateChannelCore (new ChannelFactory<TChannel> (endpointConfigurationName), f => f.CreateChannel ());
181                 }
182
183                 protected override ServiceEndpoint CreateDescription ()
184                 {
185                         ContractDescription cd = ContractDescription.GetContract (typeof (TChannel));
186                         ServiceEndpoint ep = new ServiceEndpoint (cd);
187                         ep.Behaviors.Add (new ClientCredentials ());
188                         return ep;
189                 }
190         }
191
192         class DummyClientBase<T> : ClientBase<T> where T : class
193         {
194                 public DummyClientBase (ChannelFactory<T> factory)
195                         : base (factory)
196                 {
197                 }
198         }
199 }