1

对,所以我有一个 Windows 服务,它每隔几分钟就会关闭并在服务器上做一些工作。该服务从 App.config 读取有关它连接到的服务器的大量信息(主机、用户名、端口等),并且效果很好。

我现在有一个要求,该服务可以满足 n 个不同的服务器。所以现在我的服务需要从 App.config 中读取,并按顺序为 server1..serverN 做它需要做的事情。等待预定的时间,然后再次从 server1 开始。

我不知道如何或什么是在 App.config 中存储 n 组服务器设置的最佳方式,然后以编程方式确定有多少组设置,然后读取每组设置。

我想过有一个设置告诉我有 5 台服务器,然后设置 server1..server5 的设置,但这真的不优雅。

有一个更好的方法吗?

我的完整源文件如下:

using System;
using System.Collections;
using System.Text;
using System.Configuration;
using System.Xml;

namespace FTPConfig
{
    public class MyAppConfig : ConfigurationSection
    {
        public static MyAppConfig GetConfiguration()
        {
            MyAppConfig configuration = ConfigurationManager.GetSection("MainSettings") as MyAppConfig;

            if (configuration != null) return configuration;

            return new MyAppConfig();
        }
    }

    [ConfigurationProperty("host", IsRequired = true)]
    public String Host 
    { 
        get 
        { 
            return this["host"] as string; 
        } 
    }
}
4

2 回答 2

2

您可以在 app.config 文件中使用自定义部分,并在那里使用您喜欢的任何 xml。

于 2012-11-21T18:12:42.133 回答
0

我想为库程序集的模型类提供一些设置,在搜索解决方案时,我发现了一个名为“XmlConfigurator”的类,此处描述了该类。在简历中,您可以在 App.config 中创建一个部分并将其映射到您想要的任何类型。例如。假设您有以下课程:

public class MySettings
{
    public int Setting1 { get; set; }

    public string Setting2 { get; set; }
}

在您的 app.config 中,您只需添加:

<configuration>
<configSections>
    <section name="MySettings" type="MyApp.XmlConfigurator, MyApp" />
</configSections>

<MySettings type="MyApp.MySettings, MyApp">
    <Setting1>10</Setting1>
    <Setting2>MyValue</Setting2>
</MySettings>
</configuration>

初始化应用程序时,您可以轻松加载它:

var settings = (MyApp.MySettings)ConfigurationManager.GetSection("MySettings");

XmlConfigurator:

public sealed class XmlConfigurator : IConfigurationSectionHandler
{
    public XmlConfigurator()
    {
    }

    //<?xml version="1.0" encoding="utf-8" ?>
    //<configuration>
    //    <configSections>
    //        <section name="DispatchSettings" type="MyNamespace.XmlConfigurator, MyNamespace.Core" />
    //    </configSections>

    //    <DispatchSettings type="MyNamespace.DispatchSettings, MyNamespace.Core">
    //        <ServiceIsActive>True</ServiceIsActive>
    //        <DispatchProcessBatchSize>100</DispatchProcessBatchSize>
    //    </DispatchSettings>
    public object Create(object parent, object configContext, XmlNode section)
    {
        XPathNavigator navigator = null;
        String typeName = null;
        Type sectionType = null;
        XmlSerializer xs = null;
        XmlNodeReader reader = null;

        try
        {
            Object settings = null;

            if (section == null)
                return settings;

            navigator = section.CreateNavigator();
            typeName = (string)navigator.Evaluate("string(@type)");
            sectionType = Type.GetType(typeName);

            if (sectionType == null)
                throw new ArgumentException("Could not find the specified type: " + typeName);

            xs = new XmlSerializer(sectionType);
            reader = new XmlNodeReader(section);

            settings = xs.Deserialize(reader);

            return settings;
        }
        finally
        {
            xs = null;
        }
    }
}
于 2012-11-21T18:32:51.697 回答