ฉันจะออกแบบ App.config ให้มีการตั้งค่าจำนวนตัวแปรได้อย่างไร

ใช่แล้ว ฉันมีบริการ Windows ที่จะหยุดทำงานและทำงานบางอย่างบนเซิร์ฟเวอร์ทุกๆ สองสามนาที บริการอ่านข้อมูลจำนวนมาก (โฮสต์ ชื่อผู้ใช้ พอร์ต ฯลฯ) เกี่ยวกับเซิร์ฟเวอร์ที่เชื่อมต่อจาก App.config และทำงานได้ดีมาก

ตอนนี้ฉันมีข้อกำหนดว่าบริการนี้รองรับเซิร์ฟเวอร์ที่แตกต่างกัน ดังนั้นตอนนี้บริการของฉันต้องอ่านจาก App.config และทำสิ่งที่ต้องทำสำหรับ server1..serverN ตามลำดับ รอเวลาที่กำหนดไว้แล้วเริ่มต้นที่เซิร์ฟเวอร์ 1 อีกครั้ง

ฉันไม่รู้ว่าวิธีที่ดีที่สุดหรืออย่างไรในการจัดเก็บการตั้งค่าเซิร์ฟเวอร์ n ชุดใน App.config จากนั้นจึงกำหนดโดยทางโปรแกรมว่ามีชุดการตั้งค่าจำนวนเท่าใดแล้วอ่านแต่ละชุด

ฉันคิดว่าจะมีการตั้งค่าที่บอกฉันว่ามีเซิร์ฟเวอร์ 5 เครื่องและมีการตั้งค่าสำหรับเซิร์ฟเวอร์1..เซิร์ฟเวอร์5แต่นั่นไม่ได้สวยงามจริงๆ

มีวิธีที่ดีกว่าในการทำเช่นนี้หรือไม่?

ไฟล์ต้นฉบับเต็มของฉันอยู่ด้านล่าง:

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; 
        } 
    }
}

person Chris A    schedule 21.11.2012    source แหล่งที่มา
comment
ฉันว่า app.config ไม่เหมาะกับงานนี้ ทำไมไม่คงการตั้งค่าของคุณในเอกสาร xml ที่สามารถซีเรียลไลซ์ได้หรือในฐานข้อมูล   -  person Grant H.    schedule 21.11.2012
comment
@GrantH. DB น่าจะใช้งานมากเกินไปสำหรับแอปพลิเคชันบริการเล็กๆ นี้ และเนื่องจากฉันไม่มี UI ฉันจึงหวังว่าจะได้ไฟล์ที่ผู้ใช้ทางเทคนิคสามารถแก้ไขได้ซึ่งสามารถแก้ไขได้ ขอบใจนะ ฉันอาจจะไม่มีทางเลือกแล้วก็ได้   -  person Chris A    schedule 21.11.2012


คำตอบ (2)


คุณสามารถใช้ส่วนที่กำหนดเองในไฟล์ app.config และใช้ xml ใดก็ได้ที่คุณต้องการ

person wimh    schedule 21.11.2012
comment
ใช่ ฉันหวังว่าฉันจะเข้าใจได้ว่าทำไมฉันถึงได้รับ The type or namespace name 'ConfigurationSection' can not be found (คุณขาดคำสั่งการใช้หรือการอ้างอิงแอสเซมบลีหรือไม่) ตลอดเวลา ฉันไม่รู้ว่าทำไม แต่ฉันไม่สามารถทำให้มันทำงานได้ไม่ว่าฉันจะทำอะไรก็ตาม... - person Chris A; 21.11.2012
comment
@ChrisA คุณเพิ่มการอ้างอิงถึง System.Configuration หรือไม่? - person wimh; 22.11.2012
comment
ใช่. ฉันจะแก้ไขคำถามเดิมด้วย. cs แบบเต็มตอนนี้ - person Chris A; 22.11.2012
comment
นายคือตำนาน!! ขอบคุณมาก! แสดงให้เห็นว่าเกิดอะไรขึ้นเมื่อคุณพยายามเรียนรู้ภาษาและสภาพแวดล้อมใหม่ด้วยตนเอง ฉันจะไม่มีวันทำอย่างนั้น! - person Chris A; 22.11.2012

ฉันต้องการทำให้การตั้งค่าบางอย่างพร้อมใช้งานสำหรับคลาสแบบจำลองของแอสเซมบลีของไลบรารี และเมื่อค้นหาวิธีแก้ปัญหา ฉันพบคลาสที่เรียกว่า "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;
        }
    }
}
person Arthur Nunes    schedule 21.11.2012