Joel's SharePoint Architect Blog

SharePoint 2010, MOSS & WSS Tips and Consultancy Tales

Subscribe Subscribe  View Joel Jeffery's profile on LinkedIn
joelblogs.co.uk | joelj.co.uk | joeljeffery.co.uk | jfdiphoenix.co.uk

Posts Tagged ‘ASP.NET’

Thu, 15 Nov 2007 18:07:39 GMT

weblogs.asp.net/rhoward

An excellent session from another non-Microsoft presenter. Another one of these brain-the-size-of-a-planet guys who’s career seems to flit in and out of Microsoft. Rob worked at Microsoft on all versions of ASP.NET.

How the pieces fit together

ASP.NET is not just the page model we’re all used to. It’s also a complete request / response framework. You could implement php on it if you were that way inclined.

ISAPI extensions

ASP.NET under IIS6 is simply a single ISAPI extension dll. This means you can use multiple versions of the .NET Framework alongside each other without conflicts; each lives in its own worker process.

Under the ASP.NET wp, it creates the HttpRuntime. The separate wp gives us true independence between apps on the same box. IIS6 has a kernel-level driver called http.sys, and creates a worker process w3wp.exe.

Http.sys introduces a concept called Kernel Mode Caching. This takes page cached items and stores and retrieves it from the IIS level, without drilling into ASP.NET, which gives us a lot more performance.

Role of HttpRuntime

  • Customise and Extend ASP.NET
  • Perform special authentication
  • Take over .jpg extension
  • Provide developrs a great way to innovate

ASP.NET runtime infrastructure
ASP.NET doesn’t care where it’s hosted; that is completely abstracted. HttpContext is created on hitting a page, and is flowed throughout the page lifetime. HttpHaners include all the various extension type: .aspx, .asph, .asmx.

HttpRuntime is the Request / Response infrastructure. It has access to everything you have access to in normal pages, without the Page model.

HttpRuntime can be hosted outside ASP.NET. This means you can process pages and their events outside IIS. Example of this is the Cassini web server. Another possible use would be in a tool that mirrors and flattens web sites to stage as HTML.

Interception Events
Synonymous with ISAPI filters. Can be used for request participation or termination of events etc. Interception Events can be coded with either IHttpModule components or global.asax. You can specify the order of precedence is .config. ISVs writing pluggable components would use Modules. A single project would probably use global.asax. Both provide the same functionality, but Modules are pluggable.

Handler lifecycle
It’s good to get a canonical list of events in the right order. So here they are:

  1. BeginRequest
  2. AuthenticateRequest
  3. AuthorizeRequest
  4. ResolveRequestCache
  5. AcquireRequestState
  6. PreRequstHandlerExecute
  7. Target Handler Extension (e.g. Page /.aspx)
  8. PostRequstHandlerExecute
  9. ReleaseRequestData
  10. UpdateRequestCache
  11. EndRequest

SessionState Tips

  • Turn it off if you don’t need it
  • Set it to read-only if you can
  • Only set it to read-write where you’ll be updating it

HandlerFactory

A tool to help you create different Handlers for different request types.

HttpHandler

Enables processing of Urls. Similar to ISAPI filters.

HttpContext

Encapsulates all request information. One instance created for each request, flowed throughout request lifetime. Developers can extend HttpContext.

Page Lifecycle

For completeness, here is the canonical list of events that fire in the lifetime of a page.

  1. PreInit
  2. Init
  3. InitComplete
  4. PreLoad
  5. Load
  6. Target Control Events
  7. Load Complete
  8. PreRender
  9. SaveStateComplete
  10. Render
  11. Unload

Summary
The presentation was excellent. Some detailed info, some just old hat. The Handler and Module stuff, while not new techniques, are traditionally the domain of the more hardcore ASP.NET developers. The question is why this detail, and why now? Well, my take on this is that there is a feature uplift coming: modularised ASP.NET in IIS7. This will create a whole new space for developers to play and write Modules and Handlers as first class citizens of the IIS pipeline. Microsoft wants us to play in that space, so that means getting folks used to the existing concepts now.

Technorati Profile

Technorati Tags: ASP.NET, TechEd 2007

Extending the IIS 7 Pipeline – Carlos Aguilar Mares

Tue, 13 Nov 2007 22:05:36 GMT

Carlos Aguilar Mares
Senior Development Lead

It seems the architecture of IIS has changed substantially with this release. If IIS6 was the bedrock that got us to Application Pools, and good .NET integration, IIS7 takes us to a new level. The IIS team has done a lot of refactoring to get there.

 

IIS6

IIS7

Monolithic server implementation

Componentised Server

Limited customisation

Pluggable modular functionality

Fixed functionality

Small generic request pipeline

Extensibility limited to ISAPI Filters

Enables lightweight servers and specialised servers

 

In IIS6, ASP.NET is implemented as an ISAPI filter, which sits right at the end of the pipeline. This caused a few problems – including duplication of features and wasted computation; authentication in ASP.NET would happen after IIS6 authenticated anyway. Another example of authentication badness in IIS6 occurs when you try to secure non-ASP.NET assets: you can only accomplish this by funnelling all HTTP requests via ASP.NET.

IIS7 offers two integrations models: Classic Mode (for compatibility – like IIS6) and Integrated Mode. ASP.NET is now implemented as a bundle of modules that fit into the redesigned IIS pipeline. Integrated Mode can process *all* requests and gives hider fidelity notifications.

.NET Extensibility

  • Existing ASP.NET API – using IHttpModel and IHttpHandler types
  • Expanded ASP.NET APIs
    • New HttpApplication Events
      • MapRequestHandler, LogRequest, PostLogRequest
    • Inject and modify IIS system headers and collections
      • HttpRequest.Headers
      • HttpReponse.Headers
      • HttpReponse.DisableKernelCache
      • HttpReponse.ServerVariables

Handlers vs Modules

 

Modules

Handlers

Provide services to all requests

Provides services to specific extensions

Basic Authentication module

Handlers ISAPI handler (.dll)

Compression module (etc)

Static handler (.htm, .jpg, .gif,  etc)

 

IIS 7 pipeline allows native and managed modules and handlers

  • “Integrated” Application Pool mode
    • use the managed modules and handlers
    • ASP.NET v2.0 and above can be configured to run in Integrated Mode
  • “Classic” Application Pool
    • IIS 6 style invocation of .NET
    • ASP.NET v1.1 and earlier can only be configured to run in Classic Mode

Building Managed Modules and Handlers

Modules – Building a Module to cause IIS to Log to SQL Server

The steps to build a module are more or less:

  • Add a class to your project – e.g.  public class SqlLogModule: IHttpModule
  • Edit the code!
    • Add to public void Init()
      context.LodgRequest += new EventHandler(OnLogRequest)
    • Fill in OnLogRequest
      HttpApplication app = (HttpApplication) sender;
      Log(app.Context); // TODO: write this function! :)
  • IIS Manager -> Configure Modules -> Managed Modules
    • Add Managed Module
      • Name "SqlLogModule"
      • Type "SqlLogModule"
        This adds web.config to our project!
  • Edit web.config <system.webServer> section. ASP.NET ignores this section; it’s only read by IIS7.

[Side question: how is all this possible? Won't ASP.NET 2.0 keel over at this point? Answer: no, .NET v2.0 was refactored for Vista and Server 2008.] 

Technorati Tags: ASP.NET, IIS Pipeline, TechEd 2007