数码幽灵的自学Blog

.Net 学习历程

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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

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


对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之外的元素中的配置
object settings =
            ConfigurationSettings.GetConfig(
"appSettings");
NameValueCollection nvc 
= settings as NameValueCollection;
if (nvc != null)
{
  
string val = (string)nvc["xxx"];
}



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





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



一个自定义Section Handler的例子:

首先,自己的配置Xml文件:

<configuration>
  
<acmeGroup>
    
<acme>
      
<font>Courier New</font>
      
<backgroundColor>Green</backgroundColor>
      
<underlineLinks>true</underlineLinks>
      
<horizontalWidth>600</horizontalWidth>
      
<verticalWidth>800</verticalWidth>
     
</acme>
  
</acmeGroup>
</configuration>


然后是对应的一个保存数据的映射类:
// File: AcmeSettings.cs
namespace EssentialAspDotNet.Config
{
  
public class AcmeSettings
  
{
    
public string Font;
    
public string BackgroundColor;
    
public bool   UnderlineLinks;
    
public int    HorizontalWidth;
    
public int    VerticalWidth;
  }

}





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

// File: AcmeConfigHandler.cs
namespace EssentialAspDotNet.Config
{
  
public class AcmeConfigHandler :
                               IConfigurationSectionHandler
  
{
    
public object Create(object parent, object input,
                         XmlNode node)
    
{
      AcmeSettings aset 
= new AcmeSettings();

      
foreach (XmlNode n in node.ChildNodes)
      
{
        
switch (n.Name)
        
{
          
case ("font"):
            aset.Font 
= n.InnerText;
            
break;
          
case ("backgroundColor"):
            aset.BackgroundColor 
= n.InnerText;
            
break;
          
case ("underlineLinks"):
            aset.UnderlineLinks 
= bool.Parse(n.InnerText);
            
break;
          
case ("horizontalWidth"):
            aset.HorizontalWidth 
= int.Parse(n.InnerText);
            
break;
          
case ("verticalWidth"):
            aset.VerticalWidth 
= int.Parse(n.InnerText);
            
break;
        }

      }

      
return aset;
    }

  }

}




当然,如果不原意实现自己的Handler的话,也可以使用和AppSettings相同的策略来读取配置,但是必须使用
<add>元素来标记配置;实际上就是一个键值对,配置文件如下:
<configuration>
  
<configSections>
     
<section name="myGroup"
    type
="System.Configuration.NameValueFileSectionHandler, System, Version=1.0.3300.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"
/>

  
</configSections>

  
<myGroup>
    
<add key="font" value="Courier New"/>
    
<add key="backgroundColor" value="Green"/>
    
<add key="underlineLinks" value="true"/>
    
<add key="horizontalWidth" value="600"/>
    
<add key="verticalWidth" value="800"/>
  
</myGroup>

</configuration>



相应使用的方法如下:

// File: TestAcmeSettings.aspx
protected void Page_Load(object src, EventArgs e)
{
  NameValueCollection 
set;
  
set = ConfigurationSettings.GetConfig("myGroup")
        
as NameValueCollection;

  
// use set here (like set["Font"],
  
// set["BackgroundColor"], etc.)
}


posted on 2004-07-10 13:55  数码幽灵  阅读(1615)  评论(0编辑  收藏  举报