Answer one question

Source: Internet
Author: User

Ask "about the use of the configuration architecture under. net2.0"
Fandango

Hello, I am very benefited from reading the article "using the configuration architecture under. net2.0" in your blog. I encountered a problem when I wrote an application configuration module in the past two days. I 'd like to ask you :)
You have. the usage of the configuration architecture under net2.0 "describes the configurationsection, configurationelementcollection, and configurationelement classes. I applied them to my read app. in the config configuration file code, the effect is good. my app. the content and source code of the config file are as follows:
[App. config]:
------------------------------------------------------
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Configsections>
<Section name = "systems" type = "testapp. configuration. systemssection, testapp"/>
</Configsections>
<Systems>
<System name = "production" Server = "prodserver" database = "Prod"/>
<System name = "Demo" Server = "demoserver" database = "Demo"/>
<System name = "testing" Server = "testserver" database = "test"/>
<System name = "development" Server = "devserver" database = "Dev"/>
</Systems>
</Configuration>
[Source program]:
------------------------------------------------------
[Systemssection. CS]:
Using system;
Using system. Collections. Generic;
Using system. configuration;
Using system. text;
Namespace testapp. Configuration
{
Public sealed class systemssection: configurationsection
{
Public systemssection ()
{
}
[Configurationproperty ("", isdefaultcollection = true)]
Public systemscollection Systems
{
Get
{
Return (systemscollection) base [""];
}
}
}
}
[Systemscollection. CS]:
Using system;
Using system. Collections. Generic;
Using system. configuration;
Using system. text;
Namespace testapp. Configuration
{
Public sealed class systemscollection: configurationelementcollection
{
Protected override configurationelement createnewelement ()
{
Return new systemelement ();
}
Protected override object getelementkey (configurationelement element)
{
Return (systemelement) element). Name;
}
Public override configurationelementcollectiontype collectiontype
{
Get
{
Return configurationelementcollectiontype. basicmap;
}
}
Protected override string elementname
{
Get
{
Return "system ";
}
}
}
}
[Systemelement. CS]:
Using system;
Using system. Collections. Generic;
Using system. configuration;
Using system. text;
Namespace testapp. Configuration
{
Public sealed class systemelement: configurationelement
{
[Configurationproperty ("name", iskey = true, isrequired = true)]
Public string name
{
Get
{
Return (string) base ["name"];
}
Set
{
Base ["name"] = value;
}
}
[Configurationproperty ("server", isrequired = true)]
Public String Server
{
Get
{
Return (string) base ["server"];
}
Set
{
Base ["server"] = value;
}
}
[Configurationproperty ("Database", isrequired = true)]
Public String Database
{
Get
{
Return (string) base ["Database"];
}
Set
{
Base ["Database"] = value;
}
}
Public override string tostring ()
{
String output = "systemelement: \ n ";
Output + = string. Format ("name = {0} \ n", name );
Output + = string. Format ("Server = {0} \ n", server );
Output + = string. Format ("database = {0} \ n", database );
Return output;
}
}
}
------------------------------------------------------
[Demo]:
Public void readappconfig ()
{
Configurationsection syssection = configurationmanager. getsection ("systems") as configurationsection;
Systemssection Ss = (systemssection) syssection;
Systemscollection onecollection = ss. systems;
Foreach (systemelement oneelement in onecollection)
{
Console. writeline (oneelement. tostring ());
}
}
------------------------------------------------------
[Output]:
Systemelement:
Name = Production
Server = prodserver
Database = Prod
Systemelement:
Name = demo
Server = demoserver
Database = demo
Systemelement:
Name = testing
Server = testserver
Database = test
Systemelement:
Name = Development
Server = devserver
Database = Dev
------------------------------------------------------
The program has been successfully written here, but I have other requirements to implement. I want to put the above app. the content in the config file is saved in the custom configuration file for reading when the program is running, for example, D: \ testapp \ My. config file. based on the routine provided in your article, I wrote another DEMO code:
------------------------------------------------------
[Demo]:
Public void readmyconfig ()
{
Execonfigurationfilemap filemap = new execonfigurationfilemap ();
Filemap. execonfigfilename = @ "D: \ testapp \ My. config ";
Configuration CFG = configurationmanager. openmappedexeconfiguration (filemap, configurationuserlevel. None );
Systemssection Ss = cfg. getsection ("systems") as systemssection
Systemscollection onecollection = ss. systems;
Foreach (systemelement oneelement in onecollection)
{
Console. writeline (oneelement. tostring ());
}
}
However, the return value of the "cfg. getsection (" systems ") as systemssection" statement is null. I really don't know how to solve this problem. Thank you for your advice.

Sorry, I have been busy for a while. I didn't see this message, but now I have no idea whether you can see it.

You said that the returned section through getsection ("") is null. I think you should set a breakpoint here to check which sessions exist in the CFG configuration you read. I think it is easy to find out the problem.

Two problems that are easy to occur: sessions does not have the system session. Check the file or the configuration type is incorrect. It should be easy to identify the problem.

I use

Cfg. sessions ["systems"] As systemssection.

Hope to help you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.