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