2013-10-15 38 views
1

我在這裏遇到了一個嚴重的問題,我試圖爲我的網絡應用程序配置一個新的部分,並嘗試了10萬種不同的東西后,我認爲如果我發佈自己的帖子會更好。如何使工作ConfigSection?

這裏是我的代碼:

<configSections>  
    <section name="EmailConfigurationSection" 
      type="ServiceEmail.Helper.EmailConfigurationSection, ServiceEmail.Helper, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/> 
</configSections> 

<EmailConfigurationSection isDebug="true" 
    smtpAddress="***" 
    smtpLogin="***" 
    smtpPassword="***" 
    smtpPort="***" 
    emailPersonForTest="***@***.lu"/>  
public class EmailConfigurationSection : ConfigurationSection 
{ 
    private static readonly EmailConfigurationSection settings = ConfigurationManager.GetSection("EmailConfigurationSection") as EmailConfigurationSection; 

    public static EmailConfigurationSection EmailConfiguration 
    { 
     get { return settings; } 
    } 

    [ConfigurationProperty("isDebug", DefaultValue = "false", IsRequired = false)] 
    public Boolean IsDebug 
    { 
     get { return (Boolean) this["isDebug"]; } 
     set { this["isDebug"] = value; } 
    } 
} 

......我在這裏我的web.config中定義的所有財產

每次我試圖訪問一個屬性,我得到了一個NullReferenceException電子郵件配置

我已經設置了我的web.conf它被複制到輸出目錄中。

使用NameCollectionValue後,我得到的是:

Une exception a été levée par l'initialiseur de type pour 'ServiceEmail.Helper.EmailConfigurationSection'. --System.Configuration.ConfigurationErrorsException: Une erreur s'est produite lors de la création du gestionnaire de section de configuration pour EmailConfigurationSection : Impossible de charger le fichier ou l'assembly 'ServiceEmail.Helper, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' ou une de ses dépendances. Le fichier spécifié est introuvable. (C:\inetpub\wwwroot\Service_Mails\web.config line 8) ---> System.IO.FileNotFoundException: Impossible de charger le fichier ou l'assembly 'ServiceEmail.Helper, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' ou une de ses dépendances. Le fichier spécifié est introuvable. 
    à System.Configuration.TypeUtil.GetTypeWithReflectionPermission(IInternalConfigHost host, String typeString, Boolean throwOnError) 
    à System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.Init(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord) 
    à System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.InitWithRestrictedPermissions(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord) 
    à System.Configuration.RuntimeConfigurationRecord.CreateSectionFactory(FactoryRecord factoryRecord) 
    à System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey, Boolean& isRootDeclaredHere) 
    --- Fin de la trace de la pile d'exception interne --- 
    à System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey, Boolean& isRootDeclaredHere) 
    à System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject) 
    à System.Configuration.BaseConfigurationRecord.GetSection(String configKey) 
    à System.Configuration.ConfigurationManager.GetSection(String sectionName) 
    à ServiceEmail.Helper.EmailConfigurationSection..cctor() dans C:\Users\***\documents\visual studio 2010\Projects\ServiceEmail\ServiceEmail\Helper\EmailConfigurationSection.cs:ligne 14 --ServiceEmail 

回答

1

更新:

從您的編輯,看來你有你的類型屬性的錯誤。確保dll的名字真的是ServiceEmail.Helper.dll。它可能只是ServiceEmail.dll


您可能試圖過早地初始化設置。

也許你可以嘗試更換

private static readonly EmailConfigurationSection settings = ConfigurationManager.GetSection("EmailConfigurationSection") as EmailConfigurationSection; 

public static EmailConfigurationSection EmailConfiguration 
{ 
    get 
    { 
     return settings; 
    } 
} 

有:

public static EmailConfigurationSection EmailConfiguration 
{ 
    get 
    { 
     return ConfigurationManager.GetSection("EmailConfigurationSection") as EmailConfigurationSection; 
    } 
} 
+0

不工作,謝謝你的幫助... – Heaven42

+1

@Greenarrow從你的編輯,似乎你有你的類型屬性錯誤。確定該dll名稱是ServiceEmail.Helper.dll,不只是ServiceEmail.dll? – jbl

+0

現在我在我的自定義部分和之前嘗試過的nameValue集合之間產生了一個無效的投射,我清理了我的代碼並告訴您它是否正常;) – Heaven42

0

請您與以下試試?

EmailConfigurationSectionloc = 
    (EmailConfigurationSection)ConfigurationSettings.GetConfig("EmailConfigurationSection"); 
+0

謝謝你的幫助,但不工作...同樣的錯誤 – Heaven42

+0

能請這樣的內置的NameValueCollection檢查。 NameValueCollection objEmail =(NameValueCollection)ConfigurationSettings.GetConfig(「EmailConfigurationSection」); – mit

+0

我剛剛做了,我做了編輯,你可以看到我得到的 – Heaven42