Duwamish in-depth analysis - configuration articles

xiaoxiao2021-03-06  150

Introduction: Almost everything in this section of the ASP.NET programming, when talking about how to manage the database connection string, use the database connection string in the Web.config file and the following access procedures: 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 recompiling the entire application, which gives a very convenient for the deployment and transplantation of the application. 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.

-------------------------------------------------- ---------------------------- Configure the section handler declaration in the duwamish solution, the web.config file is placed in a web project Because Web.config requires IIS and ASP.NET Runtime management and support, it should be placed in a virtual directory, let's take a look at its first part:

See three configuration segment processing program declarations (section), which must appear in the top of the configuration file and Mark, here, they only use the Name and Type properties, where the name attribute defines the name of the specified configuration section, and the Type property specifies the configuration section processing of the designation from the configuration file. The name of the program class, there are two parts, the front is the class name of the handler, and the 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 on 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 (About the SYSTEM.WEB node has a large number of articles, this is no longer repeated): - set this to the file with the trace settings. This file shop.traceframework.tracing.tracefile "value =" duwamishtrace.txt "/> < - The Tracelevel for this Switch. -> <-! This description of the Tracing.SwitchName switch -> <

! - Use the standard tracelevel value: 0 = OFF 1 = error 2 = warning 3 = info 4 = verbose -> < ! DuwamishConfiguration> <- Settings specific to the Duwamish application ->

and Root XML tag is located after the area is located in the area. -------------------------------------------------- ------------------------------ Configure the section handler before, the section defines the processing section: dUWAMISH7 .SystemFramework.ApplicationConfiguration and Duwamish7.Common.DuwamishConfiguration, they are located SystemFramework and Common project, .net requires all able to handle configuration section IConfigurationSectionHandler class must implement the interface, the interface is very simple and IConfigurationSectionHandler, only one object Create (object parent , the Object ConfigContext, the XMLNode Section method, this method does not require active call, it is automatically called when configurationSettings.getconfig is static, that is, when you use configurationSettings.getconfig to get the configuration section when you use configurationSettings.getConfig in the program , .NET will automatically instantiate the configuration section processing class and call the CREATE method based on the class name and path defined in the changing section declaration. Below is Duwamish's processing class calling process: 1. The ApplicationConfiguration.onApplicationStart static method is called in Global.asax's Application_onstart method and obtains an absolute path of the application 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 in the method for processing calls System.Configuration.ConfigurationSettings.GetConfig 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 did not get GetConfig returned, because mentioned above, the getConfig method triggered the CREATE method of the configuration section handler, so only need to take the configuration value in the CREATE method.

3, an example configuration read: 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_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.ReadSett ing (settings, WEB_ENABLEPAGECACHE, WEB_ENABLEPAGECACHE_DEFAULT); enableSsl = ApplicationConfiguration.ReadSetting (settings, WEB_ENABLESSL, WEB_ENABLESSL_DEFAULT);} return settings;} can be seen here, in fact, not the Duwamish own hand to read data from an XmlNode inside, but directly to the The data is forwarded to a NameValueSectionHandler Do the actual configuration read, and its own work is just to check if there is an configuration value of the actual definition. If not, it gives the default value.

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

New Post(0)