Duwamish in-depth analysis - configuration articles

xiaoxiao2021-03-06  82

Summary:

This article details the structural processing method and use of the Web.config configuration file of Duwamish online e-bookstore, which expounds the role of each functional module of the configuration file.

table of Contents:

introduction

Configuration section handler declaration

Custom configuration section

Configuration section handler

to sum up

Reference

Author

introduction:

Almost in the book for the ASP.NET programming, when talking about how to manage the database connection string, it is used in the web.config file in the following form when talking about how to manage the database connection string.

PPSettings>

DD key = "Connectionstring" value = "data source = localhost; initial catalog = database; user ID =; password =" />

Then use the following ways in the program:

System.configuration.configurationSettings.AppSettings ["Connectionstring"]

The benefits of this are very obvious: When the database has change, it is only necessary to change the connection string in web.config without recompilating the entire application, which gives the application's deployment and transplantation. Very convenient.

If you think that Web.config's role is limited to this, then you are wrong, web.config configuration features are very powerful, it can support the ASP.NET configuration settings set using their own XML configuration tag extension standard, in duwamish To a certain extent, it reflects its function. Here I will analyze DUWAMISH's web.config files, so that you can learn about developing a typical .NET web application.

Configuration section handler declaration

In the duwamish solution, the web.config file is placed under the Web project, because web.config requires IIS and ASP.NET Runtime management and support, so it should be placed in a virtual directory, let's take a look at it first. first part:

Here, three configuration segment processing program declarations are defined, and they must appear between the and tags on the top of the configuration file, where they only use the Name and Type properties, where Name The attribute defines the name of the specified configuration section, and the Type property specifies the name of the configuration segment processing program class specified from the configuration file, there are two parts, the front is the class name of the handler, and is an Assembly name ( Assembly must be in the bin directory) and the version number, aucture and other information. What do they mean? For example, the first section, Asp.Net meaning is to tell the system when using System.Configuration.ConfigurationSettings.GetConfig ( "ApplicationConfiguration") in the program this static method to read ApplicationConfiguration configuration section when calls Duwamish7.SystemFramework.ApplicationConfiguration This class is processed for this configuration section. Regarding the configuration section processing class, we will discuss in detail later, let us continue to see the web.config file down.

Custom configuration section

After the node, we can see the following XML elements (there are a lot of articles about the SYSTEM.WEB node, this is no longer repeated):

PPLICATIONCONFIGURATION>

DD key = "systemframework.tracing.enabled" Value = "false" />

To the root application directory. ->

DD key = "systemframework.tracing.tracefile" value = "duwamishtrace.txt" />

DD key = "systemframework.tracing.tracelen" value = "4" />

Environment Variables or the registry ->

DD key = "systemframework.tracing.switchname" value = "duwamishtraceswitch" />

DD key = "systemframework.tracing.switchDescription" value = "error and information tracing for duwamish" />

Note: The Default Duwamish7 Event Source Name Is Created In The Local Machine During Setup. If you wish to log events to a different est.

->

DD key = "systemframework.eventlog.enabled" Value = "true" />

DD key = "systemframework.eventlog.machine" value = "." />

DD key = "systemframework.eventlog.sourcename" value = "duwamish7" />

0 = OFF

1 = Error

2 = WARNING

3 = Info

4 = Verbose ->

DD key = "systemframework.eventlog.loglevel" value = "1" />

DD key = "duwamish.dataaccess.connectionstring" value = "server = luyan / netsdk; user id = duwamish7_login; password = password; dataword = duwamish7; connection reset = false" />

DD key = "duwamish.web.enablepagecache" value = "true" />

DD key = "duwamish.web.pagecacheexpiresinseconds" Value = "3600" />

DD key = "duwamish.web.enablessl" value = "false" />

DD Key = "." value = "" />

DD key = "modules" value = "" />

DD key = "../ Common / data" value = "" />

DD key = "../ systemframework" value = "" />

DD key = "../ Business / face" value = "" />

DD key = "../ business / rules" value = "" /> dd key = "../ DataAccess" value = "" />

DD key = "Secure" value = "" />

DD key = "DOCS / Common" Value = "" />

DD key = "DOCS / DATAACCESS" Value = "" />

DD key = "DOCS / FACADE" Value = "" />

DD key = "DOCS / RULES" Value = "" />

DD key = "DOCS / Web" Value = "" "/>

The configuration section information is divided into two main areas: Configure the section handler declaration area and configuration section setting area, here is the three section configuration segments that have just defined, which contains actual configuration settings, and the purpose description, please see the comment, All configuration information must reside between the and root XML tags, the configuration section setting area is after the area.

Configuration section handler

The following has been introduced, and the section defines the processing configuration section: duwamish7.systemframework.ApplicationConfiguration and duwamish7.common.duwamiSHConfiguration, they are located in the SystemFramework and CommON projects, .NET regulations, all classes that can handle the configuration festival must be implemented IConfigurationSectionHandler interface, IConfigurationSectionHandler the interface is very simple, there is only one object Create (object parent, object configContext, XmlNode section) method, which does not need to take the initiative to call, it is automatically invoked when ConfigurationSettings.GetConfig static method, that is When you use configurationSettings.getConfig to get the configuration section in the program, .NET automatically instantiates the configuration section processing class according to the class name and path defined in the changing section declaration, and calls the CREATE method. Below is the process class call scheme of duwamish:

1. Call the ApplicationConfiguration.onApplicationStart static method in Global.asax's Application_onstart method and obtain an absolute path of the application root.

Void Application_onstart ()

{

ApplicationConfiguration.onApplicationStart (Context.Server.MAppath);

String configPath = path.combine (context.server.mappath (context.Request.ApplicationPath),

"remoteClient.cfg");

IF (file.exists (configPath)) RemotingConfiguration.configure (configPath);

}

2, ApplicationConfiguration.onApplicationStart static method calls system.configuration.configurationSettings.getConfig method processing configuration section:

Public Static void onapplicationStart (String myappppath)

{

approot = myapppp path;

System.configuration.configurationSettings.getConfig ("ApplicationConfiguration");

System.configuration.configurationSettings.getConfig ("DuwamiShconfiguration");

System.configuration.configurationSettings.getConfig ("SourceViewer");

}

Everyone has noticed that duwamish did not get the value returned by getConfig, because the getConfig method triggered the CREATE method of the configuration segment processing program, so only need to take the configuration value in the CREATE method.

3, configuration reading example: duwamish7.common.duwamishConfiguration class

Public Object Create (Object Parent, Object ConfigContext, XMLNode Section)

{

NameValuecollection settings;

Try

{

NameValuesectionHandler BaseHandler = new namevaluesectionhandler ();

Settings = (NameValueCollection) BaseHandler.create (PARENT, ConfigContext, Section);

}

Catch

{

Settings = NULL;

}

IF (settings == null)

{

Dbconnectionstring = dataaccess_connectionstring_default;

PageCacheexpiresinseconds = Web_pagecaccheexpiresinseconds_default;

EnablepageCache = web_enablepagecache_default;

Enablessl = Web_enablessl_default;

}

Else

{

Dbconnectionstring = ApplicationConfiguration.readsetting (Settings,

DataAccess_connectionstring, dataaccess_connectionstring_default);

PageCacheexPiresInseconds =

ApplicationConfiguration.readsetting (Settings, Web_Pagecacheexpiresinseconds,

Web_pagecacheexpiresinseconds_default);

EnablepageCache = ApplicationConfiguration.readsetting (settings, web_enablepagecache, web_enablepagecache_default);

Enablessl = ApplicationConfiguration.readsetting (Settings, Web_enablessl, Web_enablessl_Default);

}

Return settings;

}

It can be seen here that Duwamish actually does not manually read data from an XMLNode, but directly forwards the data to a NameValueSectionLler to do actual configuration read, and its own work is just to check if there is an actual definition. If not, you give the default value.

to sum up:

At this point, the configuration value in the web.config is read in the static variable of the configuration class, and the static variables of the configuration class can be directly accessed by the static variables of the program, for example, as anywhere in the program, as long as Enter duwamish7.comMon.duwamishConfiguration.ConnectionsTring You can get: server = luyan / netsdk; user ID = duwamish7_login; password = password; database = duwamish7; connection reset = false This string. More ideal, you can extend your own configuration festival and configure the section handler to preprocessing complicated custom configurations.

Reference:

ASP.NET configuration

MS-help: //ms.vscc/ms.msdnvs.2052/cpguide/html/cpconaspnetconfiguration.htm

Profile architecture

MS-help: //ms.netframeworksdk.chs/cpgenref/html/gngrfnetFrameworkConfigurationFileschema.htm

转载请注明原文地址:https://www.9cbs.com/read-106408.html

New Post(0)