Merge pull request #260 from pcc/topmost
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel / ChannelFactory.cs
1 //
2 // ChannelFactory.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.Generic;
30 using System.Collections.ObjectModel;
31 using System.ServiceModel.Channels;
32 using System.ServiceModel.Description;
33 using System.ServiceModel.Dispatcher;
34 using System.ServiceModel.Security;
35 using System.Configuration;
36 using System.ServiceModel.Configuration;
37 using System.Xml;
38
39 namespace System.ServiceModel
40 {
41         public abstract class ChannelFactory : CommunicationObject,
42                 IChannelFactory, ICommunicationObject, IDisposable
43         {
44                 // instance members
45
46                 ServiceEndpoint service_endpoint;
47                 IChannelFactory factory;
48                 List<IClientChannel> opened_channels = new List<IClientChannel> ();
49
50                 protected ChannelFactory ()
51                 {
52                 }
53
54                 internal IChannelFactory OpenedChannelFactory {
55                         get {
56                                 if (factory == null) {
57                                         factory = CreateFactory ();
58                                         factory.Open ();
59                                 }
60
61                                 return factory;
62                         }
63                         private set {
64                                 factory = value;
65                         }
66                 }
67
68                 internal List<IClientChannel> OpenedChannels {
69                         get { return opened_channels; }
70                 }
71
72                 public ServiceEndpoint Endpoint {
73                         get { return service_endpoint; }
74                 }
75
76                 public ClientCredentials Credentials {
77                         get { return Endpoint.Behaviors.Find<ClientCredentials> (); }
78                 }
79
80                 protected internal override TimeSpan DefaultCloseTimeout {
81                         get { return Endpoint.Binding.CloseTimeout; }
82                 }
83
84                 protected internal override TimeSpan DefaultOpenTimeout {
85                         get { return Endpoint.Binding.OpenTimeout; }
86                 }
87
88                 protected virtual void ApplyConfiguration (string endpointConfig)
89                 {
90                         if (endpointConfig == null)
91                                 return;
92
93 #if NET_2_1
94                         try {
95                                 // It should automatically use XmlXapResolver
96                                 var cfg = new SilverlightClientConfigLoader ().Load (XmlReader.Create ("ServiceReferences.ClientConfig"));
97
98                                 SilverlightClientConfigLoader.ServiceEndpointConfiguration se = null;
99                                 if (endpointConfig == "*")
100                                         se = cfg.GetServiceEndpointConfiguration (Endpoint.Contract.Name);
101                                 if (se == null)
102                                         se = cfg.GetServiceEndpointConfiguration (endpointConfig);
103
104                                 if (se.Binding != null && Endpoint.Binding == null)
105                                         Endpoint.Binding = se.Binding;
106                                 else // ignore it
107                                         Console.WriteLine ("WARNING: Configured binding not found in configuration {0}", endpointConfig);
108                                 if (se.Address != null && Endpoint.Address == null)
109                                         Endpoint.Address = se.Address;
110                                 else // ignore it
111                                         Console.WriteLine ("WARNING: Configured endpoint address not found in configuration {0}", endpointConfig);
112                         } catch (Exception) {
113                                 // ignore it.
114                                 Console.WriteLine ("WARNING: failed to load endpoint configuration for {0}", endpointConfig);
115                         }
116 #else
117
118                         string contractName = Endpoint.Contract.ConfigurationName;
119                         ClientSection client = ConfigUtil.ClientSection;
120                         ChannelEndpointElement endpoint = null;
121
122                         foreach (ChannelEndpointElement el in client.Endpoints) {
123                                 if (el.Contract == contractName && (endpointConfig == el.Name || endpointConfig == "*")) {
124                                         if (endpoint != null)
125                                                 throw new InvalidOperationException (String.Format ("More then one endpoint matching contract {0} was found.", contractName));
126                                         endpoint = el;
127                                 }
128                         }
129
130                         if (endpoint == null)
131                                 throw new InvalidOperationException (String.Format ("Client endpoint configuration '{0}' was not found in {1} endpoints.", endpointConfig, client.Endpoints.Count));
132
133 #if NET_4_0
134                         var binding = String.IsNullOrEmpty (endpoint.Binding) ? null : ConfigUtil.CreateBinding (endpoint.Binding, endpoint.BindingConfiguration);
135                         var contractType = ConfigUtil.GetTypeFromConfigString (endpoint.Contract, NamedConfigCategory.Contract);
136                         if (contractType == null)
137                                 throw new ArgumentException (String.Format ("Contract '{0}' was not found", endpoint.Contract));
138                         var contract = String.IsNullOrEmpty (endpoint.Contract) ? Endpoint.Contract : ContractDescription.GetContract (contractType);
139
140                         if (!String.IsNullOrEmpty (endpoint.Kind)) {
141                                 var se = ConfigUtil.ConfigureStandardEndpoint (contract, endpoint);
142                                 if (se.Binding == null)
143                                         se.Binding = binding;
144                                 if (se.Address == null && se.Binding != null) // standard endpoint might have empty address
145                                         se.Address = new EndpointAddress (endpoint.Address);
146                                 if (se.Binding == null && se.Address != null) // look for protocol mapping
147                                         se.Binding = ConfigUtil.GetBindingByProtocolMapping (se.Address.Uri);
148
149                                 service_endpoint = se;
150                         } else {
151                                 if (binding == null && endpoint.Address != null) // look for protocol mapping
152                                         Endpoint.Binding = ConfigUtil.GetBindingByProtocolMapping (endpoint.Address);
153                         }
154 #endif
155                         if (Endpoint.Binding == null)
156                                 Endpoint.Binding = ConfigUtil.CreateBinding (endpoint.Binding, endpoint.BindingConfiguration);
157                         if (Endpoint.Address == null)
158                                 Endpoint.Address = new EndpointAddress (endpoint.Address);
159
160                         if (endpoint.BehaviorConfiguration != "")
161                                 ApplyBehavior (endpoint.BehaviorConfiguration);
162 #endif
163                 }
164
165 #if !NET_2_1
166                 private void ApplyBehavior (string behaviorConfig)
167                 {
168                         BehaviorsSection behaviorsSection = ConfigUtil.BehaviorsSection;
169                         EndpointBehaviorElement behaviorElement = behaviorsSection.EndpointBehaviors [behaviorConfig];
170                         int i = 0;
171                         foreach (BehaviorExtensionElement el in behaviorElement) {
172                                 IEndpointBehavior behavior = (IEndpointBehavior) el.CreateBehavior ();
173                                 Endpoint.Behaviors.Remove (behavior.GetType ());
174                                 Endpoint.Behaviors.Add (behavior);
175                         }
176                 }
177 #endif
178
179                 protected virtual IChannelFactory CreateFactory ()
180                 {
181                         bool isOneWay = true; // check OperationDescription.IsOneWay
182                         foreach (var od in Endpoint.Contract.Operations)
183                                 if (!od.IsOneWay) {
184                                         isOneWay = false;
185                                         break;
186                                 }
187
188                         BindingParameterCollection pl = CreateBindingParameters ();
189
190                         // the assumption on the type of created channel could
191                         // be wrong, but would mostly fit the actual 
192                         // requirements. No books have explained how it is done.
193
194                         // try duplex
195                         switch (Endpoint.Contract.SessionMode) {
196                         case SessionMode.Required:
197                                 if (Endpoint.Binding.CanBuildChannelFactory<IDuplexSessionChannel> (pl))
198                                         return Endpoint.Binding.BuildChannelFactory<IDuplexSessionChannel> (pl);
199                                 break;
200                         case SessionMode.Allowed:
201                                 if (Endpoint.Binding.CanBuildChannelFactory<IDuplexChannel> (pl))
202                                         return Endpoint.Binding.BuildChannelFactory<IDuplexChannel> (pl);
203                                 if (Endpoint.Binding.CanBuildChannelFactory<IDuplexSessionChannel> (pl))
204                                         return Endpoint.Binding.BuildChannelFactory<IDuplexSessionChannel> (pl);
205                                 break;
206                         default:
207                                 if (Endpoint.Binding.CanBuildChannelFactory<IDuplexChannel> (pl))
208                                         return Endpoint.Binding.BuildChannelFactory<IDuplexChannel> (pl);
209                                 break;
210                         }
211
212                         if (Endpoint.Contract.CallbackContractType != null)
213                                 throw new InvalidOperationException ("The binding does not support duplex channel types that the contract requies for CallbackContractType.");
214
215                         if (isOneWay) {
216                                 switch (Endpoint.Contract.SessionMode) {
217                                 case SessionMode.Required:
218                                         if (Endpoint.Binding.CanBuildChannelFactory<IOutputSessionChannel> (pl))
219                                                 return Endpoint.Binding.BuildChannelFactory<IOutputSessionChannel> (pl);
220                                         if (Endpoint.Binding.CanBuildChannelFactory<IDuplexSessionChannel> (pl))
221                                                 return Endpoint.Binding.BuildChannelFactory<IDuplexSessionChannel> (pl);
222                                         break;
223                                 case SessionMode.Allowed:
224                                         if (Endpoint.Binding.CanBuildChannelFactory<IOutputChannel> (pl))
225                                                 return Endpoint.Binding.BuildChannelFactory<IOutputChannel> (pl);
226                                         if (Endpoint.Binding.CanBuildChannelFactory<IDuplexChannel> (pl))
227                                                 return Endpoint.Binding.BuildChannelFactory<IDuplexChannel> (pl);
228                                         goto case SessionMode.Required;
229                                 default:
230                                         if (Endpoint.Binding.CanBuildChannelFactory<IOutputChannel> (pl))
231                                                 return Endpoint.Binding.BuildChannelFactory<IOutputChannel> (pl);
232                                         if (Endpoint.Binding.CanBuildChannelFactory<IDuplexChannel> (pl))
233                                                 return Endpoint.Binding.BuildChannelFactory<IDuplexChannel> (pl);
234                                         break;
235                                 }
236                         }
237                         // both OneWay and non-OneWay contracts fall into here.
238                         {
239                                 switch (Endpoint.Contract.SessionMode) {
240                                 case SessionMode.Required:
241                                         if (Endpoint.Binding.CanBuildChannelFactory<IRequestSessionChannel> (pl))
242                                                 return Endpoint.Binding.BuildChannelFactory<IRequestSessionChannel> (pl);
243                                         break;
244                                 case SessionMode.Allowed:
245                                         if (Endpoint.Binding.CanBuildChannelFactory<IRequestChannel> (pl))
246                                                 return Endpoint.Binding.BuildChannelFactory<IRequestChannel> (pl);
247                                         if (Endpoint.Binding.CanBuildChannelFactory<IRequestSessionChannel> (pl))
248                                                 return Endpoint.Binding.BuildChannelFactory<IRequestSessionChannel> (pl);
249                                         break;
250                                 default:
251                                         if (Endpoint.Binding.CanBuildChannelFactory<IRequestChannel> (pl))
252                                                 return Endpoint.Binding.BuildChannelFactory<IRequestChannel> (pl);
253                                         break;
254                                 }
255                         }
256                         throw new InvalidOperationException (String.Format ("The binding does not support any of the channel types that the contract '{0}' allows.", Endpoint.Contract.Name));
257                 }
258
259                 BindingParameterCollection CreateBindingParameters ()
260                 {
261                         BindingParameterCollection pl =
262                                 new BindingParameterCollection ();
263
264                         ContractDescription cd = Endpoint.Contract;
265 #if !NET_2_1
266                         pl.Add (ChannelProtectionRequirements.CreateFromContract (cd));
267 #endif
268
269                         foreach (IEndpointBehavior behavior in Endpoint.Behaviors)
270                                 behavior.AddBindingParameters (Endpoint, pl);
271
272                         return pl;
273                 }
274
275                 protected abstract ServiceEndpoint CreateDescription ();
276
277                 void IDisposable.Dispose ()
278                 {
279                         Close ();
280                 }
281
282                 public T GetProperty<T> () where T : class
283                 {
284                         if (OpenedChannelFactory != null)
285                                 return OpenedChannelFactory.GetProperty<T> ();
286                         return null;
287                 }
288
289                 protected void EnsureOpened ()
290                 {
291                         if (Endpoint == null)
292                                 throw new InvalidOperationException ("A service endpoint must be configured for this channel factory");
293                         if (Endpoint.Contract == null)
294                                 throw new InvalidOperationException ("A service Contract must be configured for this channel factory");
295                         if (Endpoint.Binding == null)
296                                 throw new InvalidOperationException ("A Binding must be configured for this channel factory");
297
298                         if (State != CommunicationState.Opened)
299                                 Open ();
300                 }
301
302                 protected void InitializeEndpoint (
303                         string endpointConfigurationName,
304                         EndpointAddress remoteAddress)
305                 {
306                         InitializeEndpoint (CreateDescription ());
307                         if (remoteAddress != null)
308                                 service_endpoint.Address = remoteAddress;
309                         ApplyConfiguration (endpointConfigurationName);
310                 }
311
312                 protected void InitializeEndpoint (Binding binding,
313                         EndpointAddress remoteAddress)
314                 {
315                         InitializeEndpoint (CreateDescription ());
316                         if (binding != null)
317                                 service_endpoint.Binding = binding;
318                         if (remoteAddress != null)
319                                 service_endpoint.Address = remoteAddress;
320                 }
321
322                 protected void InitializeEndpoint (ServiceEndpoint endpoint)
323                 {
324                         if (endpoint == null)
325                                 throw new ArgumentNullException ("endpoint");
326                         service_endpoint = endpoint;
327                 }
328
329                 protected override void OnAbort ()
330                 {
331                         if (OpenedChannelFactory != null)
332                                 OpenedChannelFactory.Abort ();
333                 }
334
335                 Action<TimeSpan> close_delegate;
336                 Action<TimeSpan> open_delegate;
337
338
339                 protected override IAsyncResult OnBeginClose (
340                         TimeSpan timeout, AsyncCallback callback, object state)
341                 {
342                         if (close_delegate == null)
343                                 close_delegate = new Action<TimeSpan> (OnClose);
344                         return close_delegate.BeginInvoke (timeout, callback, state);
345                 }
346
347                 protected override IAsyncResult OnBeginOpen (
348                         TimeSpan timeout, AsyncCallback callback, object state)
349                 {
350                         if (open_delegate == null)
351                                 open_delegate = new Action<TimeSpan> (OnClose);
352                         return open_delegate.BeginInvoke (timeout, callback, state);
353                 }
354
355                 protected override void OnEndClose (IAsyncResult result)
356                 {
357                         if (close_delegate == null)
358                                 throw new InvalidOperationException ("Async close operation has not started");
359                         close_delegate.EndInvoke (result);
360                 }
361
362                 protected override void OnEndOpen (IAsyncResult result)
363                 {
364                         if (open_delegate == null)
365                                 throw new InvalidOperationException ("Async close operation has not started");
366                         open_delegate.EndInvoke (result);
367                 }
368
369                 protected override void OnClose (TimeSpan timeout)
370                 {
371                         DateTime start = DateTime.Now;
372                         foreach (var ch in opened_channels.ToArray ())
373                                 ch.Close (timeout - (DateTime.Now - start));
374                         if (OpenedChannelFactory != null)
375                                 OpenedChannelFactory.Close (timeout - (DateTime.Now - start));
376                 }
377
378                 protected override void OnOpen (TimeSpan timeout)
379                 {
380                 }
381
382                 protected override void OnOpening ()
383                 {
384                         base.OnOpening ();
385                         OpenedChannelFactory = CreateFactory ();
386                 }
387
388                 protected override void OnOpened ()
389                 {
390                         base.OnOpened ();
391                         OpenedChannelFactory.Open ();
392                 }
393         }
394
395 #if obsolete
396         [ServiceContract]
397         interface UninitializedContract
398         {
399                 [OperationContract]
400                 void ItShouldReallyGone ();
401         }
402 #endif
403 }