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;
36 namespace System.ServiceModel
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
44 ServiceEndpoint service_endpoint;
46 protected ChannelFactory ()
50 public ServiceEndpoint Endpoint {
51 get { return service_endpoint; }
55 public ClientCredentials Credentials {
56 get { return Endpoint.Behaviors.Find<ClientCredentials> (); }
60 protected internal override TimeSpan DefaultCloseTimeout {
61 get { return Endpoint.Binding.CloseTimeout; }
64 protected internal override TimeSpan DefaultOpenTimeout {
65 get { return Endpoint.Binding.OpenTimeout; }
68 protected virtual void ApplyConfiguration (string endpointConfig)
71 if (endpointConfig == null)
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 == "*")) {
80 throw new InvalidOperationException (String.Format ("More then one endpoint matching contract {0} was found.", contractName));
86 throw new InvalidOperationException (String.Format ("Client endpoint configuration '{0}' was not found in {1} endpoints.", endpointConfig, client.Endpoints.Count));
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);
93 if (res.BehaviorConfiguration != "")
94 ApplyBehavior (res.BehaviorConfiguration);
99 private void ApplyBehavior (string behaviorConfig)
101 BehaviorsSection behaviorsSection = (BehaviorsSection) ConfigurationManager.GetSection ("system.serviceModel/behaviors");
102 EndpointBehaviorElement behaviorElement = behaviorsSection.EndpointBehaviors [behaviorConfig];
104 foreach (BehaviorExtensionElement el in behaviorElement) {
105 IEndpointBehavior behavior = (IEndpointBehavior) el.CreateBehavior ();
106 Endpoint.Behaviors.Remove (behavior.GetType ());
107 Endpoint.Behaviors.Add (behavior);
113 protected virtual IChannelFactory CreateFactory ()
115 throw new NotImplementedException ();
118 protected abstract ServiceEndpoint CreateDescription ();
120 void IDisposable.Dispose ()
126 public T GetProperty<T> () where T : class
128 throw new NotImplementedException ();
131 protected void EnsureOpened ()
133 if (State != CommunicationState.Opened)
137 protected void InitializeEndpoint (
138 string endpointConfigurationName,
139 EndpointAddress remoteAddress)
141 InitializeEndpoint (CreateDescription ());
142 service_endpoint.Address = remoteAddress;
143 ApplyConfiguration (endpointConfigurationName);
146 protected void InitializeEndpoint (Binding binding,
147 EndpointAddress remoteAddress)
149 InitializeEndpoint (CreateDescription ());
150 service_endpoint.Binding = binding;
151 service_endpoint.Address = remoteAddress;
154 protected void InitializeEndpoint (ServiceEndpoint endpoint)
156 service_endpoint = endpoint;
160 protected override void OnAbort ()
162 throw new NotImplementedException ();
166 protected override IAsyncResult OnBeginClose (
167 TimeSpan timeout, AsyncCallback callback, object state)
169 throw new NotImplementedException ();
173 protected override IAsyncResult OnBeginOpen (
174 TimeSpan timeout, AsyncCallback callback, object state)
176 throw new NotImplementedException ();
180 protected override void OnEndClose (IAsyncResult result)
182 throw new NotImplementedException ();
186 protected override void OnEndOpen (IAsyncResult result)
188 throw new NotImplementedException ();
192 protected override void OnClose (TimeSpan timeout)
197 protected override void OnOpen (TimeSpan timeout)
202 protected override void OnOpening ()
207 protected override void OnOpened ()
214 interface UninitializedContract
217 void ItShouldReallyGone ();