プロパティの潜在的な子要素と属性を含むカスタム web.config セクションを定義するにはどうすればよいですか?
-
08-06-2019 - |
質問
私が開発する Web アプリケーションでは、相互に依存する構成設定が必要になることが多く、各環境間を移動するときに変更する必要がある設定もあります。
現在、すべての設定は単純なキーと値のペアですが、カスタム構成セクションを作成すると、2 つの値を同時に変更する必要がある場合や、環境に合わせて設定を変更する必要がある場合が明確になるため便利です。
カスタム構成セクションを作成する最良の方法は何ですか?値を取得するときに特別な考慮事項はありますか?
解決
属性、子構成セクション、および制約の使用
また、配管を自動的に処理する属性を使用することや、制約を簡単に追加する機能を提供することもできます。
ここでは、私自身がサイトの 1 つで使用しているコードの例を紹介します。制約を設けて、1 人のユーザーが使用できるディスク領域の最大量を指定します。
MailCenterConfiguration.cs:
namespace Ani {
public sealed class MailCenterConfiguration : ConfigurationSection
{
[ConfigurationProperty("userDiskSpace", IsRequired = true)]
[IntegerValidator(MinValue = 0, MaxValue = 1000000)]
public int UserDiskSpace
{
get { return (int)base["userDiskSpace"]; }
set { base["userDiskSpace"] = value; }
}
}
}
これはweb.configで次のように設定されます
<configSections>
<!-- Mailcenter configuration file -->
<section name="mailCenter" type="Ani.MailCenterConfiguration" requirePermission="false"/>
</configSections>
...
<mailCenter userDiskSpace="25000">
<mail
host="my.hostname.com"
port="366" />
</mailCenter>
子要素
子 XML 要素 郵便 上記と同じ .cs ファイルに作成されます。ここではポートに制約を追加しました。ポートにこの範囲外の値が割り当てられている場合、構成がロードされるときにランタイムがエラーを出します。
MailCenterConfiguration.cs:
public sealed class MailCenterConfiguration : ConfigurationSection
{
[ConfigurationProperty("mail", IsRequired=true)]
public MailElement Mail
{
get { return (MailElement)base["mail"]; }
set { base["mail"] = value; }
}
public class MailElement : ConfigurationElement
{
[ConfigurationProperty("host", IsRequired = true)]
public string Host
{
get { return (string)base["host"]; }
set { base["host"] = value; }
}
[ConfigurationProperty("port", IsRequired = true)]
[IntegerValidator(MinValue = 0, MaxValue = 65535)]
public int Port
{
get { return (int)base["port"]; }
set { base["port"] = value; }
}
使用
これをコード内で実際に使用するには、MailCenterConfigurationObject をインスタンス化するだけです。 自動的に web.config から関連するセクションを読み取ります。
MailCenterConfiguration.cs
private static MailCenterConfiguration instance = null;
public static MailCenterConfiguration Instance
{
get
{
if (instance == null)
{
instance = (MailCenterConfiguration)WebConfigurationManager.GetSection("mailCenter");
}
return instance;
}
}
別のファイル.cs
public void SendMail()
{
MailCenterConfiguration conf = MailCenterConfiguration.Instance;
SmtpClient smtpClient = new SmtpClient(conf.Mail.Host, conf.Mail.Port);
}
有効性をチェックする
構成がロードされ、一部のデータが設定したルールに準拠していない場合、ランタイムがエラーを起こすと以前に述べました (例:MailCenterConfiguration.cs 内)。私はサイトが起動したら、できるだけ早くこれらのことを知りたいと思う傾向があります。これを解決する 1 つの方法は、 _Global.asax.cx.Application_Start_ に構成をロードすることです。構成が無効な場合は、例外によってそのことが通知されます。サイトは起動せず、代わりに詳細な例外情報が表示されます。 死のイエロースクリーン.
Global.asax.cs
protected void Application_ Start(object sender, EventArgs e)
{
MailCenterConfiguration.Instance;
}
他のヒント
クイックン・ダーティ:
まず、 構成セクション そして 構成要素 クラス:
public class MyStuffSection : ConfigurationSection
{
ConfigurationProperty _MyStuffElement;
public MyStuffSection()
{
_MyStuffElement = new ConfigurationProperty("MyStuff", typeof(MyStuffElement), null);
this.Properties.Add(_MyStuffElement);
}
public MyStuffElement MyStuff
{
get
{
return this[_MyStuffElement] as MyStuffElement;
}
}
}
public class MyStuffElement : ConfigurationElement
{
ConfigurationProperty _SomeStuff;
public MyStuffElement()
{
_SomeStuff = new ConfigurationProperty("SomeStuff", typeof(string), "<UNDEFINED>");
this.Properties.Add(_SomeStuff);
}
public string SomeStuff
{
get
{
return (String)this[_SomeStuff];
}
}
}
次に、フレームワークに構成クラスを処理する方法を知らせます。 web.config:
<configuration>
<configSections>
<section name="MyStuffSection" type="MyWeb.Configuration.MyStuffSection" />
</configSections>
...
そして実際に以下に独自のセクションを追加します。
<MyStuffSection>
<MyStuff SomeStuff="Hey There!" />
</MyStuffSection>
その後、コード内で次のように使用できます。
MyWeb.Configuration.MyStuffSection configSection = ConfigurationManager.GetSection("MyStuffSection") as MyWeb.Configuration.MyStuffSection;
if (configSection != null && configSection.MyStuff != null)
{
Response.Write(configSection.MyStuff.SomeStuff);
}
優れたものがあります MSDN の例 を使用して ConfigurationCollection
構成項目のリストが含まれる web.config のカスタム セクションには .NET 4.5 が含まれます。
カスタム構成は非常に便利なものであり、多くの場合、アプリケーションでは拡張可能なソリューションが必要になります。
.NET 1.1 については、記事を参照してください。 http://aspnet.4guysfromrolla.com/articles/020707-1.aspx
注記:上記の解決策は .NET 2.0 でも機能します。
.NET 2.0 固有のソリューションについては、記事を参照してください。 http://aspnet.4guysfromrolla.com/articles/032807-1.aspx
これはセクション ハンドラーを使用して実現できます。書き方の基本的な概要は次のとおりです。 http://www.codeproject.com/KB/aspnet/ConfigSections.aspx ただし、これは app.config を参照しており、web.config で使用するために作成するのとほとんど同じです。これにより、基本的に構成ファイル内に独自の XML ツリーを作成し、より高度な構成を実行できるようになります。
私が見つけた最も簡単な方法は、 アプリ設定セクション.
Web.config に以下を追加します。
<appSettings> <add key="MyProp" value="MyVal"/> </appSettings>
コードからアクセス
NameValueCollection appSettings = ConfigurationManager.AppSettings; string myPropVal = appSettings["MyProp"];