Au début, j'ai défini la section personnalisée dans l'application Console et cela fonctionne très bien. Mais maintenant je veux déplacer certaines fonctionnalités vers la bibliothèque. Mais, malheureusement, je ne peux pas. Parce que maintenant ma section personnalisée a été définie comme nulle.Comment accéder à ConfigurationSection personnalisée à partir de la bibliothèque?
La simple application de la console comprennent le code suivant:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
NotificationsConfigurationSection section
= NotificationsConfigurationSection.GetSection();
string emailAddress = section.EmailAddress;
Console.WriteLine(emailAddress);
Console.Read();
}
}
}
Le fichier avec une configuration personnalisée qui est située dans la bibliothèque séparée comprennent le code suivant:
namespace ClassLibrary1
{
public class NotificationsConfigurationSection : ConfigurationSection
{
private const string configPath = "cSharpConsultant/notifications";
public static NotificationsConfigurationSection GetSection()
{
return (NotificationsConfigurationSection)
ConfigurationManager.GetSection(configPath);
}
/*This regular expression only accepts a valid email address. */
[RegexStringValidator(@"[\w._%+-][email protected][\w.-]+\.\w{2,4}")]
/*Here, we register our configuration property with an attribute.
Note that the default value is required if we use
the property validation attributes, which will unfortunately
attempt to validate the initial blank value if a default isn't found*/
[ConfigurationProperty("emailAddress", DefaultValue = "[email protected]")]
public string EmailAddress
{
get { return (string)this["emailAddress"]; }
set { this["emailAddress"] = value; }
}
}
}
En app.config pour la bibliothèque I inclure les nœuds suivants:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="cSharpConsultant">
<section name="notifications"
type="ClassLibrary1.NotificationsConfigurationSection,
ClassLibrary1"/>
</sectionGroup>
</configSections>
<cSharpConsultant>
<notifications emailAddress="[email protected]"/>
</cSharpConsultant>
</configuration>
Quelqu'un peut-il m'aider?
En fait, vous avez raison. Le même app.config doit être dans l'application Console plutôt que dans ClassLibrary. Je vous remercie! – apros