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; }
54 public ClientCredentials Credentials {
55 get { return Endpoint.Behaviors.Find<ClientCredentials> (); }
58 protected internal override TimeSpan DefaultCloseTimeout {
59 get { return Endpoint.Binding.CloseTimeout; }
62 protected internal override TimeSpan DefaultOpenTimeout {
63 get { return Endpoint.Binding.OpenTimeout; }
66 protected virtual void ApplyConfiguration (string endpointConfig)
68 if (endpointConfig == null)
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 == "*")) {
77 throw new InvalidOperationException (String.Format ("More then one endpoint matching contract {0} was found.", contractName));
83 throw new InvalidOperationException (String.Format ("Client endpoint configuration '{0}' was not found in {1} endpoints.", endpointConfig, client.Endpoints.Count));
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);
90 if (res.BehaviorConfiguration != "")
91 ApplyBehavior (res.BehaviorConfiguration);
94 private void ApplyBehavior (string behaviorConfig)
96 BehaviorsSection behaviorsSection = (BehaviorsSection) ConfigurationManager.GetSection ("system.serviceModel/behaviors");
97 EndpointBehaviorElement behaviorElement = behaviorsSection.EndpointBehaviors [behaviorConfig];
99 foreach (BehaviorExtensionElement el in behaviorElement) {
100 IEndpointBehavior behavior = (IEndpointBehavior) el.CreateBehavior ();
101 Endpoint.Behaviors.Remove (behavior.GetType ());
102 Endpoint.Behaviors.Add (behavior);
107 protected virtual IChannelFactory CreateFactory ()
109 throw new NotImplementedException ();
112 protected abstract ServiceEndpoint CreateDescription ();
114 void IDisposable.Dispose ()
120 public T GetProperty<T> () where T : class
122 throw new NotImplementedException ();
125 protected void EnsureOpened ()
127 if (State != CommunicationState.Opened)
131 protected void InitializeEndpoint (
132 string endpointConfigurationName,
133 EndpointAddress remoteAddress)
135 InitializeEndpoint (CreateDescription ());
136 service_endpoint.Address = remoteAddress;
137 ApplyConfiguration (endpointConfigurationName);
140 protected void InitializeEndpoint (Binding binding,
141 EndpointAddress remoteAddress)
143 InitializeEndpoint (CreateDescription ());
144 service_endpoint.Binding = binding;
145 service_endpoint.Address = remoteAddress;
148 protected void InitializeEndpoint (ServiceEndpoint endpoint)
150 service_endpoint = endpoint;
154 protected override void OnAbort ()
156 throw new NotImplementedException ();
160 protected override IAsyncResult OnBeginClose (
161 TimeSpan timeout, AsyncCallback callback, object state)
163 throw new NotImplementedException ();
167 protected override IAsyncResult OnBeginOpen (
168 TimeSpan timeout, AsyncCallback callback, object state)
170 throw new NotImplementedException ();
174 protected override void OnEndClose (IAsyncResult result)
176 throw new NotImplementedException ();
180 protected override void OnEndOpen (IAsyncResult result)
182 throw new NotImplementedException ();
186 protected override void OnClose (TimeSpan timeout)
191 protected override void OnOpen (TimeSpan timeout)
196 protected override void OnOpening ()
201 protected override void OnOpened ()
207 interface UninitializedContract
210 void ItShouldReallyGone ();