Application Configuration


One thing alluded to throughout this chapter is the existence of a conceptual application containing Web pages and configuration settings. This is an important concept to grasp, especially when configuring your Web site for multiple concurrent users.

A few notes on terminology and application lifetime are necessary here. An application is defined as all files in your project, and is configured by the Web.Config file. An Application object is created when an application is started for the first time, which will be when the first HTTP request arrives. Also at this time the Application_Start event is triggered and a pool of HttpApplication instances is created. Each incoming request receives one of these instances, which performs request processing. Note that this means HttpApplication objects do not need to cope with concurrent access, unlike the global Application object. When all HttpApplication instances finish their work, the Application_End event fires and the application terminates, destroying the Application object.

The event handlers for the events mentioned earlier (along with handlers for all other events discussed in this chapter) can be defined in a Global.asax file, which you can add to any Web site project. The generated file contains blanks for you to fill in; for example:

  protected void Application_Start(Object sender, EventArgs e) { } 

When an individual user accesses the Web application, a session is started. Similar to the application, this involves the creation of a user-specific Session object, along with the triggering of a Session_Start event. Within a session, individual requests trigger Application_BeginRequest and Application_EndRequest events. These can occur several times over the scope of a session as different resources within the application are accessed. Individual sessions can be terminated manually, or will time out if no further requests are received. Session termination triggers a Session_End event and the destruction of the Session object.

Against the background of this process, you can do several things to streamline your application. If all instances of your application use a single, resource-heavy object, for example, you might consider instantiating it at the application level. This could improve performance and reduce memory usage with multiple users, because in most requests no such instantiation will be required.

Another technique you can use is to store session-level information for use by individual users across requests. This might include user-specific information extracted from a data store when the user first connects, and available until the user ceases to submit requests or logs out.

These techniques are beyond the scope of this book - and you might want to consult Professional ASP.NET 2.0 (Wiley Publishing, Inc., ISBN 0-7645-7610-0) for details - but it helps to have a broad understanding of the processes.

Finally, you need to look at Web.Config files. A Web site will usually have one of these in its root directory (although it is not created for you by default), and may have additional ones in subdirectories to configure directory-specific settings (such as security). The PCSWebApp3 Web site developed in this chapter received an auto-generated Web.Config file when you added a stored database connection string, which you can see in the file:

  <connectionStrings>   <add name="MRBConnectionString" connectionString="Data Source=.\SQLEXPRESS;      AttachDbFilename=|DataDirectory|\MeetingRoomBooker.mdf;      Integrated Security=True;User Instance=True"      providerName="System.Data.SqlClient" /> </connectionStrings> 

If you ran the project in debug mode, you’ll also see some additional settings in the Web.Config file.

You can edit Web.Config files manually, but you can also configure Web sites (and their underlying configuration files) using a tool. This tool is accessible on the Website menu in Visual Studio, under ASP.NET Configuration. The display for this tool is shown in Figure 32-15.

image from book
Figure 32-15

As you can see from the text, this tool lets you configure a number of settings, including security. You see a lot more of this tool in the next chapter.




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net