IConfigurationSectionHandler 使用~

读取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 
 77 
 78         /// <summary>
 79         /// Indicates whether we should use Redis server for caching (instead of default in-memory caching)
 80         /// </summary>
 81         public bool RedisCachingEnabled { get; private set; }
 82         /// <summary>
 83         /// Redis connection string. Used when Redis caching is enabled
 84         /// </summary>
 85         public string RedisCachingConnectionString { get; private set; }
 86 
 87 
 88 
 89         /// <summary>
 90         /// Indicates whether we should support previous nopCommerce versions (it can slightly improve performance)
 91         /// </summary>
 92         public bool SupportPreviousNopcommerceVersions { get; private set; }
 93 
 94 
 95 
 96         /// <summary>
 97         /// A value indicating whether the site is run on multiple instances (e.g. web farm, Windows Azure with multiple instances, etc).
 98         /// Do not enable it if you run on Azure but use one instance only
 99         /// </summary>
100         public bool MultipleInstancesEnabled { get; private set; }
101 
102         /// <summary>
103         /// A value indicating whether the site is run on Windows Azure Websites
104         /// </summary>
105         public bool RunOnAzureWebsites { get; private set; }
106 
107         /// <summary>
108         /// Connection string for Azure BLOB storage
109         /// </summary>
110         public string AzureBlobStorageConnectionString { get; private set; }
111         /// <summary>
112         /// Container name for Azure BLOB storage
113         /// </summary>
114         public string AzureBlobStorageContainerName { get; private set; }
115         /// <summary>
116         /// End point for Azure BLOB storage
117         /// </summary>
118         public string AzureBlobStorageEndPoint { get; private set; }
119 
120 
121         /// <summary>
122         /// A value indicating whether a store owner can install sample data during installation
123         /// </summary>
124         public bool DisableSampleDataDuringInstallation { get; private set; }
125         /// <summary>
126         /// By default this setting should always be set to "False" (only for advanced users)
127         /// </summary>
128         public bool UseFastInstallationService { get; private set; }
129         /// <summary>
130         /// A list of plugins ignored during nopCommerce installation
131         /// </summary>
132         public string PluginsIgnoredDuringInstallation { get; private set; }
133     }
View Code

使用

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

 

IConfigurationSectionHandler  是 在System.Configuration 下

 

转载于:https://www.cnblogs.com/sunzgod/p/6225655.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值