1

そうです、私にはWindowsサービスがあり、数分ごとにサーバー上でいくつかの作業を実行します。このサービスは、接続先のサーバーに関する多くの情報(ホスト、ユーザー名、ポートなど)をApp.configから読み取り、非常にうまく機能します。

現在、サービスがn個の異なるサーバーに対応しているという要件があります。したがって、私のサービスはApp.configから読み取り、server1..serverNに対して必要な処理を順番に実行する必要があります。所定の時間待ってから、server1から再開します。

nセットのサーバー設定をApp.configに保存し、プログラムで設定のセットがいくつあるかを判断して、各セットを読み取る方法や方法がわかりません。

サーバーが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 に答える