作者 :Saad Ladki
摘要
本文介绍如何使用 Microsoft.Web.Administration API 以编程方式访问 IIS 配置文件(applicationHost.config 和 web.config)中的配置部分。 它处理包含嵌套元素和集合的更复杂的部分。 使用 IIS_schema.xml 文件中定义的真实 IIS 配置部分的示例。 本文介绍如何使用 Microsoft.Web.Administration 提供的基类以及强类型类来表示这些节,以泛型方式访问和操作这些节。
介绍
本文提供了使用 Microsoft.Web.Administration API 以编程方式访问 IIS 配置部分的示例。 这将处理具有嵌套元素(system.webServer/asp)和集合(system.webServer/security/isapiCgiRestriction)的部分。 第一节演示如何使用 Microsoft.Web.Administration 提供的泛型基类来操作这些节,第二节提供了表示这些节的强类型包装类的使用方式的示例。
包含嵌套元素和集合的节的架构
包含嵌套元素的节
包含嵌套元素的节的示例是 system.webServer/asp 节。 “session”元素在“asp”节中定义。 本节的架构如下所示。 架构和其他嵌套元素中还有其他属性,这些属性未显示在示例中。
<sectionSchema name="system.webServer/asp">
<attribute name="appAllowClientDebug" type="bool" defaultValue="false" />
<attribute name="appAllowDebugging" type="bool" defaultValue="false" />
<!-- Omitted to simplify -->
<element name="session">
<attribute name="allowSessionState" type="bool" defaultValue="true" />
<attribute name="keepSessionIdSecure" type="bool" defaultValue="true" />
</element>
</sectionSchema>
包含集合的部分
使用键对元素集合进行索引的配置节是 system.webServer/security/isapiCgiRestriction 配置节。 该部分的架构如下所示。
<sectionSchema name="system.webServer/security/isapiCgiRestriction">
<collection addElement="add" clearElement="clear" removeElement="remove">
<attribute name="path" type="string" expanded="true" required="true" isUniqueKey="true" validationType="nonEmptyString" />
<attribute name="allowed" type="bool" required="true" defaultValue="false" />
<attribute name="groupId" type="string" required="false" />
<attribute name="description" type="string" />
</collection>
<attribute name="notListedIsapisAllowed" type="bool" defaultValue="false" />
<attribute name="notListedCgisAllowed" type="bool" defaultValue="false" />
</sectionSchema>
对分区的通用访问
Microsoft.Web.Administration 提供了多个泛型基类,这些基类表示配置系统中的不同组件。
- 配置: 表示单个配置文件(applicationHost.config 或站点和应用程序的 web.config 文件)
- ConfigurationElement: 用于表示配置文件中的元素的泛型实体。 这是配置节、集合项、节中的嵌套元素等的基类。
- ConfigurationAttribute: 表示 ConfigurationElement 中的属性
- ConfigurationSection: 派生自 ConfigurationElement 并表示 IIS 架构文件中定义的节。 用于访问节的各种属性。
- ConfigurationElementCollection: 由 ConfigurationElements 组成的集合类。 也派生自 ConfigurationElement。
下面的示例演示如何在 system.webServer/asp 节中访问嵌套元素。
static void Main(string[] args) {
ServerManager serverManager = new ServerManager();
Configuration config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection section = config.GetSection("system.webServer/asp");
ConfigurationElement element = section.GetChildElement("session");
Console.Write("allowSessionState attribute value: ");
Console.WriteLine(element.GetAttributeValue("allowSessionState"));
Console.WriteLine("Set allowSessionState value to false");
element.SetAttributeValue("allowSessionState", false);
Console.Write("allowSessionState attribute value: ");
Console.WriteLine(element.GetAttributeValue("allowSessionState"));
serverManager.CommitChanges();
}
ServerManager 类是访问各种服务器设置的入口点。 我们在 applicationHost.config 文件中设置属性,但也可以在各个站点和应用程序的 web.config 文件中访问和更改这些属性。 CommitChanges 调用保存更改的设置。
下一个示例演示如何循环访问集合中的元素并向其添加元素。
static void Main(string[] args) {
ServerManager serverManager = new ServerManager();
Configuration config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection section =
config.GetSection("system.webServer/security/isapiCgiRestriction");
ConfigurationElementCollection collection = section.GetCollection();
// Iterating through the elements in the collection foreach (ConfigurationElement element in collection)
{
Console.Write("Path: " + element.GetAttributeValue("path"));
Console.WriteLine(" Allowed: " + element.GetAttributeValue("allowed"));
}
// Adding a new element to the collection ConfigurationElement newElement = collection.CreateElement();
newElement.SetAttributeValue("path", @"%SystemDir%\someDll.dll");
newElement.SetAttributeValue("allowed", false);
collection.Add(newElement);
serverManager.CommitChanges();
}
用于访问分区的强类型类
可以编写基于泛型基类的强类型类,以便更容易、更直观地访问属性。 当可能必须多次访问同一部分以执行不同操作时,这特别有用。 这些类还可确保对属性类型进行编译时检查。 它们避免了对 GetChildElement 和 GetAttributeValue 等方法的繁琐调用。
下面的示例显示了 system.webServer/asp 节的包装器和该部分的“session”元素。 此代码片段中不包含该节中的所有属性。
public sealed class AspSection : ConfigurationSection {
private AspSession _session;
public AspSection()
{
}
public AspSession Session
{
get
{
if (_session == null)
{
_session = (AspSession)GetChildElement("session",
typeof(AspSession));
}
return _session;
}
}
}
public sealed class AspSession : ConfigurationElement {
public AspSession()
{
}
public bool AllowSessionState
{
get {
return (bool)base["allowSessionState"];
}
set
{
base["allowSessionState"] = value;
}
}
}
上面的示例中的 GetChildElement 调用用于访问配置元素(节、集合元素等)中的嵌套元素。 调用 base... 包装 GetAttributeValue 和 SetAttributeValue 类,这些类检索和设置这些属性的值。
下一个示例演示如何以强类型方式访问嵌套元素中的属性。
static void Main(string[] args) {
ServerManager serverManager = new ServerManager();
Configuration config = serverManager.GetApplicationHostConfiguration();
AspSection section = (AspSection)config.GetSection("system.webServer/asp",
typeof(AspSection));
Console.WriteLine(section.Session.AllowSessionState);
section.Session.AllowSessionState = false;
serverManager.CommitChanges();
}
观察对 config.GetSection 的调用,此调用需要一个节路径以及要创建的节类型。 默认情况下,调用 config.GetSection 会创建一个 ConfigurationSection,如果需要获取一个强类型包装器实例,则需要传入类型。
这里是如何为具有集合的部分创建强类型类的示例。 此示例使用 system.webServer/security/isapiCgiRestriction 节。 此代码片段不包括本节中存在的所有属性。
public sealed class IsapiCgiRestrictionSection : ConfigurationSection {
private IsapiCgiRestrictionCollection _collection;
public IsapiCgiRestrictionSection() {
}
public IsapiCgiRestrictionCollection IsapiCgiRestrictions {
get {
if (_collection == null) {
_collection =
(IsapiCgiRestrictionCollection)GetCollection(typeof(IsapiCgiRestrictionCollection));
}
return _collection;
}
}
}
基类 (ConfigurationElement) 的 GetCollection 方法用于访问该元素的默认集合。
下一个示例显示集合本身的强类型类和集合中的元素。
public sealed class IsapiCgiRestrictionCollection : ConfigurationElementCollectionBase<IsapiCgiRestrictionElement> {
public IsapiCgiRestrictionCollection() {
}
public new IsapiCgiRestrictionElement this[string path] {
get {
for (int i = 0; i< Count; i++) {
IsapiCgiRestrictionElement restriction = base[i];
if (String.Equals=(Environment.ExpandEnvironmentVariables(restriction.Path),
Environment.ExpandEnvironmentVariables(path), StringComparison.OrdinalIgnoreCase)) {
return restriction;
}
}
return null;
}
}
public IsapiCgiRestrictionElement Add(string path, bool allowed) {
IsapiCgiRestrictionElement element = CreateElement();
element.Path = path;
element.Allowed = allowed;
return Add(element);
}
protected override IsapiCgiRestrictionElement CreateNewElement(string elementTagName) {
return new IsapiCgiRestrictionElement();
}
}
public sealed class IsapiCgiRestrictionElement : ConfigurationElement {
public IsapiCgiRestrictionElement() {
}
public bool Allowed {
get {
return (bool)base["allowed"];
}
set {
base["allowed"] = value;
}
}
public string Path {
get {
return (string)base["path"];
}
set {
base["path"] = value;
}
}
}
对于强类型的集合类,能够使用集合键对集合进行索引是非常有用的,在本例中,集合键是“path”属性。 Add 方法的参数采用该元素的必需属性。 在这种情况下,Add 只能采用“path”属性,然后“允许”属性将具有其默认值。
下面是使用这些强类型类循环访问集合中的条目并向其添加元素的示例。
static void Main(string[] args) {
ServerManager serverManager = new ServerManager();
Configuration config = serverManager.GetApplicationHostConfiguration();
IsapiCgiRestrictionSection section =
(IsapiCgiRestrictionSection)config.GetSection("system.webServer/security/isapiCgiRestriction", typeof(IsapiCgiRestrictionSection));
// Iterating through the elements in the collection
foreach (IsapiCgiRestrictionElement element in section.IsapiCgiRestrictions) {
Console.Write("Path: " + element.Path);
Console.WriteLine(" Allowed: " + element.Allowed);
}
// Adding a new element to the collection
section.IsapiCgiRestrictions.Add(@"%SystemDir%\someDll.dll", false);
serverManager.CommitChanges();
}