3

C#.NETコンソールアプリケーションのapp.configファイルにカスタム構成セクションを作成しようとしています。次のように、一部のサーバーに関する詳細を保存します。

<configSections>
  <sectionGroup name="serverGroup">
    <section name="server" type="RPInstaller.ServerConfig" allowLocation="true" allowDefinition="Everywhere"/>
  </sectionGroup>
</configSections>
<serverGroup>
  <server>
    <name>rmso2srvm</name>
    <isBatchServer>false</isBatchServer>
  </server>
  <server>
    <name>rmsb2srvm</name>
    <isBatchServer>true</isBatchServer>
  </server>
</serverGroup>

私は次のようにサーバーセクションに定義されたクラスを持っています:

namespace RPInstaller
{
    public class ServerConfig : ConfigurationSection
    {
        [ConfigurationProperty("name", IsRequired=true)]
        public string Name {...}

        [ConfigurationProperty("isBatchServer", IsRequired = true)]
        public bool IsBatchServer {...}
    }
}

サーバーセクションを読み込もうとすると、「セクションは構成ファイルごとに1回だけ表示される必要があります」という例外が発生します。

app.configファイル内で複数のサーバーセクションを合法的に定義するにはどうすればよいですか?

4

2 に答える 2

2
<confgisections>
    <section name="server" type="RPInstaller.ServerConfig" allowLocation="true" allowDefinition="Everywhere"/>
</confgisections>
<server>
  <servers>
    </clear>
    <add name="rmso2srvm" isBatchServer="false"/>
    <add name="rmsb2srvm" isBatchServer="true"/>
  </servers>
</server>

以前にカスタムセクションを設定した方法です

アクセスするVBコード:

 Dim cfg As ServerSection = (ConfigurationManager.GetSection("Server"),ServerSection)
 cfg.ServersCollection("nameOfServer").isBatchServer
于 2011-05-24T08:06:11.160 に答える
0

1つのweb.configに複数のサーバーセクションを作成することはできません。カスタムセクションの複数の要素のみ。web.configを確認してください-エラーの原因はコードが原因ではないようです。


更新:「server」要素の要素を定義していません-ConfigurationSectionのみ。したがって、ランタイムはrmso2srvmfalseのようなセクションを待機します

ServerElement : ConfigurationElementクラスを追加し、セクションクラスの定義に追加する必要があります。

namespace RPInstaller
{
  public class ServerConfig : ConfigurationSection 
  {
    public class ServerElement : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired=true)]
        public string Name {...}
        [ConfigurationProperty("isBatchServer", IsRequired = true)]
        public bool IsBatchServer {...}  
    }
  }
}

詳細はこちら: http: //msdn.microsoft.com/en-us/library/2tw134k3.aspx

于 2011-05-24T08:16:11.400 に答える