自定义配置部分已保存,没有数据

本文关键字:数据 保存 配置部 自定义 | 更新日期: 2023-09-27 18:33:14

我想在应用程序会话之间保留一些数据,并决定使用.NET Configuration API(System.Configuration命名空间(,事实证明这比我预期的要困难得多。

我创建了一个自定义配置部分,如下所示:

用户邮件源部分

public class UserMailSourcesSection : ConfigurationSection
{
    [ConfigurationProperty("User", IsRequired = true)]
    public string User { get; set; }
    [ConfigurationProperty("UserMailSources", IsDefaultCollection = false)]
    [ConfigurationCollection(typeof(MailSourceElemet))]
    public MailSourceElemetCollection MailSources
    {
        get
        {
            return (MailSourceElemetCollection)this["UserMailSources"];
        }
        set
        {
            this["UserMailSources"] = value;
        }
    }
    public UserMailSourcesSection()
    {
        this.User = String.Empty;
    }
}

(其余相关代码在问题末尾(

现在实际的问题是,在配置了我想要保留的所有值后,我从保存本节的代码部分获得了异常(请注意,不是非常描述性的异常(。我将其追溯到使用具有一些空值的参数调用的MailSourceElementCollection.Add。该参数是从微软的代码传递的。我推断它必须使用默认的无参数构造函数创建参数的新实例。我认为解决方案是显式编写一个无参数构造函数,该构造函数将用某种默认值(特别是String.Empty(替换空值,并且似乎有效。代码运行,save 方法没有抛出,但有一个新问题 - 即使我在 Main 方法中显式设置了所有内容,实际上也没有保存任何数据:

PublisherElemet publisher = new PublisherElemet();
publisher.Publisher = "test";
PublisherElementCollection publishers = new PublisherElementCollection();
publishers.Add(publisher);
MailSourceElemet mailSource = new MailSourceElemet();
mailSource.HostName = "test";
mailSource.Port = 0;
mailSource.Username = "test";
mailSource.Password = "test";
mailSource.Publishers = publishers;
MailSourceElemetCollection mailSources = new MailSourceElemetCollection();
mailSources.Add(mailSource);
UserMailSourcesSection userMailSources = new UserMailSourcesSection();
userMailSources.User = Environment.UserName;
userMailSources.MailSources = mailSources;
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.Sections.Add("MailSources", userMailSources);
config.Save(ConfigurationSaveMode.Modified);

在此之后,预期的输出为:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <configSections>
  <section name="MailSources" type="Email_To_Rss_V2.Classes.UserMailSourcesSection, Email-To-Rss-V2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
 </configSections>
 <MailSources User="User">
  <UserMailSources>
    <add HostName="test" Port="0" Username="test" Password="test">
      <Publishers>
        <add Publihser="test" />
      </Publishers>
    </add>
  </UserMailSources>
</MailSources>

而实际输出为:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <configSections>
  <section name="MailSources" type="Email_To_Rss_V2.Classes.UserMailSourcesSection, Email-To-Rss-V2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
 </configSections>
 <MailSources User="">
  <UserMailSources>
    <add HostName="" Port="0" Username="" Password="">
      <Publishers>
        <add Publihser="" />
      </Publishers>
    </add>
  </UserMailSources>
</MailSources>

真正奇怪的是,在调试器中显示数据在那里,它似乎没有输出到文件。

以下是相关代码的其余部分:

MailSourceElementCollection

public class MailSourceElemetCollection : ConfigurationElementCollection
{
    public override ConfigurationElementCollectionType CollectionType
    {
        get
        {
            return ConfigurationElementCollectionType.AddRemoveClearMap;
        }
    }
    public MailSourceElemet this[int index]
    {
        get
        {
            return (MailSourceElemet)BaseGet(index);
        }
        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }
            BaseAdd(0, value);
        }
    }
    protected override object GetElementKey(ConfigurationElement element)
    {
        if (((MailSourceElemet)element).Username == null)
        {
            Console.WriteLine("null");
        }
        return ((MailSourceElemet)element).Username;
    }
    protected override ConfigurationElement CreateNewElement()
    {
        return new MailSourceElemet();
    }
    public void Add(MailSourceElemet mailSource)
    {
        BaseAdd(mailSource);
    }
    public void Clear()
    {
        this.BaseClear();
    }
    public void Remove(MailSourceElemet element)
    {
        BaseRemove(element.Username);
    }
    public void Remove(string name)
    {
        BaseRemove(name);
    }
    public void RemoveAt(int index)
    {
        BaseRemoveAt(index);
    }
}

邮件源元素

public class MailSourceElemet : ConfigurationElement
{
    [ConfigurationProperty("HostName", IsRequired = true)]
    public string HostName { get; set; }
    [ConfigurationProperty("Port", IsRequired = true)]
    public int Port { get; set; }
    [ConfigurationProperty("Username", IsRequired = true)]
    public string Username { get; set; }
    [ConfigurationProperty("Password", IsRequired = true)]
    public string Password { get; set; }
    [ConfigurationProperty("Publishers", IsDefaultCollection = false)]
    [ConfigurationCollection(typeof(PublisherElemet))]
    public PublisherElementCollection Publishers
    {
        get
        {
            return (PublisherElementCollection)this["Publishers"];
        }
        set
        {
            this["Publishers"] = value;
        }
    }
    public MailSourceElemet()
    {
        this.HostName = String.Empty;
        this.Port = 0;
        this.Username = String.Empty;
        this.Password = String.Empty;
    }
}

PublisherElementCollection

public class PublisherElementCollection : ConfigurationElementCollection
{
    public override ConfigurationElementCollectionType CollectionType
    {
        get
        {
            return ConfigurationElementCollectionType.AddRemoveClearMap;
        }
    }
    public PublisherElemet this[int index]
    {
        get
        {
            return (PublisherElemet)BaseGet(index);
        }
        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }
            BaseAdd(0, (PublisherElemet)value);
        }
    }
    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((PublisherElemet)element).Publisher;
    }
    protected override ConfigurationElement CreateNewElement()
    {
        return new PublisherElemet();
    }
    public void Add(PublisherElemet publisher)
    {
        BaseAdd(publisher);
    }
    public void Clear()
    {
        this.BaseClear();
    }
    public void Remove(PublisherElemet publisher)
    {
        BaseRemove(publisher.Publisher);
    }
    public void Remove(string name)
    {
        BaseRemove(name);
    }
    public void RemoveAt(int index)
    {
        BaseRemoveAt(index);
    }
}

发布者元素

public class PublisherElemet : ConfigurationElement
{
    [ConfigurationProperty("Publihser", IsRequired = true)]
    public string Publisher { get; set; }
    public PublisherElemet()
    {
        Publisher = String.Empty;
    }
}

提前感谢!

自定义配置部分已保存,没有数据

看看"配置部分设计器",这是一个很好的"Visual Studio 外接程序,允许您以图形方式设计 .NET 配置节,并为其自动生成所有必需的代码和架构定义 (XSD(。

使用此工具设计您的配置部分,[我想您会喜欢它:)]。