Update mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/RelaxngPattern.cs
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel / ServiceHost.cs
1 //
2 // ServiceHost.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005-2006 Novell, Inc.  http://www.novell.com
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28 using System;
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
36 namespace System.ServiceModel
37 {
38         public class ServiceHost : ServiceHostBase
39         {
40                 Type service_type;
41                 object instance;
42                 Dictionary<string,ContractDescription> contracts;
43
44                 protected ServiceHost ()
45                 {
46                 }
47
48                 public ServiceHost (object serviceInstance,
49                         params Uri [] baseAddresses)
50                 {
51                         if (serviceInstance == null)
52                                 throw new ArgumentNullException ("serviceInstance");
53                         InitializeDescription (serviceInstance,
54                                 new UriSchemeKeyedCollection (baseAddresses));
55                 }
56
57                 public ServiceHost (Type serviceType,
58                         params Uri [] baseAddresses)
59                 {
60                         InitializeDescription (serviceType,
61                                 new UriSchemeKeyedCollection (baseAddresses));
62                 }
63
64                 public object SingletonInstance {
65                         get { return instance; }
66                 }
67
68                 public ServiceEndpoint AddServiceEndpoint (
69                         Type implementedContract, Binding binding, string address)
70                 {
71                         return AddServiceEndpoint (implementedContract, binding,
72                                 new Uri (address, UriKind.RelativeOrAbsolute));
73                 }
74
75                 public ServiceEndpoint AddServiceEndpoint (
76                         Type implementedContract, Binding binding, string address, Uri listenUri)
77                 {
78                         return AddServiceEndpoint (implementedContract, binding,
79                                 new Uri (address, UriKind.RelativeOrAbsolute), listenUri);
80                 }
81
82                 public ServiceEndpoint AddServiceEndpoint (
83                         Type implementedContract, Binding binding, Uri address)
84                 {
85                         return AddServiceEndpoint (implementedContract,
86                                 binding, address, null);
87                 }
88
89                 public ServiceEndpoint AddServiceEndpoint (
90                         Type implementedContract, Binding binding, Uri address, Uri listenUri)
91                 {
92                         EndpointAddress ea = new EndpointAddress (BuildAbsoluteUri (address, binding));
93
94                         ContractDescription cd = GetExistingContract (implementedContract);
95                         if (cd == null) {
96                                 cd = ContractDescription.GetContract (implementedContract);
97                                 contracts.Add (cd.ContractType.FullName, cd);
98                         }
99
100                         return AddServiceEndpointCore (cd, binding, ea, listenUri);
101                 }
102
103                 ContractDescription GetExistingContract (Type implementedContract)
104                 {
105                         foreach (ContractDescription cd in ImplementedContracts.Values)
106                                 if (cd.ContractType == implementedContract)
107                                         return cd;
108                         return null;
109                 }
110
111                 protected override ServiceDescription CreateDescription (
112                         out IDictionary<string,ContractDescription> implementedContracts)
113                 {
114                         contracts = new Dictionary<string,ContractDescription> ();
115                         implementedContracts = contracts;
116                         ServiceDescription sd;
117                         foreach (ContractDescription cd in GetServiceContractDescriptions())
118                                 contracts.Add (cd.ContractType.FullName, cd);
119
120                         if (SingletonInstance != null) {
121                                 sd = ServiceDescription.GetService (instance);                          
122                         } else {
123                                 sd = ServiceDescription.GetService (service_type);                              
124                         }
125
126                         ServiceBehaviorAttribute sba = PopulateAttribute<ServiceBehaviorAttribute> ();
127                         if (SingletonInstance != null)
128                                 sba.SetWellKnownSingleton (SingletonInstance);
129                         sd.Behaviors.Add (sba);
130
131                         return sd;
132                 }
133
134                 IEnumerable<ContractDescription> GetServiceContractDescriptions () {
135                         List<ContractDescription> contracts = new List<ContractDescription> ();
136                         Dictionary<Type, ServiceContractAttribute> contractAttributes = ContractDescriptionGenerator.GetServiceContractAttributes (service_type);
137                         foreach (Type contract in contractAttributes.Keys)
138                                 contracts.Add( ContractDescriptionGenerator.GetContract (contract, service_type));
139                         return contracts;
140                 }
141
142                 TAttr PopulateAttribute<TAttr> ()
143                 {
144                         object [] atts = service_type.GetCustomAttributes (typeof (TAttr), false);
145                         return (TAttr) (atts.Length > 0 ? atts [0] : Activator.CreateInstance (typeof (TAttr)));
146                 }
147
148                 protected void InitializeDescription (Type serviceType, UriSchemeKeyedCollection baseAddresses)
149                 {
150                         if (!serviceType.IsClass)
151                                 throw new ArgumentException ("ServiceHost only supports 'class' service types.");
152
153                         service_type = serviceType;
154
155                         InitializeDescription (baseAddresses);
156                 }
157
158                 protected void InitializeDescription (object serviceInstance, UriSchemeKeyedCollection baseAddresses)
159                 {
160                         instance = serviceInstance;
161                         InitializeDescription (serviceInstance.GetType (), baseAddresses);
162                 }
163         }
164 }