5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 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.Configuration;
30 using System.Collections.Generic;
31 using System.Security.Cryptography.X509Certificates;
32 using System.ServiceModel.Channels;
33 using System.ServiceModel.Configuration;
34 using System.ServiceModel.Description;
35 using System.ServiceModel.Dispatcher;
36 using System.Web.Configuration;
38 namespace System.ServiceModel.Configuration
40 internal static class ConfigUtil
42 static object GetSection (string name)
44 if (ServiceHostingEnvironment.InAspNet)
45 return WebConfigurationManager.GetSection (name);
47 return ConfigurationManager.GetSection (name);
50 public static BindingsSection BindingsSection {
52 return (BindingsSection) GetSection ("system.serviceModel/bindings");
56 public static ClientSection ClientSection {
57 get { return (ClientSection) GetSection ("system.serviceModel/client"); }
60 public static ServicesSection ServicesSection {
61 get { return (ServicesSection) GetSection ("system.serviceModel/services"); }
64 public static BehaviorsSection BehaviorsSection {
65 get { return (BehaviorsSection) GetSection ("system.serviceModel/behaviors"); }
68 public static ExtensionsSection ExtensionsSection {
69 get { return (ExtensionsSection) GetSection ("system.serviceModel/extensions"); }
73 public static ProtocolMappingSection ProtocolMappingSection {
75 return (ProtocolMappingSection) GetSection ("system.serviceModel/protocolMapping");
79 public static StandardEndpointsSection StandardEndpointsSection {
81 return (StandardEndpointsSection) GetSection ("system.serviceModel/standardEndpoints");
86 public static Binding CreateBinding (string binding, string bindingConfiguration)
88 BindingCollectionElement section = ConfigUtil.BindingsSection [binding];
90 throw new ArgumentException (String.Format ("binding section for {0} was not found.", binding));
92 Binding b = section.GetDefault ();
94 foreach (IBindingConfigurationElement el in section.ConfiguredBindings)
95 if (el.Name == bindingConfiguration)
96 el.ApplyConfiguration (b);
102 public static Binding GetBindingByProtocolMapping (Uri address)
104 ProtocolMappingElement el = ConfigUtil.ProtocolMappingSection.ProtocolMappingCollection [address.Scheme];
107 return ConfigUtil.CreateBinding (el.Binding, el.BindingConfiguration);
110 public static ServiceEndpoint ConfigureStandardEndpoint (ContractDescription cd, ChannelEndpointElement element)
112 string kind = element.Kind;
113 string endpointConfiguration = element.EndpointConfiguration;
115 EndpointCollectionElement section = ConfigUtil.StandardEndpointsSection [kind];
117 throw new ArgumentException (String.Format ("standard endpoint section for '{0}' was not found.", kind));
119 StandardEndpointElement e = section.GetDefaultStandardEndpointElement ();
121 ServiceEndpoint inst = e.CreateServiceEndpoint (cd);
123 foreach (StandardEndpointElement el in section.ConfiguredEndpoints) {
124 if (el.Name == endpointConfiguration) {
125 el.InitializeAndValidate (element);
126 el.ApplyConfiguration (inst, element);
134 public static Type GetTypeFromConfigString (string name)
136 Type type = Type.GetType (name);
139 foreach (var ass in AppDomain.CurrentDomain.GetAssemblies ())
140 if ((type = ass.GetType (name)) != null)
145 public static ServiceEndpoint ConfigureStandardEndpoint (ContractDescription cd, ServiceEndpointElement element)
147 string kind = element.Kind;
148 string endpointConfiguration = element.EndpointConfiguration;
150 EndpointCollectionElement section = ConfigUtil.StandardEndpointsSection [kind];
152 throw new ArgumentException (String.Format ("standard endpoint section for '{0}' was not found.", kind));
154 StandardEndpointElement e = section.GetDefaultStandardEndpointElement ();
156 ServiceEndpoint inst = e.CreateServiceEndpoint (cd);
158 foreach (StandardEndpointElement el in section.ConfiguredEndpoints) {
159 if (el.Name == endpointConfiguration) {
160 el.InitializeAndValidate (element);
161 el.ApplyConfiguration (inst, element);
170 public static KeyedByTypeCollection<IEndpointBehavior> CreateEndpointBehaviors (string bindingConfiguration)
172 var ec = BehaviorsSection.EndpointBehaviors [bindingConfiguration];
175 var c = new KeyedByTypeCollection<IEndpointBehavior> ();
176 foreach (var bxe in ec)
177 c.Add ((IEndpointBehavior) bxe.CreateBehavior ());
181 public static EndpointAddress CreateInstance (this EndpointAddressElementBase el)
183 return new EndpointAddress (el.Address, el.Identity.CreateInstance (), el.Headers.Headers);
186 public static void CopyFrom (this ChannelEndpointElement to, ChannelEndpointElement from)
188 to.Address = from.Address;
189 to.BehaviorConfiguration = from.BehaviorConfiguration;
190 to.Binding = from.Binding;
191 to.BindingConfiguration = from.BindingConfiguration;
192 to.Contract = from.Contract;
193 if (from.Headers != null)
194 to.Headers.Headers = from.Headers.Headers;
195 if (from.Identity != null)
196 to.Identity.InitializeFrom (from.Identity.CreateInstance ());
200 public static EndpointAddress CreateEndpointAddress (this ChannelEndpointElement el)
202 return new EndpointAddress (el.Address, el.Identity != null ? el.Identity.CreateInstance () : null, el.Headers.Headers);
205 public static EndpointAddress CreateEndpointAddress (this ServiceEndpointElement el)
207 return new EndpointAddress (el.Address, el.Identity != null ? el.Identity.CreateInstance () : null, el.Headers.Headers);
210 public static EndpointIdentity CreateInstance (this IdentityElement el)
212 if (el.Certificate != null)
213 return new X509CertificateEndpointIdentity (el.Certificate.CreateInstance ());
214 else if (el.CertificateReference != null)
215 return new X509CertificateEndpointIdentity (el.CertificateReference.CreateInstance ());
216 else if (el.Dns != null)
217 return new DnsEndpointIdentity (el.Dns.Value);
218 else if (el.Rsa != null)
219 return new RsaEndpointIdentity (el.Rsa.Value);
220 else if (el.ServicePrincipalName != null)
221 return new SpnEndpointIdentity (el.ServicePrincipalName.Value);
222 else if (el.UserPrincipalName != null)
223 return new UpnEndpointIdentity (el.UserPrincipalName.Value);
228 public static X509Certificate2 CreateCertificateFrom (StoreLocation storeLocation, StoreName storeName, X509FindType findType, Object findValue)
230 var store = new X509Store (storeName, storeLocation);
231 store.Open (OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
233 foreach (var c in store.Certificates.Find (findType, findValue, false))
235 throw new InvalidOperationException (String.Format ("Specified X509 certificate with find type {0} and find value {1} was not found in X509 store {2} location {3}", findType, findValue, storeName, storeLocation));
241 public static X509Certificate2 CreateInstance (this CertificateElement el)
243 return new X509Certificate2 (Convert.FromBase64String (el.EncodedValue));
246 public static X509Certificate2 CreateInstance (this CertificateReferenceElement el)
248 return CreateCertificateFrom (el.StoreLocation, el.StoreName, el.X509FindType, el.FindValue);
251 public static X509Certificate2 CreateInstance (this X509ClientCertificateCredentialsElement el)
253 return CreateCertificateFrom (el.StoreLocation, el.StoreName, el.X509FindType, el.FindValue);
256 public static X509Certificate2 CreateInstance (this X509ScopedServiceCertificateElement el)
258 return CreateCertificateFrom (el.StoreLocation, el.StoreName, el.X509FindType, el.FindValue);
261 public static X509Certificate2 CreateInstance (this X509DefaultServiceCertificateElement el)
263 return CreateCertificateFrom (el.StoreLocation, el.StoreName, el.X509FindType, el.FindValue);