DOTNET 2.0 Configuration System

Source: Internet
Author: User
Tags dotnet

Today, I used vs2005 to develop a Windows service, and found that the Configuration System of dotnet2.0 has changed greatly compared with the function of 1.x. Record and share with you.
. NET Framework 1.1 provides a basic application configuration mechanism that allows you to define read-only application settings in the application configuration file. You can access these settings through the system. configuration. deleettings class, which uses keys to retrieve the settings and returns a weak type object that must be converted to the appropriate type. This mechanism is very useful in simple cases, which focus on relatively static data that can only be modified by the Administrator. However, many other application configurations require a more powerful and flexible solution .. Net Framework 2.0 and Visual Studio 2005 provide a new configuration system for Windows Forms applications. The system solves the limitations of previous solutions and introduces many new concepts, allowing you to experience a more complete and effective design experience and a more personalized end user experience.

Configuration settings the Configuration System provides more powerful support for the definition, reading, and writing of configuration settings. Each setting is defined by its name, data type, scope, default value, and accessibility. All settings are strongly typed, And you can specify any serializable type. The configuration system supports many persistent value setting mechanisms, including string representation, XML serialization, and binary serialization. You can also define the setting as one of the following two scopes: user or application. The application settings are read-only and the value is shared by all users of the application on the computer. By default, these values are stored in the application configuration file. The following is an example of configuration settings:

<? XML version = "1.0" encoding = "UTF-8"?>

<Configuration>

<Configsections>

<Sectiongroup name = "applicationsettings" type = "system. configuration. applicationsettingsgroup, system, version = 2.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089">

<Section name = "bigteam. windows service. properties. settings "type =" system. configuration. clientsettingssection, system, version = 2.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089 "requirepermission =" false "/>

</Sectiongroup>

</Configsections>

<Applicationsettings>

<Bigteam. windowsservice. properties. Settings>

<Setting name = "bigteam_windowsservice_joinjobservice_securityservice"

Serializeas = "string">

<Value> http: // localhost/bigteam. Security/securityservice. asmx </value>

</Setting>

<Setting name = "intervalminutes" serializeas = "string">

<Value> 5 </value>

</Setting>

</Bigteam. windowsservice. properties. Settings>

</Applicationsettings>

</Configuration>

The packaging configuration system requires you to use the packaging class to access the settings stored in the configuration storage. These packaging classes provide a strong interface to the configuration system and provide a complete description of the metadata of each setting. This allows the user of the packaging class to understand the configuration method of each setting without directly accessing the configuration file.

Note that you should understand that the configuration system uses a pluggable architecture to connect and set up storage. Initially, the system only supports the storage settings in the configuration file, but at last it supports many potential storage settings.

The following is the packaging class shown previously:

Namespace bigteam. windowsservice. properties {

[Global: system. runtime. compilerservices. compilergeneratedattribute ()]

[Global: system. codedom. compiler. generatedcodeattribute ("Microsoft. visualstudio. Editors. settingsdesigner. settingssinglefilegenerator", "8.0.0.0")]

Internal sealed partial class settings: Global: system. configuration. applicationsettingsbase {

Private Static settings defaultinstance = (settings) (Global: system. configuration. applicationsettingsbase. synchronized (new settings ())));

Public static settings default {

Get {

Return defaultinstance;

}

}

[Global: system. configuration. applicationscopedsettingattribute ()]

[Global: system. Diagnostics. debuggernonusercodeattribute ()]

[Global: system. configuration. specialsettingattribute (Global: system. configuration. specialsetting. webserviceurl)]

[Global: system. configuration. defaultsettingvalueattribute ("http: // localhost/bigteam. Security/securityservice. asmx")]

Public String bigteam_windowsservice_joinjobservice_securityservice {

Get {

Return (string) (This ["bigteam_windowsservice_joinjobservice_securityservice"]);

}

}

[Global: system. configuration. applicationscopedsettingattribute ()]

[Global: system. Diagnostics. debuggernonusercodeattribute ()]

[Global: system. configuration. defaultsettingvalueattribute ("5")]

Public String intervalminutes {

Get {

Return (string) (This ["intervalminutes"]);

}

}

}

}

Use the packaging class in the application code to access and modify configuration settings. For example, the following code is used to retrieve the URL of the WebService stored in the user settings named bigteam_windowsservice_joinjobservice_securityservice. Bigteam. windowsservice. properties. settings setting = new bigteam. windowsservice. properties. settings ();
String urlsettings = Setting. bigteam_windowsservice_joinjobservice_securityservice; // you can obtain it from the configuration file.

Although the IDE supports manual creation of configuration files and packaging classes, Visual Studio 2005 provides full ide support for both tasks. In Visual C #. net, you can right-click a project in Solution Explorer and select Properties to access the setup designer. On the property page, select Settings ". You will see a designer that allows you to define all aspects of the application and user settings. When you save the changes in the designer, Visual C # automatically creates and fills in the corresponding configuration file. It will also create a packaging class and use settings to make the packaging class accessible as settings.

 

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.