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