The way to store and handle the storage and processing of several open source project configuration information Recently I have seen Duwamish7, ASP.NET Forums, Dottext several excellent open source (Microsoft official) projects because I am currently at the stage of the technical level, I It is more concerned about the specific implementation of these project procedures. The first article of architecture is the first: several open source project physical layer implementations compare this focus is different ways to store and process configuration information.
1. Duwamish7 and ASP.NET Forums have the same way, all of which implement the configuration class configuration class by implementing IconfigurationSectionHandler, as follows:
Public class
DuwamishConfiguration: iConfigurationSectionHandler
{Private static string dbConnectionString; private static bool enablePageCache; IConfigurationSectionHandler member #region IConfigurationSectionHandler members public object Create (object parent, object configContext, System.Xml.XmlNode section) {NameValueCollection settings; try {NameValueSectionHandler baseHandler = new NameValueSectionHandler (); settings = (NameValueCollection) baseHandler.Create (parent, configContext, section);} catch {settings = null;} if (! settings = null) {dbConnectionString = (string) settings [ "dbConnectionString"]; enablePageCache = Convert.ToBoolean (settings [ "enablepageCache"]);}} #ndregion public static string connectionstring {get {return dbconnectionstring;} } Public static bool enablepagecache {get {return enablepagecache;}}}
Web.config is as follows:
XML Version = "1.0" encoding = "UTF-8"?>
configsections>
Duwamishconfiguration>
configure>
Then you can get DuwamishConfiguration.Connectionstring to get database connections, duwamshconfiguration.xxxx gets the other data you defined, but before this, you need to call this method first, system.configuration.configurationSettings.getconfig ("duwamishconfiguration"); usually this method In Global.asa's Application_Start Event Process, or in your own HTTPModule's Application_Start similar event
More information about DUWAMISH7 configuration information, you can refer to: dUWAMISH in-depth analysis - Configuration article Learn Web.config by dUWAMISH WEB.CONFIG 2, Dottext configuration information storage and handling Dottext configuration information is not placed in Web.config, but put it in One of your own defined blog.config files:
XML Version = "1.0" encoding = "UTF-8"?>
BlogconfigurationSettings>
Then obtain data by serialization, first define the corresponding class:
[Serializable] public class
BlogconfigurationSettings
{Private string _connectionString; public string ConnectionString {get {return _connectionString;} set {_connectionString = value;}} private bool _enablePageCache; public bool EnablePageCache {get {return _enablePageCache;} set {_enablePageCache = value;}}} may then be as follows Methods These configuration class objects are obtained:
Public Static
BlogconfigurationSettings Instance (HTTPContext Context)
{// In practical applications, not forget to put the cache string filepath = context.Server.MapPath ( "~ / blog.config"); settings = (BlogConfigurationSettings) LoadSerializedObject (typeof (BookConfigurationSettings), filepath); return settings }
Public Static Object LoadSerializationObject (Type Type, String
Filename)
{FileStream fs = null; try {// open the stream fs = new FileStream (filename, FileMode.Open, FileAccess.Read); XmlSerializer serializer = new XmlSerializer (type); return serializer.Deserialize (fs);} catch (Exception e) {throw e;} finally {i (fs! = null) fs.close ();}}
As for the exception, it's a specific application, this is the problem of seeing people.