04 HTTP Pipeline - Copy

Embed Size (px)

Citation preview

  • 8/6/2019 04 HTTP Pipeline - Copy

    1/28

    HTTP PipelineASP.NET WebMethod Internals

  • 8/6/2019 04 HTTP Pipeline - Copy

    2/28

    2

    WebMethod Internals

    WebMethod framework is built on .NET's HTTP pipeline

    functionality provided by .asmx HTTP handler

    (System.Web.Services.Protocols.WebServiceHandlerFactory)

    Understanding the pipeline is essential

    influences design choices

    defines configuration options

    provides extensibility hooks

  • 8/6/2019 04 HTTP Pipeline - Copy

    3/28

    3

    The HTTP Pipeline

    System.Web defines a server-side HTTP pipeline pipeline can run on top of any Web server

    pipeline configured via XML configuration files

    many of the pipeline classes are extensible

    applications, modules, and handlers define processing

  • 8/6/2019 04 HTTP Pipeline - Copy

    4/28

    4

    HTTP Pipeline Classes

    HTTP Pipeline Classes

    HttpWorkerRequest is low-level request

    HttpRuntime is pipeline entry point

    HttpContext wraps request in simpler object model

    HttpApplicationFactory provides HttpApplication for vroot

    HttpApplication processes messages via modules/handlers

    IHttpModule classes pre-/post-process requests

    IHttpHandlerFactory classes provide handler factories

    IHttpHandler classes process requests and generate response

  • 8/6/2019 04 HTTP Pipeline - Copy

    5/28

    5

    Module

    Module

    Handler

    HttpRuntime

    HttpApplication

    Factory

    HttpApplication

    Module

    HandlerFactory

    HttpContext

    HttpRequestHttpRespons

    e

    IHttpModule

    IHttpHandler-Factory

    IHttpHandler

    HTTP Pipeline

  • 8/6/2019 04 HTTP Pipeline - Copy

    6/28

    6

    Integration with IIS

    Pipeline relies on IIS to receive/dispatch requests by default

    requests dispatched by ISAPI extension: ASPNET_ISAPI.DLL

    ASPNET_ISAPI.DLL passes requests through named pipe to a

    worker process called ASPNET_WP.EXE

    ASPNET_WP.EXE processes requests using HTTP pipeline

    pipeline can be integrated with other web servers too

  • 8/6/2019 04 HTTP Pipeline - Copy

    7/28

    7

    inetinfo.exe

    aspnet_isapi.dll

    HTTP

    aspnet_wp.exe

    named pipe HttpRuntime

    IIS and the HTTP Pipeline

  • 8/6/2019 04 HTTP Pipeline - Copy

    8/28

    8

    Pipeline Process Model

    ASPNET_WP.EXE worker process completely configurable

    ASPNET_ISAPI.DLL reads processModel configuration

    configured in IIS Admin Console (Windows .NET Server)

    configured in global machine.config file (Windows XP)

    each worker process services unlimited requests by default

    lifetime of worker process can be limited by several factors

    when limit is reached, process is replaced with a new instance

  • 8/6/2019 04 HTTP Pipeline - Copy

    9/28

    9

    Implementing an HTTP handler

    HTTP handlers process a request and generate a response

    handlers are classes that implement IHttpHandler

    pipeline calls ProcessRequest when request arrives

    pipeline provides an HttpContext object

    handlers can be instantiated by default factory

    if handlers IsReusable property is true, handler will be reused

  • 8/6/2019 04 HTTP Pipeline - Copy

    10/28

    10

    namespace MyNamespace{public class BasicHandler : IHttpHandler{public void ProcessRequest(HttpContext ctx)

    {ctx.Response.ContentType = "text/xml";ctx.Response.Write("");ctx.Response.Write(ctx.Request.Url);ctx.Response.Write("");

    }

    public bool IsReusable { get { return true; } }}

    }

    Implementing IHttpHandler

  • 8/6/2019 04 HTTP Pipeline - Copy

    11/28

    11

    HttpContext

    The HttpContext class models HTTP message exchange

    Properties hold references to HttpRequest, HttpResponse,

    state management objects, etc.

    Current context always available to processing thread via static

    HttpContext.Current property

  • 8/6/2019 04 HTTP Pipeline - Copy

    12/28

    12

    HttpContext Property Description

    Application Per-application cross-request state

    Application Instance Application object processing request

    Cache Per-application cached state

    Handler Handler object processing request

    Items Per-request state

    Request HTTP request message

    Response HTTP response message

    Server Utility functionsSession Per-user cross-request state

    User User information

    HttpContext Properties

  • 8/6/2019 04 HTTP Pipeline - Copy

    13/28

    13

    Deploying an HTTP handler

    Once an HTTP handler is implemented, it must be deployed

    handler assembly placed in virtual directory's bin or GAC

    You must also configure when the handler is called

    using an .ashx file or

    adding an entry to web.config and the IIS script map

    \inetpub\wwwroot

    \mathweb.config\binmath.dll

    virtual directory

    bin subdirectory

  • 8/6/2019 04 HTTP Pipeline - Copy

    14/28

    14

    Referencing Handlers from .ashx Files

    Handlers can be referenced in .ashx files

    allows deployment without touching IIS or configuration files

    .ashx handler pre-configured with IIS during installation

    .ashx WebHandler declaration references handler class

    IHttpHandler-derived class

  • 8/6/2019 04 HTTP Pipeline - Copy

    15/28

    15

    .ashx and JIT Compilation

    An .ashx file can directly contain IHttpHandler code JIT compiled during first invocation

    using System.Web;

    public class BasicHandler : IHttpHandler{...

    }

  • 8/6/2019 04 HTTP Pipeline - Copy

    16/28

    16

    aspnet_isapi.dll

    IIS Configuration

  • 8/6/2019 04 HTTP Pipeline - Copy

    17/28

    17

    HTTP Pipeline Configuration Hierarchy

    HTTP pipeline is configured using a hierarchy of .config files root file in .NET installation directory (machine.config)

    other files in virtual directories and subdirectories (web.config)

    processing based on cumulative configuration data

  • 8/6/2019 04 HTTP Pipeline - Copy

    18/28

    18

    +

    c:\winnt\Microsoft.NET\Framework\v1.x\CONFIG\machine.config

    +

    c:\inetpub\wwwroot\foo\web.config

    c:\inetpub\wwwroot\foo\bar\web.config

    http://www.baz.com/foo/quux.ashx

    http://www.baz.com/foo/bar/quux.ashx

    Pipeline Configuration Files

  • 8/6/2019 04 HTTP Pipeline - Copy

    19/28

    19

    Adding a Handler Mapping

  • 8/6/2019 04 HTTP Pipeline - Copy

    20/28

    20

    +

    c:\winnt\Microsoft.NET\Framework\v1.x\CONFIG\machine.config

    c:\inetpub\wwwroot\foo\web.config

    http://www.baz.com/foo/quux.ashx

    Removing a Handler Mapping

  • 8/6/2019 04 HTTP Pipeline - Copy

    21/28

    21

    Useful Predefined HTTP Handlers

    Often useful to allow some operations but not others There are several predefined HTTP handlers to block requests

    HttpForbiddenHandler

    HttpMethodNotAllowedHandler

    HttpNotFoundHandler

    HttpNotImplementedHandler

    There is a also a standard HTTP handler for static files

    StaticFileHandler

  • 8/6/2019 04 HTTP Pipeline - Copy

    22/28

    22

    Mapping to Predefined HTTP Handlers

  • 8/6/2019 04 HTTP Pipeline - Copy

    23/28

    23

    WebMethod (.asmx) Handler

    WebMethod functionality provided by .asmx handler

    ...

  • 8/6/2019 04 HTTP Pipeline - Copy

    24/28

    24

    WebMethods and the HTTP Context

    WebMethods can access the current HttpContext Current static property accesses correct object for thread

    Provides access to Session, Application, etc.

    public class MathService{[WebMethod]public int Add(int n1, int n2){int sum = n1 + n2;HttpContext.Current.Application["last_sum"] = sum;return sum;

    }}

  • 8/6/2019 04 HTTP Pipeline - Copy

    25/28

    25

    Deriving from WebService

    WebService base class eases access to HttpContext provides public properties that expose HttpContext properties

    Request and Response are notable exceptions

    public class MathService : WebService{[WebMethod]public int Add(int n1, int n2){int sum = n1 + n2;Application["last_sum"] = sum;return sum;

    }}

  • 8/6/2019 04 HTTP Pipeline - Copy

    26/28

    26

    HTTP Request

    POST /math/add.asmx

    INETINFO.EXE

    (IIS)

    aspnet_wp.exe

    (ASP.NET Worker Process)

    aspnet_isapi.dll(ASP.NET

    ISAPI

    Extension)

    Built-in

    .asmx

    Handler

    class

    add.asmx

    HTTP Response

    HTTP/1.1200 OK ...

    Assembly with

    MathService

    class

    Compilesto

    IHttpHandler

    A Day in the Life of a WebMethod

    IIS metabase

    machine.config

    .asmx mapsto

    .asmx maps to

    XmlSerializerAttributes

    inform

  • 8/6/2019 04 HTTP Pipeline - Copy

    27/28

    27

    Summary

    HTTP pipeline supports server-side HTTP programming The pipeline integrates with IIS (by default)

    HTTP context modeled as hierarchy of objects

    HTTP handlers are endpoints for HTTP messages

    HTTP modules are intermediate filters for HTTP messages

    HTTP application objects coordinate request processing,

    tying modules and handlers together

    Pipeline behavior is controlled via hierarchy of XML files

  • 8/6/2019 04 HTTP Pipeline - Copy

    28/28

    28

    References

    HTTP Pipelines, MSDN Magazine,Ewald, Brown http://msdn.microsoft.com/msdnmag/issues/02/09/HTTPPipelin

    es/default.aspx

    Real World XML Web Services, Addison Wesley, Shohoud

    Building XML Web Services for the Microsoft .NET Platform,

    MSPress, Short