PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/aspclassiccompiler/AzureStoreAsp/Assets/AspProviders/Configuration.cs

#
C# | 309 lines | 230 code | 42 blank | 37 comment | 49 complexity | b5473ddc403e4e7612b0bbf62fbbde47 MD5 | raw file
Possible License(s): Apache-2.0, AGPL-3.0
  1. // ----------------------------------------------------------------------------------
  2. // Microsoft Developer & Platform Evangelism
  3. //
  4. // Copyright (c) Microsoft Corporation. All rights reserved.
  5. //
  6. // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  7. // EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
  8. // OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  9. // ----------------------------------------------------------------------------------
  10. // The example companies, organizations, products, domain names,
  11. // e-mail addresses, logos, people, places, and events depicted
  12. // herein are fictitious. No association with any real company,
  13. // organization, product, domain name, email address, logo, person,
  14. // places, or events is intended or should be inferred.
  15. // ----------------------------------------------------------------------------------
  16. //
  17. // <copyright file="Configuration.cs" company="Microsoft">
  18. // Copyright (c) Microsoft Corporation. All rights reserved.
  19. // </copyright>
  20. //
  21. using System;
  22. using System.Collections.Generic;
  23. using System.Linq;
  24. using System.Text;
  25. using System.Globalization;
  26. using System.Configuration;
  27. using System.Collections.Specialized;
  28. using System.Runtime.InteropServices;
  29. using Microsoft.Samples.ServiceHosting.StorageClient;
  30. using Microsoft.ServiceHosting.ServiceRuntime;
  31. [assembly: CLSCompliant(true)]
  32. namespace Microsoft.Samples.ServiceHosting.AspProviders
  33. {
  34. internal static class Configuration
  35. {
  36. internal const string DefaultMembershipTableNameConfigurationString = "DefaultMembershipTableName";
  37. internal const string DefaultRoleTableNameConfigurationString = "DefaultRoleTableName";
  38. internal const string DefaultSessionTableNameConfigurationString = "DefaultSessionTableName";
  39. internal const string DefaultSessionContainerNameConfigurationString = "DefaultSessionContainerName";
  40. internal const string DefaultProfileContainerNameConfigurationString = "DefaultProfileContainerName";
  41. internal const string DefaultProviderApplicationNameConfigurationString = "DefaultProviderApplicationName";
  42. internal const string DefaultMembershipTableName = "Membership";
  43. internal const string DefaultRoleTableName = "Roles";
  44. internal const string DefaultSessionTableName = "Sessions";
  45. internal const string DefaultSessionContainerName = "sessionprovidercontainer";
  46. internal const string DefaultProfileContainerName = "profileprovidercontainer";
  47. internal const string DefaultProviderApplicationName = "appname";
  48. internal static string GetConfigurationSetting(string configurationString, string defaultValue)
  49. {
  50. return GetConfigurationSetting(configurationString, defaultValue, false);
  51. }
  52. /// <summary>
  53. /// Gets a configuration setting from application settings in the Web.config or App.config file.
  54. /// When running in a hosted environment, configuration settings are read from the settings specified in
  55. /// .cscfg files (i.e., the settings are read from the fabrics configuration system).
  56. /// </summary>
  57. internal static string GetConfigurationSetting(string configurationString, string defaultValue, bool throwIfNull)
  58. {
  59. if (string.IsNullOrEmpty(configurationString)) {
  60. throw new ArgumentException("The parameter configurationString cannot be null or empty.");
  61. }
  62. string ret = null;
  63. // first, try to read from appsettings
  64. ret = TryGetAppSetting(configurationString);
  65. // settings in the csc file overload settings in Web.config
  66. if (RoleManager.IsRoleManagerRunning)
  67. {
  68. string cscRet = TryGetConfigurationSetting(configurationString);
  69. if (!string.IsNullOrEmpty(cscRet))
  70. {
  71. ret = cscRet;
  72. }
  73. // if there is a csc config name in the app settings, this config name even overloads the
  74. // setting we have right now
  75. string refWebRet = TryGetAppSetting(StorageAccountInfo.CSConfigStringPrefix + configurationString);
  76. if (!string.IsNullOrEmpty(refWebRet))
  77. {
  78. cscRet = TryGetConfigurationSetting(refWebRet);
  79. if (!string.IsNullOrEmpty(cscRet))
  80. {
  81. ret = cscRet;
  82. }
  83. }
  84. }
  85. // if we could not retrieve any configuration string set return value to the default value
  86. if (string.IsNullOrEmpty(ret) && defaultValue != null)
  87. {
  88. ret = defaultValue;
  89. }
  90. if (string.IsNullOrEmpty(ret) && throwIfNull)
  91. {
  92. throw new ConfigurationErrorsException(string.Format(CultureInfo.InstalledUICulture, "Cannot find configuration string {0}.", configurationString));
  93. }
  94. return ret;
  95. }
  96. internal static string GetConfigurationSettingFromNameValueCollection(NameValueCollection config, string valueName)
  97. {
  98. if (config == null)
  99. {
  100. throw new ArgumentNullException("config");
  101. }
  102. if (valueName == null) {
  103. throw new ArgumentNullException("valueName");
  104. }
  105. string sValue = config[valueName];
  106. if (RoleManager.IsRoleManagerRunning)
  107. {
  108. // settings in the hosting configuration are stronger than settings in app config
  109. string cscRet = TryGetConfigurationSetting(valueName);
  110. if (!string.IsNullOrEmpty(cscRet))
  111. {
  112. sValue = cscRet;
  113. }
  114. // if there is a csc config name in the app settings, this config name even overloads the
  115. // setting we have right now
  116. string refWebRet = config[StorageAccountInfo.CSConfigStringPrefix + valueName];
  117. if (!string.IsNullOrEmpty(refWebRet))
  118. {
  119. cscRet = TryGetConfigurationSetting(refWebRet);
  120. if (!string.IsNullOrEmpty(cscRet))
  121. {
  122. sValue = cscRet;
  123. }
  124. }
  125. }
  126. return sValue;
  127. }
  128. internal static bool GetBooleanValue(NameValueCollection config, string valueName, bool defaultValue)
  129. {
  130. string sValue = GetConfigurationSettingFromNameValueCollection(config, valueName);
  131. if (string.IsNullOrEmpty(sValue))
  132. {
  133. return defaultValue;
  134. }
  135. bool result;
  136. if (bool.TryParse(sValue, out result))
  137. {
  138. return result;
  139. }
  140. else
  141. {
  142. throw new ConfigurationErrorsException(string.Format(CultureInfo.InstalledUICulture, "The value must be boolean (true or false) for property '{0}'.", valueName));
  143. }
  144. }
  145. internal static int GetIntValue(NameValueCollection config, string valueName, int defaultValue, bool zeroAllowed, int maxValueAllowed)
  146. {
  147. string sValue = GetConfigurationSettingFromNameValueCollection(config, valueName);
  148. if (string.IsNullOrEmpty(sValue))
  149. {
  150. return defaultValue;
  151. }
  152. int iValue;
  153. if (!Int32.TryParse(sValue, out iValue))
  154. {
  155. if (zeroAllowed)
  156. {
  157. throw new ConfigurationErrorsException(string.Format(CultureInfo.InstalledUICulture, "The value must be a non-negative 32-bit integer for property '{0}'.", valueName));
  158. }
  159. throw new ConfigurationErrorsException(string.Format(CultureInfo.InstalledUICulture, "The value must be a positive 32-bit integer for property '{0}'.", valueName));
  160. }
  161. if (zeroAllowed && iValue < 0)
  162. {
  163. throw new ConfigurationErrorsException(string.Format(CultureInfo.InstalledUICulture, "The value must be a non-negative 32-bit integer for property '{0}'.", valueName));
  164. }
  165. if (!zeroAllowed && iValue <= 0)
  166. {
  167. throw new ConfigurationErrorsException(string.Format(CultureInfo.InstalledUICulture, "The value must be a positive 32-bit integer for property '{0}'.", valueName));
  168. }
  169. if (maxValueAllowed > 0 && iValue > maxValueAllowed)
  170. {
  171. throw new ConfigurationErrorsException(string.Format(CultureInfo.InstalledUICulture, "The value '{0}' can not be greater than '{1}'.", valueName, maxValueAllowed.ToString(CultureInfo.InstalledUICulture)));
  172. }
  173. return iValue;
  174. }
  175. internal static string GetStringValue(NameValueCollection config, string valueName, string defaultValue, bool nullAllowed)
  176. {
  177. string sValue = GetConfigurationSettingFromNameValueCollection(config, valueName);
  178. if (string.IsNullOrEmpty(sValue) && nullAllowed)
  179. {
  180. return null;
  181. }
  182. else if (string.IsNullOrEmpty(sValue) && defaultValue != null)
  183. {
  184. return defaultValue;
  185. }
  186. else if (string.IsNullOrEmpty(sValue))
  187. {
  188. throw new ConfigurationErrorsException(string.Format(CultureInfo.InstalledUICulture, "The parameter '{0}' must not be empty.", valueName));
  189. }
  190. return sValue;
  191. }
  192. internal static string GetStringValueWithGlobalDefault(NameValueCollection config, string valueName, string defaultConfigString, string defaultValue, bool nullAllowed)
  193. {
  194. string sValue = GetConfigurationSettingFromNameValueCollection(config, valueName);
  195. if (string.IsNullOrEmpty(sValue))
  196. {
  197. sValue = GetConfigurationSetting(defaultConfigString, null);
  198. }
  199. if (string.IsNullOrEmpty(sValue) && nullAllowed)
  200. {
  201. return null;
  202. }
  203. else if (string.IsNullOrEmpty(sValue) && defaultValue != null)
  204. {
  205. return defaultValue;
  206. }
  207. else if (string.IsNullOrEmpty(sValue))
  208. {
  209. throw new ConfigurationErrorsException(string.Format(CultureInfo.InstalledUICulture, "The parameter '{0}' must not be empty.", valueName));
  210. }
  211. return sValue;
  212. }
  213. internal static string GetInitExceptionDescription(StorageAccountInfo table, StorageAccountInfo blob) {
  214. StringBuilder builder = new StringBuilder();
  215. builder.Append(GetInitExceptionDescription(table, "table storage configuration"));
  216. builder.Append(GetInitExceptionDescription(blob, "blob storage configuration"));
  217. return builder.ToString();
  218. }
  219. internal static string GetInitExceptionDescription(StorageAccountInfo info, string desc) {
  220. StringBuilder builder = new StringBuilder();
  221. builder.Append("The reason for this exception is typically that the endpoints are not correctly configured. " + Environment.NewLine);
  222. if (info == null)
  223. {
  224. builder.Append("The current " + desc + " is null. Please specify a table endpoint!" + Environment.NewLine);
  225. }
  226. else
  227. {
  228. string baseUri = (info.BaseUri == null) ? "null" : info.BaseUri.ToString();
  229. builder.Append("The current " + desc + " is: " + baseUri + ", usePathStyleUris is: " + info.UsePathStyleUris + Environment.NewLine);
  230. builder.Append("Please also make sure that the account name and the shared key are specified correctly. This information cannot be shown here because of security reasons.");
  231. }
  232. return builder.ToString();
  233. }
  234. private static string TryGetConfigurationSetting(string configName)
  235. {
  236. string ret = null;
  237. try
  238. {
  239. ret = RoleManager.GetConfigurationSetting(configName);
  240. }
  241. catch (RoleException)
  242. {
  243. return null;
  244. }
  245. return ret;
  246. }
  247. [System.Diagnostics.CodeAnalysis.SuppressMessage ("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes",
  248. Justification = "Make sure that no error condition prevents environment from reading service configuration.")]
  249. private static string TryGetAppSetting(string configName)
  250. {
  251. string ret = null;
  252. try
  253. {
  254. ret = ConfigurationSettings.AppSettings[configName];
  255. }
  256. // some exception happened when accessing the app settings section
  257. // most likely this is because there is no app setting file
  258. // this is not an error because configuration settings can also be located in the cscfg file, and explicitly
  259. // all exceptions are captured here
  260. catch (Exception)
  261. {
  262. return null;
  263. }
  264. return ret;
  265. }
  266. }
  267. }