Asp.Net中使用Config文件是有层次关系的,就是常说的Web.Config文件;
对于放在不同虚拟目录下的Config文件的作用域是不一样的;就如下图所示:
Essential ASP.NET 读书笔记--Configuration

这样做优点是:可以为特殊配置的页面生成特殊的配置,当进行目录变更时把目录进行拷贝就可以;
            缺点是:一旦站点配置出现问题,需要对每个层次下的配置逐一检查;
对这样的层次模型举个例子:
Essential ASP.NET 读书笔记--Configuration

对WebConfig做的更改将会再下一个httprequest到来的时候生效;
但是需要注意的是这将导致SessionState和ApplicationState的丢失
一个常见的问题是我们要如何保护我们的Web.Config文件?幸运的是asp.net内建了HttpForbiddenHandler来实现对特定文件的限制访问(如.Config, .cs, .vb, .asax, .resx等),当试图访问这些扩展名的文件时 ,这个Handler将被调用,返回http error,达到了默认情况下外部客户端是无法访问config文件的目的

特殊的配置元素processModel:
特殊在以下几点:
         仅允许使用在系统范围的machine.config文件中
          改变这个元素后在工作线程重启前不会有任何效果
          其他的配置元素都被托管构件所使用,只有这个是被一个非托管外部模块aspnet_isapi.dll所读取

Attribute

Values

Default

Description

Enable

true | false

true

Whether ASP.NET is hosted in an external worker process (true) or directly in inetinfo.exe (false)

timeout

Infinite | HH:MM:SS

Infinite

Total life of a process梡rocess bounced after timeout

idleTimeout

Infinite | HH:MM:SS

Infinite

Total idle life of a process梡rocess bounced when reached

shutdownTimeout

Infinite | HH:MM:SS

0:00:05

Time given to process to shut down before being killed

requestLimit

Infinite | number

Infinite

Total number of requests to serve before bouncing process

requestQueueLimit

Infinite | number

5000

Number of queued requests allowed before bouncing process

restartQueueLimit

Infinite | number

10

Number of requests kept in queue while process is restarting

memoryLimit

Number

60

Percentage of physical memory process is allowed to use before bouncing process

webGarden

true | false

false

Whether process should be affinitized with a particular CPU (for multi-CPU machines)

cpuMask

Bitmask

0xffffffff

Controls number of CPUs available for ASP.NET worker processes (webGarden must be true)

userName

SYSTEM | MACHINE | username

MACHINE

Windows identity to run the worker process in (MACHINE uses low-privileged ASPNET account)

Password

AutoGenerate | password

AutoGenerate

Password for username

logLevel

All | None | Errors

Errors

Event types logged to event log

clientConnectedCheck

HH:MM:SS

0:00:05

Time a request is left in the queue before a client-connected check is performed

comAuthenticationLevel

Default | None | Connect | Call | Pkt | PktIntegrity | PktPrivacy

Connect

Level of authentication for DCOM security

comImpersonationLevel

Default | Anonymous | Identify | Impersonate | Delegate

Impersonate

Authentication level for COM security

responseRestartDeadlockInterval

Infinite | HH:MM:SS

00:09:00

Time to wait between restarting worker process because of responseRestartDeadlockInterval

responseDeadlockInterval

Infinite | HH:MM:SS

00:03:00

For deadlock detection, timeout for responses when there are queued requests

maxWorkerThreads

Number

25

Maximum number of I/O threads per CPU in the thread pool

maxIoThreads

Number

25

Maximum number of I/O threads per CPU in the thread pool

serverErrorMessageFile

File name

""

Customization for "Server Unavailable" message


读取配置文件:
使用以下代码,可以方便的读取每个除ProcessModel之外的元素中的配置
Essential ASP.NET 读书笔记--Configurationobject settings =
Essential ASP.NET 读书笔记--Configuration            ConfigurationSettings.GetConfig(
"appSettings");
Essential ASP.NET 读书笔记--ConfigurationNameValueCollection nvc 
= settings as NameValueCollection;
Essential ASP.NET 读书笔记--Configuration
if (nvc != null)

而静态索引器ConfigurationSettings.AppSettings["xxx"]就是对以上方法的一个方便的包装;





理论上说.Net的Config文件被分为两部分:configuration section handlers和configuration data



一个自定义Section Handler的例子:

首先,自己的配置Xml文件:
Essential ASP.NET 读书笔记--Configuration
Essential ASP.NET 读书笔记--Configuration
<configuration>
Essential ASP.NET 读书笔记--Configuration  
<acmeGroup>
Essential ASP.NET 读书笔记--Configuration    
<acme>
Essential ASP.NET 读书笔记--Configuration      
<font>Courier New</font>
Essential ASP.NET 读书笔记--Configuration      
<backgroundColor>Green</backgroundColor>
Essential ASP.NET 读书笔记--Configuration      
<underlineLinks>true</underlineLinks>
Essential ASP.NET 读书笔记--Configuration      
<horizontalWidth>600</horizontalWidth>
Essential ASP.NET 读书笔记--Configuration      
<verticalWidth>800</verticalWidth>
Essential ASP.NET 读书笔记--Configuration     
</acme>
Essential ASP.NET 读书笔记--Configuration  
</acmeGroup>
Essential ASP.NET 读书笔记--Configuration
</configuration>
Essential ASP.NET 读书笔记--Configuration


然后是对应的一个保存数据的映射类:
Essential ASP.NET 读书笔记--Configuration// File: AcmeSettings.cs
Essential ASP.NET 读书笔记--Configuration
namespace EssentialAspDotNet.Config



然后,实现自己的读取相应格式配置的Handler,实现一个接口IConfigurationSectionHandler:

Essential ASP.NET 读书笔记--Configuration// File: AcmeConfigHandler.cs
Essential ASP.NET 读书笔记--Configuration
namespace EssentialAspDotNet.Config


当然,如果不原意实现自己的Handler的话,也可以使用和AppSettings相同的策略来读取配置,但是必须使用
<add>元素来标记配置;实际上就是一个键值对,配置文件如下:
Essential ASP.NET 读书笔记--Configuration<configuration>
Essential ASP.NET 读书笔记--Configuration  
<configSections>
Essential ASP.NET 读书笔记--Configuration     
<section name="myGroup"
Essential ASP.NET 读书笔记--Configuration    type
="System.Configuration.NameValueFileSectionHandler, System, Version=1.0.3300.0, Culture=neutral,
Essential ASP.NET 读书笔记--ConfigurationPublicKeyToken=b77a5c561934e089"
/>
Essential ASP.NET 读书笔记--Configuration
Essential ASP.NET 读书笔记--Configuration  
</configSections>
Essential ASP.NET 读书笔记--Configuration
Essential ASP.NET 读书笔记--Configuration  
<myGroup>
Essential ASP.NET 读书笔记--Configuration    
<add key="font" value="Courier New"/>
Essential ASP.NET 读书笔记--Configuration    
<add key="backgroundColor" value="Green"/>
Essential ASP.NET 读书笔记--Configuration    
<add key="underlineLinks" value="true"/>
Essential ASP.NET 读书笔记--Configuration    
<add key="horizontalWidth" value="600"/>
Essential ASP.NET 读书笔记--Configuration    
<add key="verticalWidth" value="800"/>
Essential ASP.NET 读书笔记--Configuration  
</myGroup>
Essential ASP.NET 读书笔记--Configuration
Essential ASP.NET 读书笔记--Configuration
</configuration>
Essential ASP.NET 读书笔记--Configuration



相应使用的方法如下:

Essential ASP.NET 读书笔记--Configuration// File: TestAcmeSettings.aspx
Essential ASP.NET 读书笔记--Configuration
protected void Page_Load(object src, EventArgs e)

相关文章: