MWA を使用した IIS 7 スキーマの拡張およびカスタム セクションへのアクセス
公開日: 2007 年 12 月 5 日 (作業者: IIS Team (英語))
更新日: 2008 年 2 月 26 日 (作業者: IIS Team (英語))
概要
IIS 7 XML 構成ファイルでは、サード パーティが独自のカスタム構成セクションを作成することができます。このドキュメントでは、IIS 7 スキーマを拡張して、IIS 7 構成ファイル (applicationHost.config および web.config) で独自の構成セクションを設定/アクセスできるようにする方法を紹介します。ユーザーは、Microsoft.Web.Administration API を使用することで、一般的な方法によって、またはセクションを表す厳密に型指定されたクラスの作成によって、カスタム セクションのプロパティをプログラムによって取得および設定できます。
はじめに
このドキュメントでは、IIS 7 スキーマを拡張してカスタム構成セクションを設定する手順と、Microsoft.Web.Administration API を使用してそのセクションのプロパティにアクセスするためのコンソール アプリケーションを作成する手順について説明します。
Microsoft.Web.Administration には、構成システムのさまざまなコンポーネントを表す汎用基本クラスがいくつか用意されています。
- Configuration: 単一の構成ファイル (サイトやアプリケーションの applicationHost.config または web.config ファイル) を表します。
- ConfigurationElement: 構成ファイル内の 1 つの要素を表すために使用される汎用エンティティです。これは、構成セクション、コレクション エントリ、セクション内の入れ子になった要素などの基本クラスです。
- ConfigurationAttribute: ConfigurationElement 内の 1 つのプロパティを表します。
- ConfigurationSection: ConfigurationElement から派生し、IIS 7 スキーマ ファイルに定義されたセクションを表します。セクションのさまざまなプロパティにアクセスするために使用します。
- ConfigurationElementCollection: ConfigurationElement で構成されるコレクション クラスです。ConfigurationElement の派生クラスです。
特定のセクションを表す厳密に型指定されたクラスは、これらの基本クラスを派生して作成します。
スキーマの拡張
スキーマを拡張するには、単純な .xml ファイルをコンピューター上の %SystemRoot%\system32\inetsrv\config\schema ディレクトリに追加します。このディレクトリには IIS_schema.xml というファイルがあります。このファイルは、独自のスキーマを設定するための見本として利用できます。CUSTOM_schema.xml という名前のファイルを作成し、下に示すような内容に設定します。この例では、カスタム セクション (myCustomSection) にブール型、文字列型、整数型、列挙型の 4 つのプロパティがあります。これらのプロパティの既定値が、次のスキーマで指定されています。
<configSchema>
<sectionSchema name="myCustomSection">
<attribute name="booleanProperty" type="bool" defaultValue="false" />
<attribute name="stringProperty" type="string" />
<attribute name="integerProperty" type="int" defaultValue="100" />
<attribute name="enumProperty" type="enum" defaultValue="EnumValue1" >
<enum name="EnumValue1" value="0" />
<enum name="EnumValue2" value="1" />
</attribute>
</sectionSchema>
</configSchema>
このセクションを applicationHost.config に登録します。次に示すような 1 行を applicationHost.config に追加します。
<configSections>
<section name="myCustomSection" />
<sectionGroup name="system.applicationHost">
…
…
…
</configSections>
コンソール アプリケーションの作成
Visual Studio を使用して、新しいコンソール アプリケーションを作成します。
%SystemRoot%\system32\inetsrv にある Microsoft.Web.Administration.dll への参照を追加します。
次に、作成した構成セクションにアクセスするための C# コードを作成します。
汎用クラスを使用した構成セクションへのアクセス
Microsoft.Web.Administration 名前空間の汎用クラスを使用して、上で作成した構成セクションにアクセスします。次のコード スニペットは、このセクションの "booleanProperty" プロパティを取得および設定する方法を示しています。このスニペットは applicationHost.config ファイルにプロパティを設定します。また、コンピューター上のすべてのサイトおよびアプリケーションで継承され、オーバーライドが可能です。
static void Main(string[] args)
{
// Initialize the server manager used to administer the sections
ServerManager serverManager = new ServerManager();
// Get the applicationHost.config file
Configuration config = serverManager.GetApplicationHostConfiguration();
// Get the custom configuration section from that file
ConfigurationSection section = config.GetSection("myCustomSection");
// Output the default value of the booleanProperty
Console.WriteLine(section.GetAttributeValue("booleanProperty"));
// Set the new value of the booleanProperty
section.SetAttributeValue("booleanProperty", true);
// Update the config file with the changes that you have made
serverManager.CommitChanges();
}
次に、変更した内容で、この applicationHost.config ファイルを更新します。
<myCustomSection booleanProperty="true" />
セクション内のすべてのプロパティを読み取り、更新するコード スニペットを以下に示します。
static void Main(string[] args) {
ServerManager serverManager = new ServerManager();
Configuration config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection section = config.GetSection("myCustomSection");
Console.WriteLine(section.GetAttributeValue("booleanProperty"));
Console.WriteLine(section.GetAttributeValue("stringProperty"));
Console.WriteLine(section.GetAttributeValue("integerProperty"));
Console.WriteLine(section.GetAttributeValue("enumProperty"));
section.SetAttributeValue("booleanProperty", true);
section.SetAttributeValue("stringProperty", "customString");
section.SetAttributeValue("integerProperty", 300);
section.SetAttributeValue("enumProperty", "EnumValue2");
serverManager.CommitChanges();
}
厳密に型指定されたクラスを使用した構成セクションへのアクセス
Microsoft.Web.Administration API に用意されている基本クラスを派生して、厳密に型指定されたクラスをカスタム構成セクション用に作成することができます。厳密に型指定されたクラスを使用することの主な利点は、特にセクションに入れ子になった要素が存在する場合に、プロパティの取得と設定が容易であることです。また、Visual Studio でこれらのクラスを使用すると、IntelliSense 機能を利用でき、コンパイル時にプロパティの型をチェックできます。
base[...] を呼び出すことによって、これらの属性値を設定する GetAttributeValue クラスおよび SetAttributeValue クラスがラップされます。
class MyCustomSection : ConfigurationSection {
public MyCustomSection() {
}
public bool BooleanProperty {
get {
return (bool)base["booleanProperty"];
}
set {
base["booleanProperty"] = value;
}
}
public string StringProperty {
get {
return (string)base["stringProperty"];
}
set {
base["stringProperty"] = value;
}
}
public int IntegerProperty {
get {
return (int)base["integerProperty"];
}
set {
base["integerProperty"] = value;
}
}
public EnumValue EnumProperty {
get {
return (EnumValue)base["enumProperty"];
}
set {
base["enumProperty"] = value;
}
}
}
public enum EnumValue {
EnumValue1 = 1,
EnumValue2 = 2
}
厳密に型指定されたクラスを使用してこの構成セクションの値を取得および設定するコードを、以下に示します。
static void Main(string[] args) {
ServerManager serverManager = new ServerManager();
Configuration config = serverManager.GetApplicationHostConfiguration();
MyCustomSection section =
(MyCustomSection)config.GetSection("myCustomSection", typeof(MyCustomSection));
Console.WriteLine(section.BooleanProperty);
Console.WriteLine(section.StringProperty);
Console.WriteLine(section.IntegerProperty);
Console.WriteLine(section.EnumProperty);
section.BooleanProperty = true;
section.StringProperty = "CustomString";
section.IntegerProperty = 300;
section.EnumProperty = EnumValue.EnumValue2;
serverManager.CommitChanges();
}