读取webconfig中自定义的xml  处理对特定的配置节的访问。

webconfig

  1. <configSections>
  2. <section name="NopConfig" type="BotanicSystem.Core.Configuration.NopConfig, BotanicSystem.Core" requirePermission="false" />
  3. </configSections>
  1. <NopConfig>
  2. <!-- Web farm support
  3. Enable "MultipleInstancesEnabled" if you run multiple instances.
  4. Enable "RunOnAzureWebsites" if you run on Windows Azure Web sites (not cloud services). -->
  5. <WebFarms MultipleInstancesEnabled="False" RunOnAzureWebsites="False" />
  6. <!-- Windows Azure BLOB storage. Specify your connection string, container name, end point for BLOB storage here -->
  7. <AzureBlobStorage ConnectionString="" ContainerName="" EndPoint="" />
  8. <!-- Redis support (used by web farms, Azure, etc). Find more about it at https://azure.microsoft.com/en-us/documentation/articles/cache-dotnet-how-to-use-azure-redis-cache/ -->
  9. <RedisCaching Enabled="false" ConnectionString="localhost" />
  10. <!-- You can get the latest version of user agent strings at http://browscap.org/ -->
  11. <UserAgentStrings databasePath="~/App_Data/browscap.xml" />
  12. <!-- Set the setting below to "False" if you did not upgrade from one of the previous versions. It can slightly improve performance -->
  13. <SupportPreviousNopcommerceVersions Enabled="True" />
  14. <!-- Do not edit this element. For advanced users only -->
  15. <Installation DisableSampleDataDuringInstallation="False" UseFastInstallationService="False" PluginsIgnoredDuringInstallation="" />
  16. </NopConfig>

解析读取

  1. /// <summary>
  2. /// Represents a NopConfig
  3. /// </summary>
  4. public partial class NopConfig : IConfigurationSectionHandler
  5. {
  6. /// <summary>
  7. /// Creates a configuration section handler.
  8. /// </summary>
  9. /// <param name="parent">Parent object.</param>
  10. /// <param name="configContext">Configuration context object.</param>
  11. /// <param name="section">Section XML node.</param>
  12. /// <returns>The created section handler object.</returns>
  13. public object Create(object parent, object configContext, XmlNode section)
  14. {
  15. var config = new NopConfig();
  16.  
  17. var startupNode = section.SelectSingleNode("Startup");
  18. config.IgnoreStartupTasks = GetBool(startupNode, "IgnoreStartupTasks");
  19.  
  20. var redisCachingNode = section.SelectSingleNode("RedisCaching");
  21. config.RedisCachingEnabled = GetBool(redisCachingNode, "Enabled");
  22. config.RedisCachingConnectionString = GetString(redisCachingNode, "ConnectionString");
  23.  
  24. var userAgentStringsNode = section.SelectSingleNode("UserAgentStrings");
  25. config.UserAgentStringsPath = GetString(userAgentStringsNode, "databasePath");
  26.  
  27. var supportPreviousNopcommerceVersionsNode = section.SelectSingleNode("SupportPreviousNopcommerceVersions");
  28. config.SupportPreviousNopcommerceVersions = GetBool(supportPreviousNopcommerceVersionsNode, "Enabled");
  29.  
  30. var webFarmsNode = section.SelectSingleNode("WebFarms");
  31. config.MultipleInstancesEnabled = GetBool(webFarmsNode, "MultipleInstancesEnabled");
  32. config.RunOnAzureWebsites = GetBool(webFarmsNode, "RunOnAzureWebsites");
  33.  
  34. var azureBlobStorageNode = section.SelectSingleNode("AzureBlobStorage");
  35. config.AzureBlobStorageConnectionString = GetString(azureBlobStorageNode, "ConnectionString");
  36. config.AzureBlobStorageContainerName = GetString(azureBlobStorageNode, "ContainerName");
  37. config.AzureBlobStorageEndPoint = GetString(azureBlobStorageNode, "EndPoint");
  38.  
  39. var installationNode = section.SelectSingleNode("Installation");
  40. config.DisableSampleDataDuringInstallation = GetBool(installationNode, "DisableSampleDataDuringInstallation");
  41. config.UseFastInstallationService = GetBool(installationNode, "UseFastInstallationService");
  42. config.PluginsIgnoredDuringInstallation = GetString(installationNode, "PluginsIgnoredDuringInstallation");
  43.  
  44. return config;
  45. }
  46.  
  47. private string GetString(XmlNode node, string attrName)
  48. {
  49. return SetByXElement<string>(node, attrName, Convert.ToString);
  50. }
  51.  
  52. private bool GetBool(XmlNode node, string attrName)
  53. {
  54. return SetByXElement<bool>(node, attrName, Convert.ToBoolean);
  55. }
  56.  
  57. private T SetByXElement<T>(XmlNode node, string attrName, Func<string, T> converter)
  58. {
  59. if (node == null || node.Attributes == null) return default(T);
  60. var attr = node.Attributes[attrName];
  61. if (attr == null) return default(T);
  62. var attrVal = attr.Value;
  63. return converter(attrVal);
  64. }
  65.  
  66. /// <summary>
  67. /// Indicates whether we should ignore startup tasks
  68. /// </summary>
  69. public bool IgnoreStartupTasks { get; private set; }
  70.  
  71. /// <summary>
  72. /// Path to database with user agent strings
  73. /// </summary>
  74. public string UserAgentStringsPath { get; private set; }
  75.  
  76. /// <summary>
  77. /// Indicates whether we should use Redis server for caching (instead of default in-memory caching)
  78. /// </summary>
  79. public bool RedisCachingEnabled { get; private set; }
  80. /// <summary>
  81. /// Redis connection string. Used when Redis caching is enabled
  82. /// </summary>
  83. public string RedisCachingConnectionString { get; private set; }
  84.  
  85. /// <summary>
  86. /// Indicates whether we should support previous nopCommerce versions (it can slightly improve performance)
  87. /// </summary>
  88. public bool SupportPreviousNopcommerceVersions { get; private set; }
  89.  
  90. /// <summary>
  91. /// A value indicating whether the site is run on multiple instances (e.g. web farm, Windows Azure with multiple instances, etc).
  92. /// Do not enable it if you run on Azure but use one instance only
  93. /// </summary>
  94. public bool MultipleInstancesEnabled { get; private set; }
  95.  
  96. /// <summary>
  97. /// A value indicating whether the site is run on Windows Azure Websites
  98. /// </summary>
  99. public bool RunOnAzureWebsites { get; private set; }
  100.  
  101. /// <summary>
  102. /// Connection string for Azure BLOB storage
  103. /// </summary>
  104. public string AzureBlobStorageConnectionString { get; private set; }
  105. /// <summary>
  106. /// Container name for Azure BLOB storage
  107. /// </summary>
  108. public string AzureBlobStorageContainerName { get; private set; }
  109. /// <summary>
  110. /// End point for Azure BLOB storage
  111. /// </summary>
  112. public string AzureBlobStorageEndPoint { get; private set; }
  113.  
  114. /// <summary>
  115. /// A value indicating whether a store owner can install sample data during installation
  116. /// </summary>
  117. public bool DisableSampleDataDuringInstallation { get; private set; }
  118. /// <summary>
  119. /// By default this setting should always be set to "False" (only for advanced users)
  120. /// </summary>
  121. public bool UseFastInstallationService { get; private set; }
  122. /// <summary>
  123. /// A list of plugins ignored during nopCommerce installation
  124. /// </summary>
  125. public string PluginsIgnoredDuringInstallation { get; private set; }
  126. }

使用

  1. var config = ConfigurationManager.GetSection("NopConfig") as NopConfig;

IConfigurationSectionHandler  是 在System.Configuration 下

IConfigurationSectionHandler 使用~的更多相关文章

  1. 使用IConfigurationSectionHandler在web.config中增加自定义配置

    一. 场景    这里仅举一个简单应用的例子,我希望在web.config里面增加网站的基本信息,如:网站名称,网站版本号,是否将网站暂时关闭等.二. 基本实现方法1. 定义配置节点对应的类:Site ...

  2. (转).net webconfig使用IConfigurationSectionHandler自定section

    自定义配置结构 (使用IConfigurationSectionHandler) 假设有以下的配置信息,其在MyInfo可以重复许多次,那么应如何读取配置呢?这时就要使用自定义的配置程序了.<m ...

  3. IConfigurationSectionHandler 接口

    IConfigurationSectionHandler 处理对特定的配置节的访问. 示例代码: public class MyConfig : IConfigurationSectionHandle ...

  4. .Net——实现IConfigurationSectionHandler接口定义处理程序处理自己定义节点

    除了使用.net里面提供的内置处理程序来处理我们的自己定义节点外,我们还能够通过多种方法,来自己定义处理类处理我们的自己定义节点,本文主要介绍通过实现IConfigurationSectionHand ...

  5. .Net——实现IConfigurationSectionHandler接口定义处理程序处理自定义节点

    除了使用.net里面提供的内置处理程序来处理我们的自定义节点外,我们还可以通过多种方法,来自己定义处理类处理我们的自定义节点,本文主要介绍通过实现IConfigurationSectionHandle ...

  6. spring.net 框架分析(三)ContextRegistry.GetContext()

    我们通过ContextRegistry.GetContext()建立了一个IApplicationContext得实例,那么这个实例具体是怎么建立的了. 我们来分析一下容器实例建立的过程: 我们在配置 ...

  7. C# 自定义Section

    一.在App.config中自定义Section,这个使用了SectionGroup <?xml version="1.0" encoding="utf-8&quo ...

  8. Url路径重写的原理

    ASP.net的地址重写(URLRewriter)实现原理及代码示例 吴剑 2007-01-01 原创文章,转载必需注明出处:http://www.cnblogs.com/wu-jian/ 概述 访问 ...

  9. 第13章 .NET应用程序配置

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...

随机推荐

  1. PHP array 操作函数

    array_map 函数的介绍 将数组的每个单元使用回调函数格式: array_map(callback, array) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

  2. iOS On-Demand Resources简单理解

    ios9引入了一个新功能,On-Demand Resources,它是app thinning 的一部分.这个机能简单的说,就是在下载app的时候,app中包含的不重要资源不下载,等到需要时,在由系统 ...

  3. SAP 销售订单的文本项目

    http://blog.itpub.net/9859323/viewspace-616508/ ls_hdname = wa_vbak-vbeln .     CALL FUNCTION 'READ_ ...

  4. sublime text3 输入中文的解决方法

    1. 下载我们需要的文件,打开终端 ,输入: git clone https://github.com/lyfeyaj/sublime-text-imfix.git 2. 将下载的文件解压之后,移到当 ...

  5. Android笔记:HTTP相关

    发送HTTP请求 HttpURLConnection.HttpClient XML解析 Pull 解析.SAX 解析.DOM 解析 解析JSON 格式数据 官方提供的JSONObject.谷歌的开源库 ...

  6. hbase集群的启动,注意几个问题

    1.hbase的改的会影响器他的组件的使用, 故而, 在修改 hadoop的任何组件后, 一定要记得其它的组件也能受到影响, 一下是我在将hadoop的集群改了之后 , 再次运行hbase的时候, 就 ...

  7. Forward-backward梯度求导(tensorflow word2vec实例)

    考虑不可分的例子         通过使用basis functions 使得不可分的线性模型变成可分的非线性模型 最常用的就是写出一个目标函数 并且使用梯度下降法 来计算     梯度的下降法的梯度 ...

  8. Logging vs NoLogging

    You Asked My Prod environments is like this. Three Node RAC, Active Data guard enabled. There is a p ...

  9. Asp.Net MVC4 + Oracle + EasyUI 学习 序章

    Asp.Net MVC4 + Oracle + EasyUI  序章 -- 新建微软实例 本文链接:http://www.cnblogs.com/likeli/p/4233387.html 1.  简 ...

  10. NPOIExcel

    public class NPOIExcel { private string _title; private string _sheetName; private string _filePath; ...