2011-07-29 5 views
1

.NET 4.0アプリケーションのカスタム構成セクションから値を読み込もうとしています。しかし、値を調べると、期待通りのデフォルト値が表示され、カスタム値は表示されません。カスタム設定セクションからの読み取りが.NETで動作しない

はここに私のクラスです:

using System; 
using System.Configuration; 

namespace TestNamespace 
{ 
    public class TestSection : ConfigurationSection 
    { 
     [ConfigurationProperty("applicationAccessID", IsRequired = true)] 
     public string ApplicationAccessID { get; set; } 

     [ConfigurationProperty("username", IsRequired = true)] 
     public Guid Username { get; set; } 

     [ConfigurationProperty("password", IsRequired = true)] 
     public Guid Password { get; set; } 
    } 
} 

私は、configファイルに以下の項目を追加しました:

<configuration> 
    <configSections> 
    <sectionGroup name="myCustomConfiguration"> 
     <section name="testSection" type="TestNamespace.TestSection,MyAssembly"/> 
    </sectionGroup> 
    </configSections> 
    <myCustomConfiguration> 
    <myAuthenticationSection applicationAccessID="1" username="9A36EC78-76B0-477B-9E36-613C13AE86BB" password="589A8696-2B9B-4ADD-906E-7245D387B594" /> 
    </myCustomConfiguration> 
... 

この行はTestSectionのインスタンスを返しますが、それはからのカスタム値を読んでいません設定:

ConfigurationManager.GetSection("myCustomConfiguration/myAuthenticationSection") 

アドバイスはありますか?

EDIT:ここ グループを除去した後、私の更新設定ファイルである:

<configuration> 
    <configSections> 
    <section name="testSection" type="TestNamespace.TestSection,MyAssembly"/> 
    </configSections> 
    <myAuthenticationSection applicationAccessID="1" username="9A36EC78-76B0-477B-9E36-613C13AE86BB" password="589A8696-2B9B-4ADD-906E-7245D387B594" /> 
... 

同じ問題...

答えて

2

あなたがグループを使用しているので、あなたもグループハンドラを実装する必要があります。

それに対して、より多くあり
public class MySettingsConfigurationGroup : ConfigurationSectionGroup 
{ 
} 

、ここで見てみましょう:http://www.codeproject.com/KB/dotnet/mysteriesofconfiguration.aspx

更新:

public class TestSection : ConfigurationSection 
{ 
    private static TestSection _testSection 
     = ConfigurationManager.GetSection("TestSection") as TestSection; 

    public static TestSection Settings 
    { 
     get 
     { 
     return _testSection; 
     } 
    } 

    [ConfigurationProperty("applicationAccessID", IsRequired = true)] 
    public string ApplicationAccessID { get; set; } 

    [ConfigurationProperty("username", IsRequired = true)] 
    public Guid Username { get; set; } 

    [ConfigurationProperty("password", IsRequired = true)] 
    public Guid Password { get; set; } 

} 

string id = TestSection.Settings.applicationAccessID; 

また、名前が一致している必要があります

<testSection applicationAccessID="1" username="9A36EC78-76B0-477B-9E36-613C13AE86BB" password="589A8696-2B9B-4ADD-906E-7245D387B594" /> 
+0

私はグループを取り出し、同じ問題を取得しています。 OPの追加コードを参照してください。 –

+0

私の更新された答えを見てください。 – Mrchief

+0

あなたのコードを使用するときに同じ問題が発生しています。奇妙なことは、意図的に何かを壊したとき(不正なセクション名などを使って)、ConfigurationManager.GetSection()の結果を調べるときにnullが返されるということです。今私はデフォルト値でTestSectionのインスタンスを取得しています。 –

2

私はこの質問にはまだ有効であるかどうかを知り、とにかくありません。

ConfigurationElement Sを扱う場合は、構成要素は、値の集合であるので、あなたは、値を読んでいないしていることを問題に対処する必要があります。

私はこのような何かのために置き換えたい:

[ConfigurationProperty("applicationAccessID", IsRequired = true)] 
public string ApplicationAccessID 
{ 
    get { return (string)this["applicationAccessID"]; } 
    set { this["applicationAccessID"] = value; } 
} 

[ConfigurationProperty("username", IsRequired = true)] 
public Guid Username 
{ 
    get { return (Guid)this["username"]; } 
    set { this["username"] = value; } 
} 

[ConfigurationProperty("password", IsRequired = true)] 
public Guid Password 
{ 
    get { return (Guid)this["password"]; } 
    set { this["password"] = value; } 
} 
関連する問題