5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005-2006 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.Generic;
30 using System.Collections.ObjectModel;
31 using System.ServiceModel.Channels;
32 using System.ServiceModel.Configuration;
33 using System.ServiceModel.Description;
34 using System.ServiceModel.Dispatcher;
35 using System.ServiceModel.Security;
36 using System.Reflection;
38 namespace System.ServiceModel
40 public abstract class ServiceHostBase
41 : CommunicationObject, IExtensibleObject<ServiceHostBase>, IDisposable
43 ServiceCredentials credentials;
44 ServiceDescription description;
45 UriSchemeKeyedCollection base_addresses;
46 TimeSpan open_timeout, close_timeout, instance_idle_timeout;
47 ServiceThrottle throttle;
48 List<InstanceContext> contexts;
49 ReadOnlyCollection<InstanceContext> exposed_contexts;
50 ChannelDispatcherCollection channel_dispatchers;
51 IDictionary<string,ContractDescription> contracts;
52 int flow_limit = int.MaxValue;
53 IExtensionCollection<ServiceHostBase> extensions;
55 protected ServiceHostBase ()
57 open_timeout = DefaultOpenTimeout;
58 close_timeout = DefaultCloseTimeout;
60 credentials = new ServiceCredentials ();
61 throttle = new ServiceThrottle ();
62 contexts = new List<InstanceContext> ();
63 exposed_contexts = new ReadOnlyCollection<InstanceContext> (contexts);
64 channel_dispatchers = new ChannelDispatcherCollection (this);
67 public event EventHandler<UnknownMessageReceivedEventArgs>
68 UnknownMessageReceived;
70 public ReadOnlyCollection<Uri> BaseAddresses {
71 get { return new ReadOnlyCollection<Uri> (base_addresses.InternalItems); }
74 internal Uri CreateUri (string sheme, Uri relatieUri) {
75 Uri baseUri = base_addresses.Contains (sheme) ? base_addresses [sheme] : null;
77 if (relatieUri == null)
79 if (relatieUri.IsAbsoluteUri)
83 return new Uri (baseUri, relatieUri);
86 public ChannelDispatcherCollection ChannelDispatchers {
87 get { return channel_dispatchers; }
90 public ServiceAuthorizationBehavior Authorization {
96 public ServiceCredentials Credentials {
97 get { return credentials; }
100 public ServiceDescription Description {
101 get { return description; }
104 protected IDictionary<string,ContractDescription> ImplementedContracts {
105 get { return contracts; }
109 public IExtensionCollection<ServiceHostBase> Extensions {
111 if (extensions == null)
112 extensions = new ExtensionCollection<ServiceHostBase> (this);
117 protected internal override TimeSpan DefaultCloseTimeout {
118 get { return DefaultCommunicationTimeouts.Instance.CloseTimeout; }
121 protected internal override TimeSpan DefaultOpenTimeout {
122 get { return DefaultCommunicationTimeouts.Instance.OpenTimeout; }
125 public TimeSpan CloseTimeout {
126 get { return close_timeout; }
127 set { close_timeout = value; }
130 public TimeSpan OpenTimeout {
131 get { return open_timeout; }
132 set { open_timeout = value; }
135 public int ManualFlowControlLimit {
136 get { return flow_limit; }
137 set { flow_limit = value; }
140 public ServiceEndpoint AddServiceEndpoint (
141 string implementedContract, Binding binding, string address)
143 return AddServiceEndpoint (implementedContract,
145 new Uri (address, UriKind.RelativeOrAbsolute));
148 public ServiceEndpoint AddServiceEndpoint (
149 string implementedContract, Binding binding,
150 string address, Uri listenUri)
152 Uri uri = new Uri (address, UriKind.RelativeOrAbsolute);
153 return AddServiceEndpoint (
154 implementedContract, binding, uri, uri);
157 public ServiceEndpoint AddServiceEndpoint (
158 string implementedContract, Binding binding,
161 return AddServiceEndpoint (implementedContract, binding, address, address);
164 public ServiceEndpoint AddServiceEndpoint (
165 string implementedContract, Binding binding,
166 Uri address, Uri listenUri)
168 EndpointAddress ea = BuildEndpointAddress (address, binding);
169 ContractDescription cd = GetContract (implementedContract);
171 throw new InvalidOperationException (String.Format ("Contract '{0}' was not found in the implemented contracts in this service host.", implementedContract));
172 return AddServiceEndpointCore (cd, binding, ea, listenUri);
175 Type PopulateType (string typeName)
177 Type type = Type.GetType (typeName);
180 foreach (ContractDescription cd in ImplementedContracts.Values) {
181 type = cd.ContractType.Assembly.GetType (typeName);
188 ContractDescription GetContract (string typeName)
190 //FIXME: hack hack hack
191 ImplementedContracts ["IHttpGetHelpPageAndMetadataContract"] =
192 ContractDescription.GetContract (typeof (IHttpGetHelpPageAndMetadataContract));
194 // FIXME: As long as I tried, *only* IMetadataExchange
195 // is the exception case that does not require full
196 // type name. Hence I treat it as a special case.
197 if (typeName == ServiceMetadataBehavior.MexContractName) {
198 if (!Description.Behaviors.Contains (typeof (ServiceMetadataBehavior)) && Array.IndexOf (Description.ServiceType.GetInterfaces (), typeof (IMetadataExchange)) < 0)
199 throw new InvalidOperationException (
200 "Add ServiceMetadataBehavior to the ServiceHost to add a endpoint for IMetadataExchange contract.");
202 ImplementedContracts [ServiceMetadataBehavior.MexContractName] =
203 ContractDescription.GetContract (typeof (IMetadataExchange));
205 foreach (ContractDescription cd in ImplementedContracts.Values)
206 if (cd.ContractType == typeof (IMetadataExchange))
211 Type type = PopulateType (typeName);
215 foreach (ContractDescription cd in ImplementedContracts.Values) {
216 // FIXME: This check is a negative side effect
217 // of the above hack.
218 if (cd.ContractType == typeof (IMetadataExchange))
221 if (cd.ContractType == type ||
222 cd.ContractType.IsSubclassOf (type) ||
223 type.IsInterface && cd.ContractType.GetInterface (type.FullName) == type)
229 internal EndpointAddress BuildEndpointAddress (Uri address, Binding binding)
231 if (!address.IsAbsoluteUri) {
232 // Find a Base address with matching scheme,
233 // and build new absolute address
234 if (!base_addresses.Contains (binding.Scheme))
235 throw new InvalidOperationException (String.Format ("Could not find base address that matches Scheme {0} for endpoint {1}", binding.Scheme, binding.Name));
237 Uri baseaddr = base_addresses [binding.Scheme];
239 if (!baseaddr.AbsoluteUri.EndsWith ("/"))
240 baseaddr = new Uri (baseaddr.AbsoluteUri + "/");
241 address = new Uri (baseaddr, address);
243 return new EndpointAddress (address);
246 internal ServiceEndpoint AddServiceEndpointCore (
247 ContractDescription cd, Binding binding, EndpointAddress address, Uri listenUri)
249 foreach (ServiceEndpoint e in Description.Endpoints)
250 if (e.Contract == cd)
252 ServiceEndpoint se = new ServiceEndpoint (cd, binding, address);
253 se.ListenUri = listenUri.IsAbsoluteUri ? listenUri : new Uri (address.Uri, listenUri);
254 Description.Endpoints.Add (se);
259 protected virtual void ApplyConfiguration ()
261 if (Description == null)
262 throw new InvalidOperationException ("ApplyConfiguration requires that the Description property be initialized. Either provide a valid ServiceDescription in the CreateDescription method or override the ApplyConfiguration method to provide an alternative implementation");
264 ServiceElement service = GetServiceElement ();
266 //TODO: Should we call here LoadServiceElement ?
267 if (service != null) {
270 HostElement host = service.Host;
271 foreach (BaseAddressElement baseAddress in host.BaseAddresses) {
272 this.base_addresses.Add (new Uri (baseAddress.BaseAddress));
276 foreach (ServiceEndpointElement endpoint in service.Endpoints) {
277 // FIXME: consider BindingName as well
278 ServiceEndpoint se = AddServiceEndpoint (
280 ConfigUtil.CreateBinding (endpoint.Binding, endpoint.BindingConfiguration),
281 endpoint.Address.ToString ());
284 // TODO: use EvaluationContext of ServiceElement.
285 ServiceBehaviorElement behavior = ConfigUtil.BehaviorsSection.ServiceBehaviors.Find (service.BehaviorConfiguration);
286 if (behavior != null) {
287 for (int i = 0; i < behavior.Count; i++) {
288 BehaviorExtensionElement bxel = behavior [i];
289 IServiceBehavior b = (IServiceBehavior) behavior [i].CreateBehavior ();
291 Description.Behaviors.Add (b);
295 // TODO: consider commonBehaviors here
297 // ensure ServiceAuthorizationBehavior
298 Authorization = Description.Behaviors.Find<ServiceAuthorizationBehavior> ();
299 if (Authorization == null) {
300 Authorization = new ServiceAuthorizationBehavior ();
301 Description.Behaviors.Add (Authorization);
304 // ensure ServiceDebugBehavior
305 ServiceDebugBehavior debugBehavior = Description.Behaviors.Find<ServiceDebugBehavior> ();
306 if (debugBehavior == null) {
307 debugBehavior = new ServiceDebugBehavior ();
308 Description.Behaviors.Add (debugBehavior);
312 private ServiceElement GetServiceElement() {
313 Type serviceType = Description.ServiceType;
314 if (serviceType == null)
317 return ConfigUtil.ServicesSection.Services [serviceType.FullName];
320 internal ContractDescription GetContract (string name, string ns)
322 foreach (ContractDescription d in ImplementedContracts.Values)
323 if (d.Name == name && d.Namespace == ns)
328 protected abstract ServiceDescription CreateDescription (
329 out IDictionary<string,ContractDescription> implementedContracts);
331 protected void InitializeDescription (UriSchemeKeyedCollection baseAddresses)
333 this.base_addresses = baseAddresses;
334 IDictionary<string,ContractDescription> retContracts;
335 description = CreateDescription (out retContracts);
336 contracts = retContracts;
338 ApplyConfiguration ();
341 protected virtual void InitializeRuntime ()
343 //First validate the description, which should call all behaviors
345 ValidateDescription ();
347 //Build all ChannelDispatchers, one dispatcher per user configured EndPoint.
348 //We must keep thet ServiceEndpoints as a seperate collection, since the user
349 //can change the collection in the description during the behaviors events.
350 Dictionary<ServiceEndpoint, ChannelDispatcher> endPointToDispatcher = new Dictionary<ServiceEndpoint,ChannelDispatcher>();
351 ServiceEndpoint[] endPoints = new ServiceEndpoint[Description.Endpoints.Count];
352 Description.Endpoints.CopyTo (endPoints, 0);
353 foreach (ServiceEndpoint se in endPoints) {
354 //Let all behaviors add their binding parameters
355 BindingParameterCollection commonParams =
356 new BindingParameterCollection ();
357 AddBindingParameters (commonParams, se);
358 ChannelDispatcher channel = BuildChannelDispatcher (se, commonParams);
359 ChannelDispatchers.Add (channel);
360 endPointToDispatcher[se] = channel;
363 //After the ChannelDispatchers are created, and attached to the service host
364 //Apply dispatching behaviors.
365 foreach (IServiceBehavior b in Description.Behaviors)
366 b.ApplyDispatchBehavior (Description, this);
368 foreach(KeyValuePair<ServiceEndpoint, ChannelDispatcher> val in endPointToDispatcher)
369 ApplyDispatchBehavior(val.Value, val.Key);
372 private void ValidateDescription ()
374 foreach (IServiceBehavior b in Description.Behaviors)
375 b.Validate (Description, this);
376 foreach (ServiceEndpoint endPoint in Description.Endpoints)
377 endPoint.Validate ();
380 private void ApplyDispatchBehavior (ChannelDispatcher dispatcher, ServiceEndpoint endPoint) {
381 foreach (IContractBehavior b in endPoint.Contract.Behaviors)
382 b.ApplyDispatchBehavior (endPoint.Contract, endPoint, dispatcher.Endpoints[0].DispatchRuntime);
383 foreach (IEndpointBehavior b in endPoint.Behaviors)
384 b.ApplyDispatchBehavior (endPoint, dispatcher.Endpoints [0]);
385 foreach (OperationDescription operation in endPoint.Contract.Operations) {
386 foreach (IOperationBehavior b in operation.Behaviors)
387 b.ApplyDispatchBehavior (operation, dispatcher.Endpoints [0].DispatchRuntime.Operations [operation.Name]);
392 internal ChannelDispatcher BuildChannelDispatcher (ServiceEndpoint se, BindingParameterCollection commonParams) {
394 //User the binding parameters to build the channel listener and Dispatcher
395 IChannelListener lf = BuildListener (se, commonParams);
396 ChannelDispatcher cd = new ChannelDispatcher (
397 lf, se.Binding.Name);
398 cd.MessageVersion = se.Binding.MessageVersion;
399 if (cd.MessageVersion == null)
400 cd.MessageVersion = MessageVersion.Default;
402 //Attach one EndpointDispacher to the ChannelDispatcher
403 EndpointDispatcher endpoint_dispatcher =
404 new EndpointDispatcher (se.Address, se.Contract.Name, se.Contract.Namespace);
405 endpoint_dispatcher.DispatchRuntime.Type = Description.ServiceType;
406 endpoint_dispatcher.ContractFilter = GetContractFilter (se.Contract);
407 endpoint_dispatcher.ChannelDispatcher = cd;
408 cd.Endpoints.Add (endpoint_dispatcher);
410 //Build the dispatch operations
411 DispatchRuntime db = endpoint_dispatcher.DispatchRuntime;
412 foreach (OperationDescription od in se.Contract.Operations)
413 if (!db.Operations.Contains (od.Name))
414 PopulateDispatchOperation (db, od);
419 private MessageFilter GetContractFilter (ContractDescription contractDescription)
421 List<string> actions = new List<string> ();
422 foreach (OperationDescription od in contractDescription.Operations)
423 foreach (MessageDescription md in od.Messages)
424 if (md.Direction == MessageDirection.Input)
425 if (md.Action == "*")
426 return new MatchAllMessageFilter ();
428 actions.Add (md.Action);
430 return new ActionMessageFilter (actions.ToArray ());
433 private void AddBindingParameters (BindingParameterCollection commonParams, ServiceEndpoint endPoint) {
435 commonParams.Add (ChannelProtectionRequirements.CreateFromContract (endPoint.Contract));
436 foreach (IServiceBehavior b in Description.Behaviors)
437 b.AddBindingParameters (Description, this, Description.Endpoints, commonParams);
439 foreach (IContractBehavior b in endPoint.Contract.Behaviors)
440 b.AddBindingParameters (endPoint.Contract, endPoint, commonParams);
441 foreach (IEndpointBehavior b in endPoint.Behaviors)
442 b.AddBindingParameters (endPoint, commonParams);
443 foreach (OperationDescription operation in endPoint.Contract.Operations) {
444 foreach (IOperationBehavior b in operation.Behaviors)
445 b.AddBindingParameters (operation, commonParams);
449 void PopulateDispatchOperation (DispatchRuntime db, OperationDescription od) {
450 string reqA = null, resA = null;
451 foreach (MessageDescription m in od.Messages) {
452 if (m.Direction == MessageDirection.Input)
457 DispatchOperation o =
459 new DispatchOperation (db, od.Name, reqA) :
460 new DispatchOperation (db, od.Name, reqA, resA);
461 bool has_void_reply = false;
462 foreach (MessageDescription md in od.Messages) {
463 if (md.Direction == MessageDirection.Input &&
464 md.Body.Parts.Count == 1 &&
465 md.Body.Parts [0].Type == typeof (Message))
466 o.DeserializeRequest = false;
467 if (md.Direction == MessageDirection.Output &&
468 md.Body.ReturnValue != null) {
469 if (md.Body.ReturnValue.Type == typeof (Message))
470 o.SerializeReply = false;
471 else if (md.Body.ReturnValue.Type == typeof (void))
472 has_void_reply = true;
477 // FIXME: support async method
478 if (od.SyncMethod != null)
479 o.Invoker = new SyncMethodInvoker (od.SyncMethod);
481 o.Invoker = new AsyncMethodInvoker (od.BeginMethod, od.EndMethod);
484 o.Formatter = BaseMessagesFormatter.Create (od);
486 if (o.Action == "*" && o.ReplyAction == "*") {
487 //Signature : Message (Message)
489 //FIXME: void (IChannel)
490 if (!o.DeserializeRequest && (!o.SerializeReply || has_void_reply))
491 db.UnhandledDispatchOperation = o;
494 db.Operations.Add (o);
498 protected void LoadConfigurationSection (ServiceElement element)
500 ServicesSection services = ConfigUtil.ServicesSection;
503 void DoOpen (TimeSpan timeout)
505 foreach (var cd in ChannelDispatchers) {
507 // This is likely hack.
508 if (cd is ChannelDispatcher)
509 ((ChannelDispatcher) cd).StartLoop ();
513 IChannelListener BuildListener (ServiceEndpoint se,
514 BindingParameterCollection pl)
516 Binding b = se.Binding;
517 if (b.CanBuildChannelListener<IReplySessionChannel> (pl))
518 return b.BuildChannelListener<IReplySessionChannel> (se.ListenUri, "", se.ListenUriMode, pl);
519 if (b.CanBuildChannelListener<IReplyChannel> (pl))
520 return b.BuildChannelListener<IReplyChannel> (se.ListenUri, "", se.ListenUriMode, pl);
521 if (b.CanBuildChannelListener<IInputSessionChannel> (pl))
522 return b.BuildChannelListener<IInputSessionChannel> (se.ListenUri, "", se.ListenUriMode, pl);
523 if (b.CanBuildChannelListener<IInputChannel> (pl))
524 return b.BuildChannelListener<IInputChannel> (se.ListenUri, "", se.ListenUriMode, pl);
526 if (b.CanBuildChannelListener<IDuplexChannel> (pl))
527 return b.BuildChannelListener<IDuplexChannel> (se.ListenUri, "", se.ListenUriMode, pl);
528 if (b.CanBuildChannelListener<IDuplexSessionChannel> (pl))
529 return b.BuildChannelListener<IDuplexSessionChannel> (se.ListenUri, "", se.ListenUriMode, pl);
530 throw new InvalidOperationException ("None of the listener channel types is supported");
534 protected override sealed void OnAbort ()
538 Action<TimeSpan> close_delegate;
539 Action<TimeSpan> open_delegate;
541 protected override sealed IAsyncResult OnBeginClose (
542 TimeSpan timeout, AsyncCallback callback, object state)
544 if (close_delegate != null)
545 close_delegate = new Action<TimeSpan> (OnClose);
546 return close_delegate.BeginInvoke (timeout, callback, state);
549 protected override sealed IAsyncResult OnBeginOpen (
550 TimeSpan timeout, AsyncCallback callback, object state)
552 if (open_delegate == null)
553 open_delegate = new Action<TimeSpan> (OnOpen);
554 return open_delegate.BeginInvoke (timeout, callback, state);
557 protected override void OnClose (TimeSpan timeout)
559 DateTime start = DateTime.Now;
560 ReleasePerformanceCounters ();
561 List<ChannelDispatcherBase> l = new List<ChannelDispatcherBase> (ChannelDispatchers);
562 foreach (ChannelDispatcherBase e in l) {
564 TimeSpan ts = timeout - (DateTime.Now - start);
565 if (ts < TimeSpan.Zero)
569 } catch (Exception ex) {
570 Console.WriteLine ("ServiceHostBase failed to close the channel dispatcher:");
571 Console.WriteLine (ex);
576 protected override sealed void OnOpen (TimeSpan timeout)
578 InitializeRuntime ();
582 protected override void OnEndClose (IAsyncResult result)
584 if (close_delegate == null)
585 throw new InvalidOperationException ("Async close operation has not started");
586 close_delegate.EndInvoke (result);
589 protected override sealed void OnEndOpen (IAsyncResult result)
591 if (open_delegate == null)
592 throw new InvalidOperationException ("Aync open operation has not started");
593 open_delegate.EndInvoke (result);
596 protected override void OnOpened ()
601 protected void ReleasePerformanceCounters ()
605 void IDisposable.Dispose ()
610 class SyncMethodInvoker : IOperationInvoker
612 readonly MethodInfo _methodInfo;
613 public SyncMethodInvoker (MethodInfo methodInfo) {
614 _methodInfo = methodInfo;
617 #region IOperationInvoker Members
619 public bool IsSynchronous {
623 public object [] AllocateParameters () {
624 return new object [_methodInfo.GetParameters ().Length];
627 public object Invoke (object instance, object [] parameters)
629 return _methodInfo.Invoke (instance, parameters);
632 public IAsyncResult InvokeBegin (object instance, object [] inputs, AsyncCallback callback, object state) {
633 throw new NotSupportedException ();
636 public object InvokeEnd (object instance, out object [] outputs, IAsyncResult result) {
637 throw new NotSupportedException ();
643 class AsyncMethodInvoker : IOperationInvoker
645 readonly MethodInfo _beginMethodInfo, _endMethodInfo;
646 public AsyncMethodInvoker (MethodInfo beginMethodInfo, MethodInfo endMethodInfo) {
647 _beginMethodInfo = beginMethodInfo;
648 _endMethodInfo = endMethodInfo;
651 #region IOperationInvoker Members
653 public bool IsSynchronous {
654 get { return false; }
657 public object [] AllocateParameters () {
658 return new object [_beginMethodInfo.GetParameters ().Length - 2 + _endMethodInfo.GetParameters().Length-1];
661 public object Invoke (object instance, object [] parameters) {
662 throw new NotImplementedException ("Can't invoke async method synchronously");
663 //BUGBUG: need to differentiate between input and output parameters.
664 IAsyncResult asyncResult = InvokeBegin(instance, parameters, delegate(IAsyncResult ignore) { }, null);
665 asyncResult.AsyncWaitHandle.WaitOne();
666 return InvokeEnd(instance, out parameters, asyncResult);
669 public IAsyncResult InvokeBegin (object instance, object [] inputs, AsyncCallback callback, object state) {
670 if (inputs.Length + 2 != _beginMethodInfo.GetParameters ().Length)
671 throw new ArgumentException ("Wrong number of input parameters");
672 object [] fullargs = new object [_beginMethodInfo.GetParameters ().Length];
673 Array.Copy (inputs, fullargs, inputs.Length);
674 fullargs [inputs.Length] = callback;
675 fullargs [inputs.Length + 1] = state;
676 return (IAsyncResult) _beginMethodInfo.Invoke (instance, fullargs);
679 public object InvokeEnd (object instance, out object [] outputs, IAsyncResult asyncResult) {
680 outputs = new object [_endMethodInfo.GetParameters ().Length - 1];
681 object [] fullargs = new object [_endMethodInfo.GetParameters ().Length];
682 fullargs [outputs.Length] = asyncResult;
683 object result = _endMethodInfo.Invoke (instance, fullargs);
684 Array.Copy (fullargs, outputs, outputs.Length);