设为首页 收藏本站
查看: 746|回复: 0

[经验分享] IConfigurationSectionHandler 使用~

[复制链接]

尚未签到

发表于 2017-7-1 09:12:15 | 显示全部楼层 |阅读模式
  读取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>
  解析读取


DSC0000.gif DSC0001.gif


  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 下

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-389914-1-1.html 上篇帖子: Java中创建访问HTTPS的自签名证书的方法 下篇帖子: Win10开发:一个简单的MobileService
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表