扩展配置文件结构
2010-11-16 08:07
281 查看
<appSetting>元素保存应用程序使用自定义信息。它有两个明显的限制,首先,它不能保存结构化的信息,如列表。
2:它不能灵活的处理各种数据类型。它只能使用单一字符串。
幸好asp.net使用一个模块化的高度可扩展的配置模型。
有三个基本的步骤:
1:确定要配置文件中保存的信息以及如何把他们组织到元素和特性。最理想的是,对于每个概念上相关的设置组都有一个元素,通过特性保存它们。
2:对于每个新元素,创建一个C#类封装它的信息。
3:在配置文件里注册新节。
编码过程:
[b]1:创建Section类[/b]
</appSettings>
<connectionStrings/>
<!--<orderService available="true" pollTimeout="00:01:00"
location="tcp://OrderComputer:8010/OrderService"/>-->
[b] <orderService available="true" pollTimeout="00:01:00">[/b]
[b] <location computer="OrderComputer" port="8010" endpoint="OrderService"/>[/b]
[b] </orderService>[/b]
<system.web>
这个是我自定义的<orderService>
public class OrderService : ConfigurationSection
{
[ConfigurationProperty("available",
IsRequired = false, DefaultValue = true)]
public bool Available
{
get { return (bool)base["available"]; }
set { base["available"] = value; }
}
[ConfigurationProperty("pollTimeout",
IsRequired = true)]
public TimeSpan PollTimeout
{
get { return (TimeSpan)base["pollTimeout"]; }
set { base["pollTimeout"] = value; }
}
[ConfigurationProperty("location",
IsRequired = true)]
public Location Location
{
get { return (Location)base["location"]; }
set { base["location"] = value; }
}
}
public class Location : ConfigurationElement
{
[ConfigurationProperty("computer",
IsRequired = true)]
public string Computer
{
get { return (string)base["computer"]; }
set { base["computer"] = value; }
}
[ConfigurationProperty("port",
IsRequired = true)]
public int Port
{
get { return (int)base["port"]; }
set { base["port"] = value; }
}
[ConfigurationProperty("endpoint",
IsRequired = true)]
public string Endpoint
{
get { return (string)base["endpoint"]; }
set { base["endpoint"] = value; }
}
}
可以看到每个属性都使用ConfigurationProperty特性映射到相应的特性名称。
[b]2:注册Section类[/b]
<configSections>
<section name="orderService" type="OrderService"/>
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<sectionGroup name="scripting" type="System.Web.Con...
[b]3:如果调用[/b]
Configuration config =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(
Request.ApplicationPath );
OrderService custSection = (OrderService)config.GetSection("orderService");
lblInfo.Text += "Retrieved service information...<br />" +
"<b>Location (computer):</b> " + custSection.Location.Computer +
"<br /><b>Available:</b> " + custSection.Available.ToString() +
"<br /><b>Timeout:</b> " + custSection.PollTimeout.ToString() + "<br /><br />";
结果:
Retrieved service information...
Location (computer): OrderComputer
Available: True
Timeout: 00:01:00
This page was served at 2010-11-16 8:06:26
2:它不能灵活的处理各种数据类型。它只能使用单一字符串。
幸好asp.net使用一个模块化的高度可扩展的配置模型。
有三个基本的步骤:
1:确定要配置文件中保存的信息以及如何把他们组织到元素和特性。最理想的是,对于每个概念上相关的设置组都有一个元素,通过特性保存它们。
2:对于每个新元素,创建一个C#类封装它的信息。
3:在配置文件里注册新节。
编码过程:
[b]1:创建Section类[/b]
</appSettings>
<connectionStrings/>
<!--<orderService available="true" pollTimeout="00:01:00"
location="tcp://OrderComputer:8010/OrderService"/>-->
[b] <orderService available="true" pollTimeout="00:01:00">[/b]
[b] <location computer="OrderComputer" port="8010" endpoint="OrderService"/>[/b]
[b] </orderService>[/b]
<system.web>
这个是我自定义的<orderService>
public class OrderService : ConfigurationSection
{
[ConfigurationProperty("available",
IsRequired = false, DefaultValue = true)]
public bool Available
{
get { return (bool)base["available"]; }
set { base["available"] = value; }
}
[ConfigurationProperty("pollTimeout",
IsRequired = true)]
public TimeSpan PollTimeout
{
get { return (TimeSpan)base["pollTimeout"]; }
set { base["pollTimeout"] = value; }
}
[ConfigurationProperty("location",
IsRequired = true)]
public Location Location
{
get { return (Location)base["location"]; }
set { base["location"] = value; }
}
}
public class Location : ConfigurationElement
{
[ConfigurationProperty("computer",
IsRequired = true)]
public string Computer
{
get { return (string)base["computer"]; }
set { base["computer"] = value; }
}
[ConfigurationProperty("port",
IsRequired = true)]
public int Port
{
get { return (int)base["port"]; }
set { base["port"] = value; }
}
[ConfigurationProperty("endpoint",
IsRequired = true)]
public string Endpoint
{
get { return (string)base["endpoint"]; }
set { base["endpoint"] = value; }
}
}
可以看到每个属性都使用ConfigurationProperty特性映射到相应的特性名称。
[b]2:注册Section类[/b]
<configSections>
<section name="orderService" type="OrderService"/>
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<sectionGroup name="scripting" type="System.Web.Con...
[b]3:如果调用[/b]
Configuration config =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(
Request.ApplicationPath );
OrderService custSection = (OrderService)config.GetSection("orderService");
lblInfo.Text += "Retrieved service information...<br />" +
"<b>Location (computer):</b> " + custSection.Location.Computer +
"<br /><b>Available:</b> " + custSection.Available.ToString() +
"<br /><b>Timeout:</b> " + custSection.PollTimeout.ToString() + "<br /><br />";
结果:
Retrieved service information...
Location (computer): OrderComputer
Available: True
Timeout: 00:01:00
This page was served at 2010-11-16 8:06:26
相关文章推荐
- 用自定义的节来扩展web.config和machine.config配置文件的结构
- 扩展 web.config 配置文件结构
- 扩展 web.config 配置文件结构
- Nginx_PHP配置文件结构设计
- 用Nginx如何配置运行无扩展名PHP文件或非.PHP扩展名文件
- 微信小程序自学第一课:工程目录结构与.json文件配置
- 基于Spring可扩展Schema提供自定义配置支持(spring配置文件中 配置标签支持)
- WebService处理大数据量数据出错:运行配置文件中指定的扩展时出现异常。 ---> 超过了最大请求长度。.
- Cloudera Manager(CDH5)内部结构、功能包括配置文件、目录位置等 2016-05-26 15:46 2112人阅读 评论(0) 收藏 举报 分类: CDH(19) 1. 相关
- HTTP 错误 404.3 NOT FOUND 由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加MIME映射。
- svc 报“由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。“的HTTP 错误 404.3 – Not Found
- 基本目录结构、配置文件
- tomcat配置文件结构
- 一个典型的hibernate.cfg.xml的配置文件结构
- 第八集思科设备的内部主要结构以及IOS和配置文件 推荐
- redis 主从配置实例、配置文件说明、及备份方式,php redis 扩展安装
- Nginx配置文件结构
- 使用SlowCheetah扩展插件解决测试环境和生产环境xml配置文件内容切换
- Atitit 项目源码管理 attilax著 1. 源码结构sdk目录结构 1 1.1. 源码分类,配置文件,主程序文件,sql文件 1 2. 源码管理,提交,更新,与同步 1 2.1. 源码同步
- php扩展开发(加载不同配置文件)