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