9th June - ASP.net Architecture

download 9th June - ASP.net Architecture

of 39

Transcript of 9th June - ASP.net Architecture

  • 7/29/2019 9th June - ASP.net Architecture

    1/39

    ASP.net Architecture

    Jeremy Boyd,Senior Technical Lead - IntergenMSDN Regional Director New Zealand

  • 7/29/2019 9th June - ASP.net Architecture

    2/39

  • 7/29/2019 9th June - ASP.net Architecture

    3/39

    Objectives

    Discuss history of ASP.NET

    Dynamic web content

    ASP

    Present new ASP.NET architecture

    .NET core

    Compilation

    Code behind Shadow copying

    Migration from ASP

  • 7/29/2019 9th June - ASP.net Architecture

    4/39

    Dynamic Web Content

    Dynamic web content has traditionally been

    generated using CGI, ISAPI, or ASP on

    Microsoft platforms

    The Common Gateway Interface (CGI) provides

    dynamic content by directly processing requests

    and issuing responses in a custom process

    The Internet Services API provides similar

    capability through filter DLLs, reducing theoverhead

    Active Server Pages (ASP) eliminates the need to

    author DLLs to provide dynamic content --

    inte rated server-side scri t enerates HTML

  • 7/29/2019 9th June - ASP.net Architecture

    5/39

    ASP

    Active Server Pages (ASP) simplify common

    tasks ISAPI DLLs were being used for

    ASP was introduced because web developers were

    building ISAPI extension DLLs to perform things likedatabase queries and posting back HTML

    Each different type of request required a new ISAPI

    extension DLL to be written

    ASP.DLL is a generic ISAPI DLL that reads .ASPfiles, parses and executes server side script blocks,

    and serves up result

  • 7/29/2019 9th June - ASP.net Architecture

    6/39

    ASP.NET == ASP.NExTversion

    On one hand, ASP.NET is an evolution of the

    ASP and is just the next version of ASP

    Same intrinsic objects available

    Script and html can be mixed

    Some ASP code can be ported with no changes

    Server-side Javascript is still supported

  • 7/29/2019 9th June - ASP.net Architecture

    7/39

    function Add(x, y){return x+y;

    }

    Test ASP Page2+2=

    server-side

    function

    interspersed

    server-side

    script

    server-side

    evaluation

    syntax

    server-side

    directive

    test.asp

    Sample ASP file

  • 7/29/2019 9th June - ASP.net Architecture

    8/39

    Fundamental change

    ASP.NET is more than just the next version of

    ASP

    Pages are compiled into assemblies improving

    performance and diagnostics

    Code-behind encourages better separation of code

    from HTML

    Extensible, server-side control architecture

    Server-side data binding model

    Form validation architecture

    Web services allow assemblies to expose

    themselves as SOAP servers

    S l ASP NET fil

  • 7/29/2019 9th June - ASP.net Architecture

    9/39

    Sample ASP.NET file

    int Add(int x, int y){return x+y;

    }

    Test ASP.NET Page

    2+2=

    server-sidefunction

    interspersedserver-side

    script

    server-side

    evaluation

    syntax

    server-side

    directive

    test.aspx

  • 7/29/2019 9th June - ASP.net Architecture

    10/39

    What is ASP.NET?

    At a high level, ASP.NET is a collection of

    .NET classes that collaborate to process an

    HTTP request and generate an HTTP

    response Some classes are loaded from system assemblies

    Some classes are loaded from GAC assemblies

    Some classes are loaded from local assemblies

    To work with ASP.NET, you must build your own

    classes that integrate into its existing class structure

    Some of your classes will be in pre-built assemblies

    Some of your classes will be in assemblies

  • 7/29/2019 9th June - ASP.net Architecture

    11/39

    High-level view of ASP.NET

    ASP.NET Worker Process

    HTTP Request

    GET /foo/foo.aspx

    HTTP Response

    HTTP/1.1 200 OK ...

    System Assemblies GAC Assemblies Local Assemblies

    system.web.

    dll

    system.data.dll

    mscorsvr.dll

    bargraph.dll

    mygacutil.dll

    acmeutil.dll

    mypage.dll

    32wie4kg.dll

    myctrl.dll

    AppDomain1

  • 7/29/2019 9th June - ASP.net Architecture

    12/39

    Pipeline architecture

    ASP.NET uses the CLR to replace IIS's

    ISAPI/ASP architecture

    User-defined handler objects used to dispatch

    HTTP requests

    Requests dispatched through ASP.NET-providedISAPI extension (aspnet_isapi.dll)

    Handlers run in an ASP.NET-provided worker

    process (aspnet_wp.exe in IIS 5,w3wp.exe inIIS 6)

    Many IIS features bypassed in favor of ASP.NET-

    provided features (WAM-based process isolation,

    ASP ob ect model and session mana ement

    HttpPipeline architecture (IIS 5 0)

  • 7/29/2019 9th June - ASP.net Architecture

    13/39

    GET /foo/foo.aspx HTTP/1.1 200 OK ...

    INETINFO.EXE (IIS 5.0)

    aspnet_isapi.dll

    (ISAPI Extension)

    aspnet_wp.exe

    (ASP.NET Worker Process)

    named pipe connectionhandler

    IHttpHandler

    Web Server (Win2000, XP)

    HttpPipeline architecture (IIS 5.0)

    HttpPipeline architecture (IIS 6 0)

  • 7/29/2019 9th June - ASP.net Architecture

    14/39

    GET /foo/foo.aspx HTTP/1.1 200 OK ...

    w3wp.exe

    (ASP.NET Worker Process)

    handler

    IHttpHandler

    Web Server (Win Server 2003)

    kernelhttp.sys

    aspnet_isapi.dll

    (ISAPI Extension)

    w3wp.exe

    (ASP.NET Worker Process)

    handler

    IHttpHandler

    Application Pool #1 Application Pool #2

    aspnet_isapi.dll

    (ISAPI Extension)

    HttpPipeline architecture (IIS 6.0)

  • 7/29/2019 9th June - ASP.net Architecture

    15/39

    Compilation vs. Interpretation

    When ASP.NET pages are first accessed, they are compiled into

    assemblies

    Subsequent access loads the page directly from the assembly

    Eliminates inefficiencies of the scripting model of ASP

    No performance difference between compiled components and embedded

    server-side code

    Debugging tools shared with all .NET development

    Whenever you author a new .aspx file, you are authoring a new class

  • 7/29/2019 9th June - ASP.net Architecture

    16/39

    Page compilation

    Every ASP.NET page is compiled into an assembly on first access

    The generated assembly contains a single class that derives from

    System.Web.UI.Page

    The generated Page-derived class is the file name of the page, replacing the"." with a "_" (like foo_aspx)

    The generated assembly is stored in the 'Temporary ASP.NET Files' directory

    on the server machine

    ASP net Page Compilation

  • 7/29/2019 9th June - ASP.net Architecture

    17/39

    HTTP Request

    GET /foo/foo.aspxIIS &

    ASP.NETPageParser::

    GetCompiledPageInstance

    Compiled

    assembly already

    exists?

    Locate foo.aspx

    Generate Page-derived

    class foo_aspx from file

    Compile to assembly

    Create foo_aspx

    instance

    Call

    Page::ProcessRequestHTTP Response

    HTTP/1.1 200 OK ...

    Content-Type: text/html;Content-Length: 300

    ...

    Page::ProcessRequestcalls

    Page::RenderControl

    yes

    no

    ASP.net Page Compilation

  • 7/29/2019 9th June - ASP.net Architecture

    18/39

    ASP.NET basics

    Each ASP.NET page is parsed and compiled into a class that extends

    System.Web.UI.Page

    Page class implements IHttpHandler

    A lot of the Page class is dedicated to forms/control processing

    Exposes HttpContext properties as own properties

    .aspx Type Information

  • 7/29/2019 9th June - ASP.net Architecture

    19/39

    Show Page Type

    .aspx Type Information

  • 7/29/2019 9th June - ASP.net Architecture

    20/39

    System.Web.UI.Page

    The Page class provides facilities for rendering HTML

    Response and Request objects are available as properties of theclass

    Methods for rendering are provided

    Events associated with generating the page are defined

    System.Web.UI.Page

  • 7/29/2019 9th June - ASP.net Architecture

    21/39

    class Page : TemplateControl, IHttpHandler{// State managementpublic HttpApplicationState Application {get;}public HttpSessionState Session {virtualget;}public Cache Cache {get;}

    // Intrinsics

    public HttpRequest Request {get;}public HttpResponse Response {get;}public HttpServerUtility Server {get;}publicstring MapPath(string virtualPath);

    // Client information

    publicstring ClientTarget {get; set;}public IPrincipal User {get;}//...

    }

    y g

    System.Web.UI.Page

  • 7/29/2019 9th June - ASP.net Architecture

    22/39

    class Page : TemplateControl, IHttpHandler{

    // Corepublic UserControl LoadControl(string virtualPath);publicvirtual ControlCollection Controls {get;}publicoverridestring ID { get; set;}

    publicbool IsPostBack {get;}

    protectedvirtualvoidRenderControl(HtmlTextWriter writer);

    // Eventspublicevent EventHandler Init;publicevent EventHandler Load;publicevent EventHandler PreRender;publicevent EventHandler Unload;

    //...}

    y g

  • 7/29/2019 9th June - ASP.net Architecture

    23/39

    Class creation

    Classes created from .aspx files can be customized

    Server-side script blocks are added to the class definition

    Member variables

    Member functions

    Interspersed script is added to a 'Render' function

    Executable code

    Aspx == Class

  • 7/29/2019 9th June - ASP.net Architecture

    24/39

    private ArrayList _values = new ArrayList();

    private void PopulateArray(){

    _values.Add("v1");_values.Add("v2");_values.Add("v3");_values.Add("v4");

    }

    aspx==class!

    member variable declaration

    member function declaration

    member function usage

    member variable usage

    p

    Server Side Code Placement in Page Compilation

  • 7/29/2019 9th June - ASP.net Architecture

    25/39

    private ArrayList m_values= new ArrayList();

    private void PopulateArray(){

    m_values.Add("v1");m_values.Add("v2");m_values.Add("v3");m_values.Add("v4");

    }

    // Machine-generated source file// ybngvkuj.0.csnamespace ASP {public class SamplePage_aspx :

    Page, IRequiresSessionState {

    private ArrayList m_values

    = new ArrayList();private void PopulateArray(){m_values.Add("v1");m_values.Add("v2");m_values.Add("v3");m_values.Add("v4");

    }public SamplePage_aspx() { /*...*/ }

    private void __Render__control1(HtmlTextWriter __output,

    Control parameterContainer){__output.Write(

    "\r\n");__output.Write(

    "\r\n\r\n");__output.Write("\r\n\r\n");

    PopulateArray();for (int i=0; i

  • 7/29/2019 9th June - ASP.net Architecture

    26/39

    Code behind

    In addition to customizing the generated Page class using embedded

    code, ASP.NET supports page inheritance

    Technique ofPage inheritance is called code-behind

    Supported through the Inherits attribute of the Page directive Promotes separation of code and presentation

    Code-behind files can be pre-compiled and placed in a directory named /bin at

    the top level of the application

    Code-behind files can be compiled on demand using the src attribute of thePage directive

    Sample Page with CodeBehind

  • 7/29/2019 9th June - ASP.net Architecture

    27/39

    aspx==class!

    p g

    SamplePage.cs

  • 7/29/2019 9th June - ASP.net Architecture

    28/39

    namespace EssentialAspDotNet.Architecture{publicclass SamplePage : Page

    { private ArrayList _values = new ArrayList();public SamplePage(){_values.Add("v1");_values.Add("v2");_values.Add("v3");

    _values.Add("v4");}

    protectedvoid WriteArray(){for (int i=0; i

  • 7/29/2019 9th June - ASP.net Architecture

    29/39

    System.Web.UI.Page

    EssentialAspDotNet.

    Architecture.SamplePage

    ASP.CodeBehind_aspx

    Using src attribute to automatically compile code behind

  • 7/29/2019 9th June - ASP.net Architecture

    30/39

    aspx==class!

  • 7/29/2019 9th June - ASP.net Architecture

    31/39

    Shadow Copying

    All assemblies in the /bin directory are shadow copied

    Placing assemblies in /bin makes them available to all pages in that application

    These assemblies are not referenced directly by ASP.NET

    Instead they are copied to an obscure location prior to loading

    If the original file ever changes, the file is re-copied

    Enables xcopy deployment

    Shadow Copy Mechanism

  • 7/29/2019 9th June - ASP.net Architecture

    32/39

    Referenced

    Assembly

    (Original)

    Shadow

    Copy (actually

    loaded)

    Fusion-

    managed

    directories

    Copied by

    assembly

    loaderShadowCopyDirectories

  • 7/29/2019 9th June - ASP.net Architecture

    33/39

    ASP->ASP.NET Migration

    Several options for migrating 'classic' ASP applications

    Run ASP side-by-side with ASP.NET, developing new pages/apps in ASP.NET

    Quickest path, but session and application state is not shared

    Convert existing ASP pages to ASP.NET (but keep using old libraries - ADOand msxml)

    Access to COM libraries incurs interop boundary crossing

    Convert existing ASP pages to ASP.NET with new libraries (ADO.NET and

    System.XML)

  • 7/29/2019 9th June - ASP.net Architecture

    34/39

    Converting ASP pages to ASP.NET

    Some pages can be converted by simply renaming to .aspx

    Most pages will need some 'touch up'

    Many directives have been changed (or removed)

    VBScript is not directly supported (must be VB.NET)

    COM object interaction may require ASPCOMPAT mode

    Code block placement different in ASP.NET

  • 7/29/2019 9th June - ASP.net Architecture

    35/39

    Updating directives from ASP->ASP.NET

    Several directives in ASP are no longer supported

    Instead, use equivalent ASP.NET directives/attributes

  • 7/29/2019 9th June - ASP.net Architecture

    36/39

    Not VBScript = VB.NET

    Many language changes mean VBScript code needs to be updated

    VB.NET doesn't support default properties

    objRS("au_fname") =>

    objRS.Fields("au_fname") IsNull won't work when testing DB results

    IsNull(dbField) => IsDBNull(dbField)

    Let and Set are no longer supported

    Set obj = CreateObject("xx") =>

    obj = CreateObject("xx")

  • 7/29/2019 9th June - ASP.net Architecture

    37/39

    Not VBScript = VB.NET

    Date() is no longer an expression (it is a type)

    Date() => DateTime.Now

    Parameters must be passed within parentheses

    Response.Write "hi" => Response.Write("hi")

  • 7/29/2019 9th June - ASP.net Architecture

    38/39

    Interacting with COM

    By default, ASP.NET applications will run in an

    MTA thread when accessing COM objects

    through interop

    Many classic ASP components are STA-threadedwhich means that all calls incur a thread-switch

    Avoid this by using the AspCompat attribute

  • 7/29/2019 9th June - ASP.net Architecture

    39/39

    Summary

    ASP.NET is an evolution of dynamic web

    page generation techniques

    All pages in ASP.NET are compiled

    assemblies

    Code-behind is a useful technique for

    separating code logic from presentation

    Shadow copying enables 'xcopy'

    deployment

    Migrating ASP applications typically

    requires some explicit conversion on your