Tutorials to .com

Tutorials to .com » Dotnet » Foundation » Duwamish-depth analysis - Part configuration

Duwamish-depth analysis - Part configuration

Print View , by: iSee ,Total views: 12 ,Word Count: 2239 ,Date: Wed, 22 Apr 2009 Time: 3:20 AM

Duwamish-depth analysis - Part configuration

This paper introduces in detail the Duwamish online bookstore Web.config configuration file, as well as the structural approach uses a configuration file on the role of the functional module.

Configuration section handler declaration
Custom configuration section
Configuration section handler

Introduced in almost every Asp.Net programming this book, in talking about how to manage the database connection string when the database and are based on the following connection string in the form of Web.Config file:
dd key = "ConnectionString" value = "data source = localhost; initial catalog = Database; user id =; password =" />
</ appSettings>
In the procedure and then access the following:
System.Configuration.ConfigurationSettings.AppSettings [ "ConnectionString"]
The benefits of doing so is clear: When there are any changes to the database, the only change in the web.config connection string without having to recompile the entire application to application deployment and transfer very large convenience.
If you think that limited the role of web.config, then you are wrong, web.config configuration very powerful, it can support the use of their own xml tags to expand the standard configuration of the ASP.NET configuration settings set in the Duwamish to some extent a reflection of its function, I will be following a detailed analysis of the web.config file Duwamish so that we may understand that a typical development. Net WEB configuration technology applications.

Configuration section handler declaration
Duwamish solution in, Web.config file is on the WEB project, because the need for IIS and the web.config of the Asp.Net Runtime management and support, so it should be placed on a virtual directory, we first take a look at it Part I:
<section name="ApplicationConfiguration" type="Duwamish7.SystemFramework.ApplicationConfiguration, Duwamish7.SystemFramework" />
<section name="DuwamishConfiguration" type="Duwamish7.Common.DuwamishConfiguration, Duwamish7.Common" />
<section name="SourceViewer" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> </ configSections>
This section defines the procedures to configure the three statements (Section), in accordance with the regulations they must appear in the configuration file at the top of <configSections> and </ configSections> tags, where they only used the name and type attributes, of which name attribute defines the name of the specified configuration section, and the type attribute provides the specified configuration file to read from the section of the configuration section handler class name, there are two parts to deal with the procedure in front of the class name, followed for the Assembly of (Assembly must be in the bin directory), and the version number, public key information.
They specifically said what does that mean? For example, the first section, the meaning is to tell Asp.Net systems, when used in the procedure System.Configuration.ConfigurationSettings.GetConfig ( "ApplicationConfiguration") static method to read the configuration ApplicationConfiguration section, we will call this Duwamish7.SystemFramework.ApplicationConfiguration class section of the configuration for processing. Section on the configuration class, we will discuss in detail later, we continue to look down to web.config file.

Custom configuration section
In <system.web> node, we can see the following xml elements (system.web node on a large number of articles that have been introduced are not repeated here):
<! - Trace file settings ->
dd key = "SystemFramework.Tracing.Enabled" value = "False" />
<! - Set this to the file with the trace settings. This file should be relative
to the root application directory. ->
dd key = "SystemFramework.Tracing.TraceFile" value = "DuwamishTrace.txt" />
<! - The TraceLevel for this switch. ->
dd key = "SystemFramework.Tracing.TraceLevel" value = "4" />
<! - This switch name. The trace level for this name can be set through
environment variables or the registry ->
dd key = "SystemFramework.Tracing.SwitchName" value = "DuwamishTraceSwitch" />
<! - This description of the Tracing.SwitchName switch ->
dd key = "SystemFramework.Tracing.SwitchDescription" value = "Error and information tracing for Duwamish" />
<! - Event log settings
Note: The default Duwamish7 event source name is created in the local machine during setup. If you wish to log events to a different event source
that event source must exist.
dd key = "SystemFramework.EventLog.Enabled" value = "True" />
dd key = "SystemFramework.EventLog.Machine" value = "." />
dd key = "SystemFramework.EventLog.SourceName" value = "Duwamish7" />
<! - Use the standard TraceLevel values:
0 = Off
1 = Error
2 = Warning
3 = Info
4 = Verbose ->
dd key = "SystemFramework.EventLog.LogLevel" value = "1" />
</ ApplicationConfiguration>
<! - Settings specific to the Duwamish application ->
dd key = "Duwamish.DataAccess.ConnectionString" value = "server = LUYAN \ NetSDK; User ID = Duwamish7_login; Password = password; database = 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" />
</ DuwamishConfiguration>
<! - Valid directories for source browsing. Keep these lower case. ->
dd key = "." value = "" />
dd key = "modules" value = "" />
dd key = ".. \ common \ data" value = "" />
dd key = ".. \ systemframework" value = "" />
dd key = ".. \ business \ facade" 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 = "" />
</ SourceViewer>
Configuration section information divided into two main areas: configuration section handler declaration area and the configuration section settings area, here is just the definition of the configuration section of the three sections set the region, it contains the actual configuration settings, see the description of its use of notes, all the configuration information must reside in <configuration> and </ configuration> root xml tags, configuration settings section is located in the region after region <configSections>.

Configuration section handler
As already introduced, section dealing with the definition in the class configuration section: Duwamish7.SystemFramework.ApplicationConfiguration and Duwamish7.Common.DuwamishConfiguration, they are located in SystemFramework and Common projects,. Net, all able to handle the type of configuration section must be achieved IConfigurationSectionHandler interface, while the IConfigurationSectionHandler interface is very simple, there is only one object Create (object parent, object configContext, XmlNode section) methods do not need to take the initiative to call this method, it is in this ConfigurationSettings.GetConfig automatically when a static method call, that is to say When you use the procedure to obtain the configuration section ConfigurationSettings.GetConfig time,. net change configurations according to a statement by the definition section of the class name and path configuration section automatically instantiated class and call the Create method. The following is a class called the Duwamish process:
1, the Application_OnStart method in global.asax inside ApplicationConfiguration.OnApplicationStart static method call, and the application of the absolute path of the root.

void Application_OnStart () (ApplicationConfiguration.OnApplicationStart (Context.Server.MapPath (Context.Request.ApplicationPath)); string configPath = Path.Combine (Context.Server.MapPath (Context.Request.ApplicationPath), "remotingclient.cfg"); if (File.Exists (configPath)) RemotingConfiguration.Configure (configPath);)

2, ApplicationConfiguration.OnApplicationStart static method call in System.Configuration.ConfigurationSettings.GetConfig approach to configuration section:

public static void OnApplicationStart (String myAppPath) (appRoot = myAppPath; System.Configuration.ConfigurationSettings.GetConfig ( "ApplicationConfiguration"); System.Configuration.ConfigurationSettings.GetConfig ( "DuwamishConfiguration"); System.Configuration.ConfigurationSettings.GetConfig ( "SourceViewer" );)

We have noticed, Duwamish and GetConfig not access the return value, as said earlier, GetConfig way to trigger the process configuration section of the Create method, so only need to configure the Create method of the value will be out there.
3, configured to read sample: Duwamish7.Common.DuwamishConfiguration category

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_PAGECACHEEXPIRESINSECONDS_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;)

Can be seen here, Duwamish does not own XmlNode from one hand to read the data inside, but directly to the data to a configuration NameValueSectionHandler to do the actual read, the work done by its own just to check if there is a practical definition of the configuration values , If not, the default value assigned.

At this point, web.config configuration values in the configuration will be read in class static variables in the future, elsewhere in the program can use to configure a static class variable to directly access the configuration values, for example, anywhere in the program, as long as Duwamish7.Common.DuwamishConfiguration.ConnectionString input can be: server = LUYAN \ NetSDK; User ID = Duwamish7_login; Password = password; database = Duwamish7; Connection Reset = FALSE this string. Better, you can extend its own configuration section handler and configuration section of the definition of more complex configuration since the pre-treatment.

.NET foundation Articles

Can't Find What You're Looking For?

Rating: Not yet rated


No comments posted.