2010-06-21 Rodrigo Kumpera <rkumpera@novell.com>
[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.ServiceModel.Channels;
31 using System.ServiceModel.Description;
32 using System.ServiceModel.Dispatcher;
33
34 namespace System.ServiceModel
35 {
36         // LAMESPEC: TChannel should have been defined as "where TChannel : IClientChannel".
37         // The returned channel is actually used as IClientChannel.
38         // (That's also likely why the type parameter name is TChannel, not TContract.)
39         public class ChannelFactory<TChannel>
40                 : ChannelFactory, IChannelFactory<TChannel>
41         {
42                 public ChannelFactory ()
43                 {
44                 }
45
46                 protected ChannelFactory (Type type)
47                 {
48                         if (type == null)
49                                 throw new ArgumentNullException ("type");
50                         if (!type.IsInterface)
51                                 throw new InvalidOperationException ("The type argument to the generic ChannelFactory constructor must be an interface type.");
52
53                         InitializeEndpoint (CreateDescription ());
54                 }
55
56                 public ChannelFactory (string endpointConfigurationName)
57                 {
58                         if (endpointConfigurationName == null)
59                                 throw new ArgumentNullException ("endpointConfigurationName");
60
61                         InitializeEndpoint (endpointConfigurationName, null);
62                 }
63
64                 public ChannelFactory (string endpointConfigurationName,
65                         EndpointAddress remoteAddress)
66                 {
67                         if (endpointConfigurationName == null)
68                                 throw new ArgumentNullException ("endpointConfigurationName");
69
70                         InitializeEndpoint (endpointConfigurationName, remoteAddress);
71                 }
72
73                 public ChannelFactory (ServiceEndpoint endpoint)
74                 {
75                         if (endpoint == null)
76                                 throw new ArgumentNullException ("serviceEndpoint");
77
78                         InitializeEndpoint (endpoint);
79                 }
80
81                 public ChannelFactory (Binding binding, string remoteAddress)
82                         : this (binding, new EndpointAddress (remoteAddress))
83                 {
84                 }
85
86                 public ChannelFactory (Binding binding)
87                         : this (binding, (EndpointAddress) null)
88                 {
89                 }
90
91                 public ChannelFactory (Binding binding, EndpointAddress remoteAddress)
92                         : this (typeof (TChannel))
93                 {
94                         if (binding == null)
95                                 throw new ArgumentNullException ();
96
97                         Endpoint.Binding = binding;
98                         Endpoint.Address = remoteAddress;
99                 }
100
101                 internal object OwnerClientBase { get; set; }
102
103                 public TChannel CreateChannel ()
104                 {
105                         EnsureOpened ();
106
107                         return CreateChannel (Endpoint.Address);
108                 }
109
110                 public TChannel CreateChannel (EndpointAddress address)
111                 {
112                         return CreateChannel (address, null);
113                 }
114
115                 static TChannel CreateChannelCore (ChannelFactory<TChannel> cf, Func<ChannelFactory<TChannel>, TChannel> f)
116                 {
117                         var ch = f (cf);
118                         ((CommunicationObject) (object) ch).Closed += delegate { cf.Close (); };
119                         return ch;
120                 }
121
122                 public static TChannel CreateChannel (Binding binding, EndpointAddress address)
123                 {
124                         return CreateChannelCore (new ChannelFactory<TChannel> (binding, address), f => f.CreateChannel ());
125                 }
126
127                 public static TChannel CreateChannel (Binding binding, EndpointAddress address, Uri via)
128                 {
129                         return CreateChannelCore (new ChannelFactory<TChannel> (binding), f => f.CreateChannel (address, via));
130                 }
131
132                 public virtual TChannel CreateChannel (EndpointAddress address, Uri via)
133                 {
134 #if MONOTOUCH
135                         throw new InvalidOperationException ("MonoTouch does not support dynamic proxy code generation. Override this method or its caller to return specific client proxy instance");
136 #else
137                         var existing = Endpoint.Address;
138                         try {
139
140                         Endpoint.Address = address;
141                         EnsureOpened ();
142                         Endpoint.Validate ();
143                         Type type = ClientProxyGenerator.CreateProxyType (typeof (TChannel), Endpoint.Contract, false);
144                         // in .NET and SL2, it seems that the proxy is RealProxy.
145                         // But since there is no remoting in SL2 (and we have
146                         // no special magic), we have to use different approach
147                         // that should work either.
148                         object proxy = Activator.CreateInstance (type, new object [] {Endpoint, this, address ?? Endpoint.Address, via});
149                         return (TChannel) proxy;
150
151                         } finally {
152                                 Endpoint.Address = existing;
153                         }
154 #endif
155                 }
156
157                 protected static TChannel CreateChannel (string endpointConfigurationName)
158                 {
159                         return CreateChannelCore (new ChannelFactory<TChannel> (endpointConfigurationName), f => f.CreateChannel ());
160                 }
161
162                 protected override ServiceEndpoint CreateDescription ()
163                 {
164                         ContractDescription cd = ContractDescription.GetContract (typeof (TChannel));
165                         ServiceEndpoint ep = new ServiceEndpoint (cd);
166                         ep.Behaviors.Add (new ClientCredentials ());
167                         return ep;
168                 }
169         }
170
171         class DummyClientBase<T> : ClientBase<T> where T : class
172         {
173                 public DummyClientBase (ChannelFactory<T> factory)
174                         : base (factory)
175                 {
176                 }
177         }
178 }