ConfigUtil.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. //
  2. // ConfigUtil.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 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.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;
  37. namespace System.ServiceModel.Configuration
  38. {
  39. internal static class ConfigUtil
  40. {
  41. static object GetSection (string name)
  42. {
  43. if (ServiceHostingEnvironment.InAspNet)
  44. return WebConfigurationManager.GetSection (name);
  45. else
  46. return ConfigurationManager.GetSection (name);
  47. }
  48. public static BindingsSection BindingsSection {
  49. get {
  50. return (BindingsSection) GetSection ("system.serviceModel/bindings");
  51. }
  52. }
  53. public static ClientSection ClientSection {
  54. get { return (ClientSection) GetSection ("system.serviceModel/client"); }
  55. }
  56. public static ServicesSection ServicesSection {
  57. get { return (ServicesSection) GetSection ("system.serviceModel/services"); }
  58. }
  59. public static BehaviorsSection BehaviorsSection {
  60. get { return (BehaviorsSection) GetSection ("system.serviceModel/behaviors"); }
  61. }
  62. public static ExtensionsSection ExtensionsSection {
  63. get { return (ExtensionsSection) GetSection ("system.serviceModel/extensions"); }
  64. }
  65. #if NET_4_0
  66. public static ProtocolMappingSection ProtocolMappingSection {
  67. get {
  68. return (ProtocolMappingSection) GetSection ("system.serviceModel/protocolMapping");
  69. }
  70. }
  71. public static StandardEndpointsSection StandardEndpointsSection {
  72. get {
  73. return (StandardEndpointsSection) GetSection ("system.serviceModel/standardEndpoints");
  74. }
  75. }
  76. #endif
  77. public static Binding CreateBinding (string binding, string bindingConfiguration)
  78. {
  79. BindingCollectionElement section = ConfigUtil.BindingsSection [binding];
  80. if (section == null)
  81. throw new ArgumentException (String.Format ("binding section for {0} was not found.", binding));
  82. Binding b = section.GetDefault ();
  83. foreach (IBindingConfigurationElement el in section.ConfiguredBindings)
  84. if (el.Name == bindingConfiguration)
  85. el.ApplyConfiguration (b);
  86. return b;
  87. }
  88. #if NET_4_0
  89. public static Binding GetBindingByProtocolMapping (Uri address)
  90. {
  91. ProtocolMappingElement el = ConfigUtil.ProtocolMappingSection.ProtocolMappingCollection [address.Scheme];
  92. if (el == null)
  93. return null;
  94. return ConfigUtil.CreateBinding (el.Binding, el.BindingConfiguration);
  95. }
  96. public static ServiceEndpoint ConfigureStandardEndpoint (ContractDescription cd, ChannelEndpointElement element)
  97. {
  98. string kind = element.Kind;
  99. string endpointConfiguration = element.EndpointConfiguration;
  100. EndpointCollectionElement section = ConfigUtil.StandardEndpointsSection [kind];
  101. if (section == null)
  102. throw new ArgumentException (String.Format ("standard endpoint section for '{0}' was not found.", kind));
  103. StandardEndpointElement e = section.GetDefaultStandardEndpointElement ();
  104. ServiceEndpoint inst = e.CreateServiceEndpoint (cd);
  105. foreach (StandardEndpointElement el in section.ConfiguredEndpoints) {
  106. if (el.Name == endpointConfiguration) {
  107. el.InitializeAndValidate (element);
  108. el.ApplyConfiguration (inst, element);
  109. break;
  110. }
  111. }
  112. return inst;
  113. }
  114. public static Type GetTypeFromConfigString (string name)
  115. {
  116. Type type = Type.GetType (name);
  117. if (type != null)
  118. return type;
  119. foreach (var ass in AppDomain.CurrentDomain.GetAssemblies ())
  120. if ((type = ass.GetType (name)) != null)
  121. return type;
  122. return null;
  123. }
  124. public static ServiceEndpoint ConfigureStandardEndpoint (ContractDescription cd, ServiceEndpointElement element)
  125. {
  126. string kind = element.Kind;
  127. string endpointConfiguration = element.EndpointConfiguration;
  128. EndpointCollectionElement section = ConfigUtil.StandardEndpointsSection [kind];
  129. if (section == null)
  130. throw new ArgumentException (String.Format ("standard endpoint section for '{0}' was not found.", kind));
  131. StandardEndpointElement e = section.GetDefaultStandardEndpointElement ();
  132. ServiceEndpoint inst = e.CreateServiceEndpoint (cd);
  133. foreach (StandardEndpointElement el in section.ConfiguredEndpoints) {
  134. if (el.Name == endpointConfiguration) {
  135. el.InitializeAndValidate (element);
  136. el.ApplyConfiguration (inst, element);
  137. break;
  138. }
  139. }
  140. return inst;
  141. }
  142. #endif
  143. public static KeyedByTypeCollection<IEndpointBehavior> CreateEndpointBehaviors (string bindingConfiguration)
  144. {
  145. var ec = BehaviorsSection.EndpointBehaviors [bindingConfiguration];
  146. if (ec == null)
  147. return null;
  148. var c = new KeyedByTypeCollection<IEndpointBehavior> ();
  149. foreach (var bxe in ec)
  150. c.Add ((IEndpointBehavior) bxe.CreateBehavior ());
  151. return c;
  152. }
  153. public static EndpointAddress CreateInstance (this EndpointAddressElementBase el)
  154. {
  155. return new EndpointAddress (el.Address, el.Identity.CreateInstance (), el.Headers.Headers);
  156. }
  157. public static void CopyFrom (this ChannelEndpointElement to, ChannelEndpointElement from)
  158. {
  159. to.Address = from.Address;
  160. to.BehaviorConfiguration = from.BehaviorConfiguration;
  161. to.Binding = from.Binding;
  162. to.BindingConfiguration = from.BindingConfiguration;
  163. to.Contract = from.Contract;
  164. if (from.Headers != null)
  165. to.Headers.Headers = from.Headers.Headers;
  166. if (from.Identity != null)
  167. to.Identity.InitializeFrom (from.Identity.CreateInstance ());
  168. to.Name = from.Name;
  169. }
  170. public static EndpointAddress CreateEndpointAddress (this ChannelEndpointElement el)
  171. {
  172. return new EndpointAddress (el.Address, el.Identity != null ? el.Identity.CreateInstance () : null, el.Headers.Headers);
  173. }
  174. public static EndpointAddress CreateEndpointAddress (this ServiceEndpointElement el)
  175. {
  176. return new EndpointAddress (el.Address, el.Identity != null ? el.Identity.CreateInstance () : null, el.Headers.Headers);
  177. }
  178. public static EndpointIdentity CreateInstance (this IdentityElement el)
  179. {
  180. if (el.Certificate != null)
  181. return new X509CertificateEndpointIdentity (el.Certificate.CreateInstance ());
  182. else if (el.CertificateReference != null)
  183. return new X509CertificateEndpointIdentity (el.CertificateReference.CreateInstance ());
  184. else if (el.Dns != null)
  185. return new DnsEndpointIdentity (el.Dns.Value);
  186. else if (el.Rsa != null)
  187. return new RsaEndpointIdentity (el.Rsa.Value);
  188. else if (el.ServicePrincipalName != null)
  189. return new SpnEndpointIdentity (el.ServicePrincipalName.Value);
  190. else if (el.UserPrincipalName != null)
  191. return new UpnEndpointIdentity (el.UserPrincipalName.Value);
  192. else
  193. return null;
  194. }
  195. public static X509Certificate2 CreateCertificateFrom (StoreLocation storeLocation, StoreName storeName, X509FindType findType, Object findValue)
  196. {
  197. var store = new X509Store (storeName, storeLocation);
  198. store.Open (OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
  199. try {
  200. foreach (var c in store.Certificates.Find (findType, findValue, false))
  201. return c;
  202. 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));
  203. } finally {
  204. store.Close ();
  205. }
  206. }
  207. public static X509Certificate2 CreateInstance (this CertificateElement el)
  208. {
  209. return new X509Certificate2 (Convert.FromBase64String (el.EncodedValue));
  210. }
  211. public static X509Certificate2 CreateInstance (this CertificateReferenceElement el)
  212. {
  213. return CreateCertificateFrom (el.StoreLocation, el.StoreName, el.X509FindType, el.FindValue);
  214. }
  215. public static X509Certificate2 CreateInstance (this X509ClientCertificateCredentialsElement el)
  216. {
  217. return CreateCertificateFrom (el.StoreLocation, el.StoreName, el.X509FindType, el.FindValue);
  218. }
  219. public static X509Certificate2 CreateInstance (this X509ScopedServiceCertificateElement el)
  220. {
  221. return CreateCertificateFrom (el.StoreLocation, el.StoreName, el.X509FindType, el.FindValue);
  222. }
  223. public static X509Certificate2 CreateInstance (this X509DefaultServiceCertificateElement el)
  224. {
  225. return CreateCertificateFrom (el.StoreLocation, el.StoreName, el.X509FindType, el.FindValue);
  226. }
  227. }
  228. }