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;
36 namespace System.ServiceModel
38 public class ServiceHost : ServiceHostBase
42 Dictionary<string,ContractDescription> contracts;
44 protected ServiceHost ()
48 public ServiceHost (object serviceInstance,
49 params Uri [] baseAddresses)
51 if (serviceInstance == null)
52 throw new ArgumentNullException ("serviceInstance");
53 InitializeDescription (serviceInstance,
54 new UriSchemeKeyedCollection (baseAddresses));
57 public ServiceHost (Type serviceType,
58 params Uri [] baseAddresses)
60 InitializeDescription (serviceType,
61 new UriSchemeKeyedCollection (baseAddresses));
64 public object SingletonInstance {
65 get { return instance; }
69 public ServiceEndpoint AddServiceEndpoint (
70 Type implementedContract, Binding binding, string address)
72 return AddServiceEndpoint (implementedContract, binding,
73 new Uri (address, UriKind.RelativeOrAbsolute));
77 public ServiceEndpoint AddServiceEndpoint (
78 Type implementedContract, Binding binding, string address, Uri listenUri)
80 return AddServiceEndpoint (implementedContract, binding,
81 new Uri (address, UriKind.RelativeOrAbsolute), listenUri);
85 public ServiceEndpoint AddServiceEndpoint (
86 Type implementedContract, Binding binding, Uri address)
88 return AddServiceEndpoint (implementedContract,
89 binding, address, address);
93 public ServiceEndpoint AddServiceEndpoint (
94 Type implementedContract, Binding binding, Uri address, Uri listenUri)
96 EndpointAddress ea = BuildEndpointAddress (address, binding);
98 ContractDescription cd = GetExistingContract (implementedContract);
100 cd = ContractDescription.GetContract (implementedContract);
101 contracts.Add (cd.ContractType.FullName, cd);
104 return AddServiceEndpointCore (cd, binding, ea, listenUri);
107 ContractDescription GetExistingContract (Type implementedContract)
109 foreach (ContractDescription cd in ImplementedContracts.Values)
110 if (cd.ContractType == implementedContract)
115 protected override ServiceDescription CreateDescription (
116 out IDictionary<string,ContractDescription> implementedContracts)
118 contracts = new Dictionary<string,ContractDescription> ();
119 implementedContracts = contracts;
120 ServiceDescription sd;
121 foreach (ContractDescription cd in GetServiceContractDescriptions())
122 contracts.Add (cd.ContractType.FullName, cd);
124 if (SingletonInstance != null) {
125 sd = ServiceDescription.GetService (instance);
127 sd = ServiceDescription.GetService (service_type);
130 ServiceBehaviorAttribute sba = PopulateAttribute<ServiceBehaviorAttribute> ();
131 if (SingletonInstance != null)
132 sba.SetWellKnownSingleton (SingletonInstance);
133 sd.Behaviors.Add (sba);
138 IEnumerable<ContractDescription> GetServiceContractDescriptions () {
139 List<ContractDescription> contracts = new List<ContractDescription> ();
140 Type contractType = null;
141 Dictionary<Type, ServiceContractAttribute> contractAttributes = ContractDescriptionGenerator.GetServiceContractAttributes (service_type);
142 foreach (Type contract in contractAttributes.Keys)
143 contracts.Add( ContractDescriptionGenerator.GetContract (contract, service_type));
147 TAttr PopulateAttribute<TAttr> ()
149 object [] atts = service_type.GetCustomAttributes (typeof (TAttr), false);
150 return (TAttr) (atts.Length > 0 ? atts [0] : Activator.CreateInstance (typeof (TAttr)));
154 protected void InitializeDescription (Type serviceType, UriSchemeKeyedCollection baseAddresses)
156 if (!serviceType.IsClass)
157 throw new ArgumentException ("ServiceHost only supports 'class' service types.");
159 service_type = serviceType;
161 InitializeDescription (baseAddresses);
165 protected void InitializeDescription (object serviceInstance, UriSchemeKeyedCollection baseAddresses)
167 instance = serviceInstance;
168 InitializeDescription (serviceInstance.GetType (), baseAddresses);