New test.
[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.Configuration;
34 using System.ServiceModel.Configuration;
35 using System.Xml;
36
37 namespace System.ServiceModel
38 {
39         [MonoTODO ("Actually it should work like existing ClientBase minus the impact of proxying. Separate TChannel from IChannel")]
40         public abstract class ChannelFactory : CommunicationObject,
41                 IChannelFactory, ICommunicationObject, IDisposable
42         {
43                 // instance members
44
45                 ServiceEndpoint service_endpoint;
46
47                 protected ChannelFactory ()
48                 {
49                 }
50
51                 public ServiceEndpoint Endpoint {
52                         get { return service_endpoint; }
53                 }
54
55 #if !NET_2_1
56                 public ClientCredentials Credentials {
57                         get { return Endpoint.Behaviors.Find<ClientCredentials> (); }
58                 }
59 #endif
60
61                 protected internal override TimeSpan DefaultCloseTimeout {
62                         get { return Endpoint.Binding.CloseTimeout; }
63                 }
64
65                 protected internal override TimeSpan DefaultOpenTimeout {
66                         get { return Endpoint.Binding.OpenTimeout; }
67                 }
68
69                 protected virtual void ApplyConfiguration (string endpointConfig)
70                 {
71                         if (endpointConfig == null)
72                                 return;
73
74 #if NET_2_1
75                         try {
76                                 // It should automatically use XmlXapResolver
77                                 var cfg = new SilverlightClientConfigLoader ().Load (XmlReader.Create ("ServiceReferences.ClientConfig"));
78                                 var se = cfg.GetServiceEndpointConfiguration (endpointConfig);
79                                 if (se.Binding != null && Endpoint.Binding == null)
80                                         Endpoint.Binding = se.Binding;
81                                 if (se.Address != null && Endpoint.Address == null)
82                                         Endpoint.Address = se.Address;
83                         } catch (Exception) {
84                                 // ignore it.
85                         }
86 #else
87
88                         string contractName = Endpoint.Contract.ConfigurationName;
89                         ClientSection client = (ClientSection) ConfigurationManager.GetSection ("system.serviceModel/client");
90                         ChannelEndpointElement res = null;
91                         foreach (ChannelEndpointElement el in client.Endpoints) {
92                                 if (el.Contract == contractName && (endpointConfig == el.Name || endpointConfig == "*")) {
93                                         if (res != null)
94                                                 throw new InvalidOperationException (String.Format ("More then one endpoint matching contract {0} was found.", contractName));
95                                         res = el;
96                                 }
97                         }
98
99                         if (res == null)
100                                 throw new InvalidOperationException (String.Format ("Client endpoint configuration '{0}' was not found in {1} endpoints.", endpointConfig, client.Endpoints.Count));
101
102                         if (Endpoint.Binding == null)
103                                 Endpoint.Binding = ConfigUtil.CreateBinding (res.Binding, res.BindingConfiguration);
104                         if (Endpoint.Address == null)
105                                 Endpoint.Address = new EndpointAddress (res.Address);
106
107                         if (res.BehaviorConfiguration != "")
108                                 ApplyBehavior (res.BehaviorConfiguration);
109 #endif
110                 }
111
112 #if !NET_2_1
113                 private void ApplyBehavior (string behaviorConfig)
114                 {
115                         BehaviorsSection behaviorsSection = (BehaviorsSection) ConfigurationManager.GetSection ("system.serviceModel/behaviors");
116                         EndpointBehaviorElement behaviorElement = behaviorsSection.EndpointBehaviors [behaviorConfig];
117                         int i = 0;
118                         foreach (BehaviorExtensionElement el in behaviorElement) {
119                                 IEndpointBehavior behavior = (IEndpointBehavior) el.CreateBehavior ();
120                                 Endpoint.Behaviors.Remove (behavior.GetType ());
121                                 Endpoint.Behaviors.Add (behavior);
122                         }
123                 }
124 #endif
125
126                 [MonoTODO]
127                 protected virtual IChannelFactory CreateFactory ()
128                 {
129                         // FIXME: 
130                         // This should be implemented to return IChannelFactory<IRequestChannel> etc.
131                         // ClientRuntimeChannel should not implement channel
132                         // creation by itself but should delegate it to this.
133                         throw new NotImplementedException ();
134                 }
135
136                 protected abstract ServiceEndpoint CreateDescription ();
137
138                 void IDisposable.Dispose ()
139                 {
140                         Close ();
141                 }
142
143                 [MonoTODO]
144                 public T GetProperty<T> () where T : class
145                 {
146                         throw new NotImplementedException ();
147                 }
148
149                 protected void EnsureOpened ()
150                 {
151                         if (State != CommunicationState.Opened)
152                                 Open ();
153                 }
154
155                 protected void InitializeEndpoint (
156                         string endpointConfigurationName,
157                         EndpointAddress remoteAddress)
158                 {
159                         InitializeEndpoint (CreateDescription ());
160                         service_endpoint.Address = remoteAddress;
161                         ApplyConfiguration (endpointConfigurationName);
162                 }
163
164                 protected void InitializeEndpoint (Binding binding,
165                         EndpointAddress remoteAddress)
166                 {
167                         InitializeEndpoint (CreateDescription ());
168                         service_endpoint.Binding = binding;
169                         service_endpoint.Address = remoteAddress;
170                 }
171
172                 protected void InitializeEndpoint (ServiceEndpoint endpoint)
173                 {
174                         service_endpoint = endpoint;
175                 }
176
177                 [MonoTODO]
178                 protected override void OnAbort ()
179                 {
180                         throw new NotImplementedException ();
181                 }
182
183                 [MonoTODO]
184                 protected override IAsyncResult OnBeginClose (
185                         TimeSpan timeout, AsyncCallback callback, object state)
186                 {
187                         throw new NotImplementedException ();
188                 }
189
190                 [MonoTODO]
191                 protected override IAsyncResult OnBeginOpen (
192                         TimeSpan timeout, AsyncCallback callback, object state)
193                 {
194                         throw new NotImplementedException ();
195                 }
196
197                 [MonoTODO]
198                 protected override void OnEndClose (IAsyncResult result)
199                 {
200                         throw new NotImplementedException ();
201                 }
202
203                 [MonoTODO]
204                 protected override void OnEndOpen (IAsyncResult result)
205                 {
206                         throw new NotImplementedException ();
207                 }
208
209                 [MonoTODO]
210                 protected override void OnClose (TimeSpan timeout)
211                 {
212                 }
213
214                 [MonoTODO]
215                 protected override void OnOpen (TimeSpan timeout)
216                 {
217                 }
218
219                 [MonoTODO]
220                 protected override void OnOpening ()
221                 {
222                 }
223
224                 [MonoTODO]
225                 protected override void OnOpened ()
226                 {
227                 }
228         }
229
230 #if obsolete
231         [ServiceContract]
232         interface UninitializedContract
233         {
234                 [OperationContract]
235                 void ItShouldReallyGone ();
236         }
237 #endif
238 }