340 likes | 503 Views
ASP.NET. XIE Mao-Qiang. 1- Overview of Req.'s Proc. GET /foo/foo.aspx. HTTP/1.1 200 OK…. aspnet_wp.exe (ASP.NET Worker Process). named channel. Page class. IHttpHandler. aspnet_isapi.dll (ISAPI Extension). INETINFO.EXE(IIS5.0). Foo.aspx. Foo.aspx Assembly.
E N D
ASP.NET XIE Mao-Qiang
1- Overview of Req.'s Proc. GET /foo/foo.aspx HTTP/1.1 200 OK… aspnet_wp.exe (ASP.NET Worker Process) named channel Page class IHttpHandler aspnet_isapi.dll (ISAPI Extension) INETINFO.EXE(IIS5.0) Foo.aspx Foo.aspx Assembly
1- Overview of Req.'s Proc. request aspnet_wp.exe AppDomain: /LM/W3svc/1/Root/MyApp-1-126702642672256757 1 HttpRequest IHttpMapPath HttpWorkerRequest HttpResponse HttpApplication 2 HttpSessionState IServiceProvider 3 HttpApplicationState HttpContext 4 HttpApplicationFactory 5 Module1 IHttpAsyncHandler IHttpHandler 6 Module2 HttpApplication IComponent Module n 7 IHttpHandler HandlerFactory Processor IHttpHandlerFactory 8
1- Flow of a Request 1/2 Create instance of HttpWorkerRequest, which include request's URL and input value. Pass this instance to HttpRuntime's static method ProcessRequest. Method HttpRuntime.ProcessRequest creates an instance of HttpContext, which records all related info about current request and be initialized by HttpWorkerRequest. 5. Call static method HttpApplicationFactory.GetApplicationInstance to generate from sky or get one instance from object pool
1- Flow of a Ruquest 2/2 6. Initialize the instance of HttpApplication and create all modules defined for current web application. 7. After creating the modules, method HttpRuntime.BeginProcessRequest will dispatch the HttpApplication instance to current request. And HttpApplication will select right handler through IHttpHandlerFactory. In case an aspx page is requested, PageHandlerFactory will create right PageHandler, which almost is the inherited class from class page. 8. After getting the handler, handler's ProcessRequest will be called.
1- HttpContext 1/3 This class maintain all data related with current request, and can be accessed by most element. The properties of HttpContext
1- HttpContext 2/3 This class maintain all data related with current request, and can be accessed by most element. The properties of HttpContext
1- HttpContext 3/3 This class maintain all data related with current request, and can be accessed by most element. The properties of HttpContext
1- Thinking What can we learn from HttpContext ?
1- HttpContext's Example <%! file:global.asax %> <%@ Application="C#" %> <object id="MyGlobalCollection" runat="server" scope="application" class="System.Collection.ArrayList"> <script> protected void Application_BeginRequest(object o, EventArgs e){ this.Context.Items["StartTime"]=DateTime.Now; } protected void Application_EndRequest(object o, EventArgs e){ DateTime dt=(DateTime)this.Context.Items["StartTime"]; TimeSpan ts=DateTime.Now-dt; this.Context.Response.Output.Write(ts); } </script>
1- Extension of Http Pipeline HttpApplication It provides events in application area, and the developer can implement authority, performance counting, customized logging, load balance… 2. Customized Handler It can be an endpoint of some request, and reject the complicated rules of Page. 3. Customized Module You can provide filter/monitor to every request.
1.1- Customized Handler 1/3 1) Composing a class implementing interface IHttpHandler public interface IHttpHandler { void ProcessRequest(HttpContext ctx); bool IsReusable{get;} } true if the IHttpHandler instance is reusable; otherwise, false.
1.1- Customized Handler 2/3 2) When ASP.NET get a request to calc.calc, it will get handler which is an instance of class "CalcHandler". <!-- file:web.config --> <configuration> <system.web> <httpHandlers> <add verb="GET" path="calc.calc" type="CalcHandler, CalcHandler" /> </httpHandlers> </system.web> </configuration>
1.1- Customized Handler 3/3 3) Configure IIS and let it use iis_isapi.dll to process the request to .calc
1.1- And You can also… <!-- file:Clac.ashx --> <%@ WebHandler Language=C# Class="CalcHandler"%> using System; using System.Web; public class CalcHandler : IHttpHandler { public void ProcessRequest(HttpContext ctx) { … } public bool IsReusable{get{return false;}} }
1.1- IHttpHandlerFactory This factory will give you an instance, which can be created or got from instances pool. HandlerFactory's deployment is same with the handler's. public interface IHttpHandlerFactory { IHttpHandler GetHandler(HttpContext ctx, string requestType, string url, string translatePath); void ReleaseHandler(IHttpHandler handler); }
1.2- Customized Module Customized module is often used to preprocess or postprocess the request, and it's very useful. It can live in all application life period. Defined Module in ASP.NET
1.2- Customized Module 1/3 This factory will give you an instance, which can be created or got from instances pool. HandlerFactory's deployment is same with the handler's. public interface IHttpModule { void Dispose(); void Init(HttpApplication context); }
1.2- Customized Module 2/3 public class TimerModule : IHttpModule { public void Dispose(){} public void Init(HttpApplication httpApp) { httpApp.BeginRequest += new EventHandler(this.OnBeginRequest); httpApp.EndRequest += new EventHandler(this.OnEndRequest); } public void OnBeginRequest(object o, EventArgs ea){ HttpApplication httpApp = o as HttpApplication; httpApp.Context.Items["sTime"] = DateTime.Now; } public void OnEndRequest(object o, EventArgs ea) { HttpApplication httpApp = o as HttpApplication; DateTime dt = (DateTime)httpApp.Context.Items["sTime"]; TimeSpan elapsed = DateTime.Now - dt; httpApp.Context.Response.Write("<BR/>Time again" + elapsed.ToString()); } }
1.2- Customized Module 3/3 <!-- file:web.config --> <configuration> <system.web> <httpModules> <add name="Timer" type="TimerModule, TimerModuleAssembly.dll" /> </httpModules> </system.web> </configuration>
2- State Management 1/3 Client get an unique session ID after requesting. And Web Application(Server) will know client’s unique session ID when client request next time. Web Application will get the information according the unique session ID
2- State Management 2/3 • Client-Side Cookies URL Rewriting Hidden fields in form submitted(ViewState) • Server-Side Session is stored in memory of single machine Session is stored in many machine (Occasion?) Session is stored persistence (Occasion?)
2- State Management 3/3 • Client-Side We want that our information is safe and can be post back. • Server-Side Service provider want that server will be scalable and they will survive through crashing.
2- How to manage ? In web.config or machine.config <configuration><system.web> <sessionstate mode=“Inproc” cookieless=“false” timeout=“20”> </system.web> </configuration> Attribute of class SessionState is listed at Table7-2, page 247
3- Cache • Cache page or user control output • Vary the cache based on user input • Cache arbitrary data • Expire a cache item when another item changes
3- Cache page or .ascx output Add the @OutputCachedirective to the page or user control, or use the methods exposed by Response.Cache
3- @OutputCache Directive Duration number of seconds VaryByParam “*” or “None” Location Any, Client, Downstream, Server, None. Default is any VaryByControl Control’s properties VaryByHeader HTTP request headers VaryByCustom
3- Cache Page <%@ Page Language=C#%> <%@ OutputCache duration=4 VaryByParam="None"%> <HTML><HEAD><TITLE> New Document </TITLE> <script runat=server> void Page_Load(){ label1.Text = DateTime.Now.ToString(); } </script></HEAD><BODY> <asp:Label id=label1 runat=server/> </BODY> </HTML>
3- Cache User Controls(ascx) <%@ Control Language=C#%> <%@ OutputCache duration=4 VaryByParam="None"%> <HTML><HEAD><TITLE> New Document </TITLE> <script runat=server> void Page_Load(){ label1.Text = DateTime.Now.ToString(); } </script></HEAD><BODY> <asp:Label id=label1 runat=server/> </BODY> </HTML>
3- Cache User Controls(aspx) <%@ Control Language=C#%> <%@ Register TagPrefix=X TagName=UC Src="FragCache.ascx"%> <HTML><HEAD><TITLE> New Document </TITLE> <script runat=server> void Page_Load(){ label1.Text = DateTime.Now.ToString(); } </script></HEAD><BODY> <asp:Label id=label1 runat=server/> <X:UC runat=server/> </BODY></HTML>
3- Vary the Cache based-on user input Add the VaryByParam, VaryByHeader, or VaryByControl attributes to the @OutputCache directive. Let’s modify “OutWithCache.aspx” <%@ OutputCache duration=10 VaryByParam="*"%> and change URL quickly.
3- Cache Arbitrary Data Insert items into the cache using either the simple syntax: Cache[“myKey”] = myItem; or by calling the Add or Insert methods of the Cache class. Let’s look the file “CacheAuthors.aspx”