PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/referencesource/System.ServiceModel.Activation/System/ServiceModel/Activation/HostedTransportConfigurationManager.cs

https://github.com/pruiz/mono
C# | 275 lines | 236 code | 31 blank | 8 comment | 27 complexity | aac3cdfd91fee9eb9ae8b812fa843d28 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Activation
  5. {
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Reflection;
  9. using System.Runtime;
  10. using System.Runtime.Diagnostics;
  11. using System.Security;
  12. using System.Security.Permissions;
  13. using System.ServiceModel;
  14. using System.ServiceModel.Configuration;
  15. using System.ServiceModel.Activation.Diagnostics;
  16. using System.ServiceModel.Diagnostics.Application;
  17. using System.Diagnostics.CodeAnalysis;
  18. class HostedTransportConfigurationManager
  19. {
  20. IDictionary<string, HostedTransportConfiguration> configurations = new Dictionary<string, HostedTransportConfiguration>(StringComparer.Ordinal);
  21. // Double-checked locking pattern requires volatile for read/write synchronization
  22. volatile bool initialized = false;
  23. MetabaseSettings metabaseSettings;
  24. // Double-checked locking pattern requires volatile for read/write synchronization
  25. static volatile HostedTransportConfigurationManager singleton;
  26. static object syncRoot = new object();
  27. #pragma warning disable 436
  28. const string WasHostingAssemblyName = "System.ServiceModel.WasHosting, Version=" + ThisAssembly.Version + ", Culture=neutral, PublicKeyToken=" + AssemblyRef.EcmaPublicKey;
  29. const string MetabaseSettingsIis7FactoryTypeName = "System.ServiceModel.WasHosting.MetabaseSettingsIis7Factory, " + WasHostingAssemblyName;
  30. #pragma warning restore 436
  31. const string CreateMetabaseSettingsIis7MethodName = "CreateMetabaseSettings";
  32. HostedTransportConfigurationManager()
  33. {
  34. if (!Iis7Helper.IsIis7)
  35. {
  36. metabaseSettings = new MetabaseSettingsIis6();
  37. }
  38. else
  39. {
  40. metabaseSettings = CreateWasHostingMetabaseSettings();
  41. }
  42. }
  43. HostedTransportConfigurationManager(MetabaseSettings metabaseSettings)
  44. {
  45. this.metabaseSettings = metabaseSettings;
  46. }
  47. [Fx.Tag.SecurityNote(Critical = "Calls Critical methods CreateMetabaseSettings.",
  48. Safe = "Ensures that only the correct, well-known method is called to construct the metabase settings. No other " +
  49. "details are leaked, and no control flow or data is allowed in.")]
  50. [SecuritySafeCritical]
  51. static MetabaseSettingsIis CreateWasHostingMetabaseSettings()
  52. {
  53. Type type = Type.GetType(MetabaseSettingsIis7FactoryTypeName, false);
  54. if (type == null)
  55. {
  56. throw FxTrace.Exception.AsError(new InvalidOperationException(SR.Hosting_MetabaseSettingsIis7TypeNotFound(MetabaseSettingsIis7FactoryTypeName, WasHostingAssemblyName)));
  57. }
  58. return CreateMetabaseSettings(type);
  59. }
  60. [SuppressMessage(FxCop.Category.Security, FxCop.Rule.SecureAsserts, Justification = "This is a private SecurityCritical method and its only caller passes in non-user data. Users cannot pass arbitrary data to this code.")]
  61. [Fx.Tag.SecurityNote(Critical = "Asserts full trust in order to call a well-known internal static in WasHosting.dll." +
  62. "Caller must ensure that 'type' argument refers to the trusted, well-known Type.")]
  63. [SecurityCritical]
  64. static MetabaseSettingsIis CreateMetabaseSettings(Type type)
  65. {
  66. object instance = null;
  67. MethodInfo method = type.GetMethod(CreateMetabaseSettingsIis7MethodName, BindingFlags.NonPublic | BindingFlags.Static);
  68. try
  69. {
  70. new PermissionSet(PermissionState.Unrestricted).Assert();
  71. instance = method.Invoke(null, null);
  72. }
  73. finally
  74. {
  75. PermissionSet.RevertAssert();
  76. }
  77. if (!(instance is MetabaseSettingsIis))
  78. {
  79. throw FxTrace.Exception.AsError(new InvalidOperationException(SR.Hosting_BadMetabaseSettingsIis7Type(type.AssemblyQualifiedName)));
  80. }
  81. return (MetabaseSettingsIis)instance;
  82. }
  83. internal static void EnsureInitializedForSimpleApplicationHost(HostedHttpRequestAsyncResult result)
  84. {
  85. if (singleton != null)
  86. {
  87. return;
  88. }
  89. lock (syncRoot)
  90. {
  91. if (singleton != null)
  92. return;
  93. singleton = new HostedTransportConfigurationManager(new MetabaseSettingsCassini(result));
  94. }
  95. }
  96. internal static MetabaseSettings MetabaseSettings
  97. {
  98. get
  99. {
  100. return HostedTransportConfigurationManager.Value.metabaseSettings;
  101. }
  102. }
  103. object ThisLock
  104. {
  105. get
  106. {
  107. return this;
  108. }
  109. }
  110. static HostedTransportConfigurationManager Value
  111. {
  112. get
  113. {
  114. if (singleton == null)
  115. {
  116. lock (syncRoot)
  117. {
  118. if (singleton == null)
  119. {
  120. //Ensure ETW tracing is initialized
  121. System.ServiceModel.Diagnostics.TraceUtility.SetEtwProviderId();
  122. if (TD.HostedTransportConfigurationManagerConfigInitStartIsEnabled())
  123. {
  124. TD.HostedTransportConfigurationManagerConfigInitStart();
  125. }
  126. ServiceHostingEnvironment.EnsureInitialized();
  127. singleton = new HostedTransportConfigurationManager();
  128. if (TD.HostedTransportConfigurationManagerConfigInitStopIsEnabled())
  129. {
  130. TD.HostedTransportConfigurationManagerConfigInitStop();
  131. }
  132. }
  133. }
  134. }
  135. return singleton;
  136. }
  137. }
  138. void EnsureInitialized()
  139. {
  140. if (!initialized)
  141. {
  142. lock (ThisLock)
  143. {
  144. if (!initialized)
  145. {
  146. // Register protocols.
  147. foreach (string protocol in metabaseSettings.GetProtocols())
  148. {
  149. // special case HTTP, it's a legacy protocol
  150. if (string.CompareOrdinal(protocol, Uri.UriSchemeHttp) == 0 ||
  151. string.CompareOrdinal(protocol, Uri.UriSchemeHttps) == 0)
  152. {
  153. HttpHostedTransportConfiguration httpConfiguration = null;
  154. if (string.CompareOrdinal(protocol, Uri.UriSchemeHttp) == 0)
  155. {
  156. httpConfiguration = new HttpHostedTransportConfiguration();
  157. }
  158. else
  159. {
  160. httpConfiguration = new HttpsHostedTransportConfiguration();
  161. }
  162. configurations.Add(protocol, httpConfiguration);
  163. }
  164. else
  165. {
  166. if (!Iis7Helper.IsIis7)
  167. {
  168. throw Fx.AssertAndThrowFatal("HostedTransportConfigurationManager.EnsureInitialized() protocols other than http and https can only be configured in IIS7");
  169. }
  170. if (AspNetPartialTrustHelpers.NeedPartialTrustInvoke)
  171. {
  172. throw FxTrace.Exception.AsError(new InvalidOperationException(SR.PartialTrustNonHttpActivation(protocol, HostingEnvironmentWrapper.ApplicationVirtualPath)));
  173. }
  174. AddHostedTransportConfigurationIis7(protocol);
  175. }
  176. }
  177. initialized = true;
  178. }
  179. }
  180. }
  181. }
  182. void AddHostedTransportConfigurationIis7(string protocol)
  183. {
  184. HostedTransportConfiguration configuration = null;
  185. try
  186. {
  187. ServiceHostingEnvironmentSection section = ServiceHostingEnvironmentSection.GetSection();
  188. if (section.TransportConfigurationTypes.ContainsKey(protocol))
  189. {
  190. TransportConfigurationTypeElement element = section.TransportConfigurationTypes[protocol];
  191. Debug.Print("HostedTransportConfigurationManager.AddHostedTransportConfigurationIis7() found TransportConfigurationTypes for protocol: " + protocol + " name: " + element.TransportConfigurationType);
  192. Type type = Type.GetType(element.TransportConfigurationType);
  193. configuration = Activator.CreateInstance(type) as HostedTransportConfiguration;
  194. configurations.Add(protocol, configuration);
  195. }
  196. else
  197. {
  198. throw FxTrace.Exception.AsError(new InvalidOperationException(SR.Hosting_ProtocolNoConfiguration(protocol)));
  199. }
  200. }
  201. catch (Exception exception)
  202. {
  203. if (!Fx.IsFatal(exception))
  204. {
  205. Debug.Print("HostedTransportConfigurationManager.AddHostedTransportConfigurationIis7() caught exception: " + exception);
  206. if (DiagnosticUtility.ShouldTraceError)
  207. {
  208. TraceUtility.TraceEvent(TraceEventType.Error, TraceCode.WebHostProtocolMisconfigured, SR.TraceCodeWebHostProtocolMisconfigured,
  209. new StringTraceRecord("Protocol", protocol),
  210. this, exception);
  211. }
  212. }
  213. throw;
  214. }
  215. }
  216. internal static Uri[] GetBaseAddresses(string virtualPath)
  217. {
  218. return HostedTransportConfigurationManager.Value.InternalGetBaseAddresses(virtualPath);
  219. }
  220. internal static HostedTransportConfiguration GetConfiguration(string scheme)
  221. {
  222. return HostedTransportConfigurationManager.Value.InternalGetConfiguration(scheme);
  223. }
  224. Uri[] InternalGetBaseAddresses(string virtualPath)
  225. {
  226. EnsureInitialized();
  227. List<Uri> baseAddresses = new List<Uri>();
  228. foreach (HostedTransportConfiguration configuration in configurations.Values)
  229. {
  230. baseAddresses.AddRange(configuration.GetBaseAddresses(virtualPath));
  231. }
  232. return baseAddresses.ToArray();
  233. }
  234. HostedTransportConfiguration InternalGetConfiguration(string scheme)
  235. {
  236. EnsureInitialized();
  237. if (!configurations.ContainsKey(scheme))
  238. {
  239. throw FxTrace.Exception.AsError(new InvalidOperationException(SR.Hosting_NotSupportedProtocol(scheme)));
  240. }
  241. return configurations[scheme];
  242. }
  243. }
  244. }