Understanding Applications and State
2007-05-14 08:45
363 查看
What's New in 2.0
Session State Extensibility - Session State is now built upon a fully extensible, provider-based storage model. ASP.NET 2.0 ships with providers for in-process, out-of-process and Sql Server storage. However developers can implement custom providers that work against other data stores by deriving fromSessionStateStoreProviderBase. The Session State feature also allows developers to create custom session ID generators as well as custom HttpModules to use instead of the default
SessionStateModule. For scalability, the Sql Server and out-of-process Session State providers allow a developer to dynamically determine on a per-request basis the session state server that should load and store session data. This allows session state data to be spread across multiple backend session state servers.
Control State - In addition to ViewState, controls can now use a ControlState dictionary for round-tripping state information using the client. The ControlState dictionary is intended to store limited size information that is critical to the functionality of the control, that should not be disabled when EnableViewState is false.
Encrypted ViewState - ASP.NET 2.0 enables encryption of ViewState through the Page directive's ViewStateEncryptionMode attribute. This can be used to protect sensitive data that needs to be roundtripped in viewstate that should not be exposed to the client user. A common use for this feature might be to encrypt the DataKeyNames values from a GridView control.
This section discusses these and other application and state management features in ASP.NET 2.0.
What is an ASP.NET Application?
ASP.NET defines an application as the sum of all files, pages, handlers, modules, and executable code that can be invoked or run in the scope of a given virtual directory (and its subdirectories) on a Web application server. For example, an "order" application might be published in the "/order" virtual directory on a Web server computer. For IIS the virtual directory can be set up in the Internet Services Manager; it contains all subdirectories, unless the subdirectories are virtual directories themselves.Each ASP.NET Framework application on a Web server is executed within a unique .NET Framework application domain, which guarantees class isolation (no versioning or naming conflicts), security sandboxing (preventing access to certain machine or network resources), and static variable isolation.
ASP.NET maintains a pool of HttpApplication instances over the course of a Web application's lifetime. ASP.NET automatically assigns one of these instances to process each incoming HTTP request that is received by the application. The particular HttpApplication instance assigned is responsible for managing the entire lifetime of the request and is reused only after the request has been completed. This means that user code within the HttpApplication does not need to be reentrant.
Creating an Application
To create an ASP.NET Framework application you can use an existing virtual directory or create a new one. For example, if you installed Windows 2000 Server including IIS, you probably have a directory C:\InetPub\WWWRoot. You can configure IIS using the Internet Services Manager, available under Start -> Programs -> Administrative Tools. Right-click on an existing directory and choose either New (to create a new virtual directory) or Properties (to promote an existing regular directory).By placing a simple .aspx page like the following in the virtual directory and accessing it with the browser, you trigger the creation of the ASP.NET application.
function doClick(index, numTabs, id) {
document.all("tab" + id, index).className = "tab";
for (var i=1; i
td.code {
padding-left:10px;
padding-right:10px;
padding-top:0px;
padding-bottom:0px;
border-left: 1px solid #B1B1B1;
border-bottom: 1px solid #DADADA;
border-top: none;
border-right: 1px solid #DADADA;
}
td.tab {
text-align:center;
font: verdana;
width:15%;
border-top: 1px solid #B1B1B1;
border-bottom: none;
border-left: 1px solid #B1B1B1;
border-left: 1px solid #B1B1B1;
cursor: hand;
background: #F0F0F0;
padding: 3px;
}
td.backtab {
text-align: center;
font: verdana;
width: 15%;
border-top: 1px solid #B1B1B1;
border-right: none;
border-bottom: 1px solid #B1B1B1;
border-left: 1px solid #B1B1B1;
cursor: hand;
background: #E3E3E3;
padding: 3px;
}
td.space {
width:70%;
font: x-small verdana;
padding: 0px 0px 0px 0px;
border-top: none;
border-right: none;
border-bottom: 1px solid #B1B1B1;
border-left: 1px solid #B1B1B1;
background: white;
}
C# | VB | |
<%@Page Language="C#"%> <html> <body> <h1>hello world, <% Response.Write(DateTime.Now.ToString()); %></h1> </body> </html> <%@Page Language="VB"%> <html> <body> <h1>hello world, <% Response.Write(DateTime.Now.ToString()) %></h1> </body> </html> |
Lifetime of an Application
An ASP.NET Framework application is created the first time a request is made to the server; before that, no ASP.NET code executes. When the first request is made, a pool of HttpApplication instances is created and the Application_Start event is raised. The HttpApplication instances process this and subsequent requests, until the last instance exits and the Application_End event is raised.Note that the Init and Dispose methods of HttpApplication are called per instance and thus can be called several times between Application_Start and Application_End. Only these events are shared among all instances of HttpApplication in one ASP.NET application.
A Note on Multiple Threads
If you use objects with application scope, you should be aware that ASP.NET processes requests concurrently and that the Application object can be accessed by multiple threads. Therefore the following code is dangerous and might not produce the expected result, if the page is repeatedly requested by different clients at the same time.function doClick(index, numTabs, id) {
document.all("tab" + id, index).className = "tab";
for (var i=1; i
td.code {
padding-left:10px;
padding-right:10px;
padding-top:0px;
padding-bottom:0px;
border-left: 1px solid #B1B1B1;
border-bottom: 1px solid #DADADA;
border-top: none;
border-right: 1px solid #DADADA;
}
td.tab {
text-align:center;
font: verdana;
width:15%;
border-top: 1px solid #B1B1B1;
border-bottom: none;
border-left: 1px solid #B1B1B1;
border-left: 1px solid #B1B1B1;
cursor: hand;
background: #F0F0F0;
padding: 3px;
}
td.backtab {
text-align: center;
font: verdana;
width: 15%;
border-top: 1px solid #B1B1B1;
border-right: none;
border-bottom: 1px solid #B1B1B1;
border-left: 1px solid #B1B1B1;
cursor: hand;
background: #E3E3E3;
padding: 3px;
}
td.space {
width:70%;
font: x-small verdana;
padding: 0px 0px 0px 0px;
border-top: none;
border-right: none;
border-bottom: 1px solid #B1B1B1;
border-left: 1px solid #B1B1B1;
background: white;
}
C# | VB | |
<% Application["counter"] = (Int32)Application["counter"] + 1; %> <% Application("counter") = CType(Application("counter"), Int32) + 1 %> |
function doClick(index, numTabs, id) {
document.all("tab" + id, index).className = "tab";
for (var i=1; i
td.code {
padding-left:10px;
padding-right:10px;
padding-top:0px;
padding-bottom:0px;
border-left: 1px solid #B1B1B1;
border-bottom: 1px solid #DADADA;
border-top: none;
border-right: 1px solid #DADADA;
}
td.tab {
text-align:center;
font: verdana;
width:15%;
border-top: 1px solid #B1B1B1;
border-bottom: none;
border-left: 1px solid #B1B1B1;
border-left: 1px solid #B1B1B1;
cursor: hand;
background: #F0F0F0;
padding: 3px;
}
td.backtab {
text-align: center;
font: verdana;
width: 15%;
border-top: 1px solid #B1B1B1;
border-right: none;
border-bottom: 1px solid #B1B1B1;
border-left: 1px solid #B1B1B1;
cursor: hand;
background: #E3E3E3;
padding: 3px;
}
td.space {
width:70%;
font: x-small verdana;
padding: 0px 0px 0px 0px;
border-top: none;
border-right: none;
border-bottom: 1px solid #B1B1B1;
border-left: 1px solid #B1B1B1;
background: white;
}
C# | VB | |
<% Application.Lock(); Application["counter"] = (Int32)Application["counter"] + 1; Application.UnLock(); %> <% Application.Lock() Application("counter") = CType(Application("counter"), Int32) + 1 Application.UnLock() %> |
function doClick(index, numTabs, id) {
document.all("tab" + id, index).className = "tab";
for (var i=1; i
td.code {
padding-left:10px;
padding-right:10px;
padding-top:0px;
padding-bottom:0px;
border-left: 1px solid #B1B1B1;
border-bottom: 1px solid #DADADA;
border-top: none;
border-right: 1px solid #DADADA;
}
td.tab {
text-align:center;
font: verdana;
width:15%;
border-top: 1px solid #B1B1B1;
border-bottom: none;
border-left: 1px solid #B1B1B1;
border-left: 1px solid #B1B1B1;
cursor: hand;
background: #F0F0F0;
padding: 3px;
}
td.backtab {
text-align: center;
font: verdana;
width: 15%;
border-top: 1px solid #B1B1B1;
border-right: none;
border-bottom: 1px solid #B1B1B1;
border-left: 1px solid #B1B1B1;
cursor: hand;
background: #E3E3E3;
padding: 3px;
}
td.space {
width:70%;
font: x-small verdana;
padding: 0px 0px 0px 0px;
border-top: none;
border-right: none;
border-bottom: 1px solid #B1B1B1;
border-left: 1px solid #B1B1B1;
background: white;
}
C# | VB | |
<script language="C#" runat="server"> public static int counter; </script> <% System.Threading.Interlocked.Increment( ref counter ); %> <script language="VB" runat="server"> Public Shared counter As Integer </script> <% System.Threading.Interlocked.Increment( counter ) %> |
相关文章推荐
- Understanding Applications and State[1]->Application Structure
- Understanding Applications and State[3]->Using the Global.asax File
- Understanding Applications and State[2]->Managing State
- Understanding Applications and State[4]->HTTP Handlers and Factories
- Reinforcement Learning in Continuous State and Action Spaces: A Brief Note
- Object Recognition and Scene Understanding(二)OpenCV 中HOG特征参数个数及图解
- Flex And Session State Management
- Understanding and Investigating SSL Issues Support Pattern
- Configuring the Character Set and Collation for Applications
- Multi-threaded applications and asynchronous I/O(翻译)
- Understanding the Ping and Traceroute Commands
- The Evolution of the web and web applications
- visualizing and understanding convolution network论文
- Object Recognition and Scene Understanding(九)模型转化Mat-〉XML
- 《Computer Vision:Algorithms and Applications》学习笔记(一)——图像旋转算法与实现
- Loading drivers and Native applications from kernel mode, without touching registry
- 03. Understanding More about Events and PostBack
- 深度学习研究理解5:Visualizing and Understanding Convolutional Networks(转)
- Tooling support for events and public render parameters for portal and portlet applications
- Understanding HBase and BigTable