5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005 Novell, Inc. http://www.novell.com
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
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;
37 namespace System.ServiceModel
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
45 ServiceEndpoint service_endpoint;
47 protected ChannelFactory ()
51 public ServiceEndpoint Endpoint {
52 get { return service_endpoint; }
56 public ClientCredentials Credentials {
57 get { return Endpoint.Behaviors.Find<ClientCredentials> (); }
61 protected internal override TimeSpan DefaultCloseTimeout {
62 get { return Endpoint.Binding.CloseTimeout; }
65 protected internal override TimeSpan DefaultOpenTimeout {
66 get { return Endpoint.Binding.OpenTimeout; }
69 protected virtual void ApplyConfiguration (string endpointConfig)
71 if (endpointConfig == null)
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;
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 == "*")) {
94 throw new InvalidOperationException (String.Format ("More then one endpoint matching contract {0} was found.", contractName));
100 throw new InvalidOperationException (String.Format ("Client endpoint configuration '{0}' was not found in {1} endpoints.", endpointConfig, client.Endpoints.Count));
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);
107 if (res.BehaviorConfiguration != "")
108 ApplyBehavior (res.BehaviorConfiguration);
113 private void ApplyBehavior (string behaviorConfig)
115 BehaviorsSection behaviorsSection = (BehaviorsSection) ConfigurationManager.GetSection ("system.serviceModel/behaviors");
116 EndpointBehaviorElement behaviorElement = behaviorsSection.EndpointBehaviors [behaviorConfig];
118 foreach (BehaviorExtensionElement el in behaviorElement) {
119 IEndpointBehavior behavior = (IEndpointBehavior) el.CreateBehavior ();
120 Endpoint.Behaviors.Remove (behavior.GetType ());
121 Endpoint.Behaviors.Add (behavior);
127 protected virtual IChannelFactory CreateFactory ()
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 ();
136 protected abstract ServiceEndpoint CreateDescription ();
138 void IDisposable.Dispose ()
144 public T GetProperty<T> () where T : class
146 throw new NotImplementedException ();
149 protected void EnsureOpened ()
151 if (State != CommunicationState.Opened)
155 protected void InitializeEndpoint (
156 string endpointConfigurationName,
157 EndpointAddress remoteAddress)
159 InitializeEndpoint (CreateDescription ());
160 service_endpoint.Address = remoteAddress;
161 ApplyConfiguration (endpointConfigurationName);
164 protected void InitializeEndpoint (Binding binding,
165 EndpointAddress remoteAddress)
167 InitializeEndpoint (CreateDescription ());
168 service_endpoint.Binding = binding;
169 service_endpoint.Address = remoteAddress;
172 protected void InitializeEndpoint (ServiceEndpoint endpoint)
174 service_endpoint = endpoint;
178 protected override void OnAbort ()
180 throw new NotImplementedException ();
184 protected override IAsyncResult OnBeginClose (
185 TimeSpan timeout, AsyncCallback callback, object state)
187 throw new NotImplementedException ();
191 protected override IAsyncResult OnBeginOpen (
192 TimeSpan timeout, AsyncCallback callback, object state)
194 throw new NotImplementedException ();
198 protected override void OnEndClose (IAsyncResult result)
200 throw new NotImplementedException ();
204 protected override void OnEndOpen (IAsyncResult result)
206 throw new NotImplementedException ();
210 protected override void OnClose (TimeSpan timeout)
215 protected override void OnOpen (TimeSpan timeout)
220 protected override void OnOpening ()
225 protected override void OnOpened ()
232 interface UninitializedContract
235 void ItShouldReallyGone ();