230 likes | 462 Views
Configuration. Configuring ASP.NET. ASP.NET is configured via an extensible configuration file format ASP.NET configuration controlled using an XML file named web.config Typically located at the top-level of the virtual root directory ASP.NET configuration happens post-IIS metabase.
E N D
Configuring ASP.NET • ASP.NET is configured via an extensible configuration file format • ASP.NET configuration controlled using an XML file named web.config • Typically located at the top-level of the virtual root directory • ASP.NET configuration happens post-IIS metabase web.config <?xmlversion="1.0"?> <configuration> <system.web> <sessionStatemode='InProc'timeout='10'/> </system.web> </configuration>
Hierarchical file layout • Configuration files for ASP.NET can be in one of 4 places: • machine • site • application • subdirectory • Configuration settings are applied hierarchically • Root machine.config file in .NET installation directory is applied first • Any web.config file in the site directory is applied next • The web.config file at the root of the virtual directory is applied next • Any web.config files found in sub-directories under the virtual root are applied last
Location element • Location element can be used to apply settings to specific files or subdirectories from a top-level configuration file • Sometimes preferable to spreading web.config files throughout your directory structure • Specify a named path and configuration settings to apply web.config <?xmlversion="1.0"?> <configuration> <locationpath="bar"> <system.web> <httpHandlers> <removeverb="*"path="*.ashx"/> </httpHandlers> </system.web> </location> </configuration>
Element Placement • Some elements are restricted in where they can be applied • Elements that cannot be applied at the subdirectory level include • authentication • sessionState • trust • httpModules (do not take effect) • The processModel element can only occur in machine.config
Impact of configuration changes • Any change to an application's web.config file will restart that application • its application domain will be unloaded and re-created • any in-process state will be lost (session, application, etc.)
Configuration Data • Custom application configuration data may be stored in web.config and accessed via static property accessors • Ideal place to store DSN or other global settings that may need to be changed at deployment • The appSettings element in the web.config file supports adding key/value pairs that can be retrieved from within an application at runtime • The value of a key is retrieved through the Context.GetConfig() method of the HttpContext class, or through the ConfigurationSettings.AppSettings property
Accessing configuration data using static property accessors Accessing configuration data using static property accessors web.config <configuration> <appSettings> <addkey="DSN" value="server=.;trusted_connection=yes;database=test"/> </appSettings> <!-- other settings--> </configuration> somepage.aspx publicvoid Page_Load(object src, EventArgs e) { string dsn = ConfigurationSettings.AppSettings["DSN“]; // use dsn here }
Process model configuration • The lifetime of the aspnet_wp.exe worker process (IIS 5) is configurable • aspnet_isapi.dll reads the processModel configuration section in global machine.config file • Each worker process used to service unlimited requests by default • In IIS 6 the worker process w3wp.exe can be controlled per application pool • processModel element ignored • settings drawn from metabase.xml
Limiting worker process usage • Lifetime of worker process can be limited by several factors • Total lifetime of process • Total time idle • Number of requests • Amount of memory consumed • When limit is reached, process is replaced with a new instance • New requests queued to new process • Old process handles pending requests before shutting down
processModel element in machine.config (IIS 5) <!-- machine.config --> <configuration> <system.web> <processModel enable="true" timeout="Infinite" idleTimeout="Infinite" shutdownTimeout="0:00:05" requestLimit="Infinite" requestQueueLimit="5000" restartQueueLimit="10" memoryLimit="60" webGarden="false" cpuMask="0xffffffff" userName="MACHINE" password="AutoGenerate" logLevel="Errors" clientConnectedCheck="0:00:05" comAuthenticationLevel="Connect" comImpersonationLevel="Impersonate"/> ... </system.web> ... </configuration>
Referencing GAC Assemblies • To add a reference to an assembly deployed in the GAC • Use the assemblies element with an add sub-element <configuration> <!-- ... --> <system.web> <compilationdebug="true"> <assemblies> <addassembly="util, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a77a5c561934e089"/> </assemblies> </compilation> </system.web> </configuration>
Configuration file extensibility • Configuration files are divided into two distinct sections • The configuration section handlers • Contains classes that parse specific sub-sections of the data portion of the file • The configuration data • Contains data parsed by the configuration section handlers
Building custom configuration section handlers • You can build your own configuration section handlers • Useful if appSettings element is not sufficient • Create a class that implements IConfigurationSectionHandler • Define a class/struct to store the configuration data • Read the incoming XML in Create() and return a reference to the configuration structure • Install the handler by adding it to the configSections element publicinterface IConfigurationSectionHandler { object Create(object parent, object input, XmlNode node); }
Leveraging NameValueFileSectionHandler • NameValueFileSectionHandler can be used to define generic configuration sections with name/value pairs • Lets you define a custom section without going to the trouble of building your own custom class <configuration> <configSections> <sectionname="myGroup" type="System.Configuration.NameValueFileSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> </configSections> <myGroup> <addkey="font"value="Courier New"/> <addkey="verticalWidth"value="800"/> </myGroup> <!-- ... --> </configuration>
Accessing custom configuration section using NameValueFileSectionHandler protectedvoid Page_Load(object src, EventArgs e) { NameValueCollection set; set = ConfigurationSettings.GetConfig("myGroup") as NameValueCollection; // use set here (like set["font"], // set["verticalWidth"], etc. }
Summary • ASP.NET applications are configured through web.config files • web.config files store configuration settings in an extensible XML format • Static application data can be stored and retrieved in web.config files • processModel controls lifetime and properties of ASP.NET process • configuration files are extensible
For more information, please contact: Uladzimir Tsikhon Software Engineering Manager, Belarus Recourse Development Department EPAM Systems, Inc. Belarus, MinskPhone: +375(17) 2101662 ext 1756 Fax: +375(17) 2101168 Email: uladzimir_tsikhon@epam.com http://www.epam.com