IIS7 modules vs. IIS6 ISAPI #9: Intuitive object model

This is the second post in the series on why IIS7 module development beats the pants off the ISAPI development for the previous version of IIS.

Last time, we talked about the class-based encapsulation model for building IIS modules, and how it’s based on C++ classes with type-safe methods as opposed to ISAPI’s static C-style function entrypoints with VOID arguments.

With ISAPI, the majority of server interactions were exposed through a generic SupportFunction callback available off the EXTENSION_CONTROL_BLOCK or HTTP_FILTER_CONTEXT  structures provided to the ISAPI Extension or ISAPI filter entrypoint functions:

BOOL ServerSupportFunction(

      HCONN ConnID,

      DWORD dwServerSupportFunction,

      LPVOID lpvBuffer,

      LPDWORD NULL,

      LPDWORD NULL

);

The ISAPI would perform the required operation by invoking this callback with the specific Function ID, and pass required arguments via the generic arguments.  For example, to check whether the client is still connected, the ISAPI would do:

BOOL fConnected = FALSE;

if ( !pECB->ServerSupportFunction( pECB->connID,

                                   HSE_REQ_IS_CONNECTED,

                                   &fConnected,

                                   NULL,

                                   NULL ) )

{

    hr = HRESULT_FROM_WIN32 ( GetLastError() );

    goto Finished;

}

The developer would first have to read documentation to figure out what support functions were available, and really carefully reading about what each function expected in its arguments.  Errors where the wrong values were being passed into the function that caused AVs or strange behavior because the function was expecting something else were extremely common.

In contrast, IIS7 provides a rich class-based API that exposes server functionality via intuitive server objects.  When the server calls one of the callback methods on your CHttpModule-derived instance, it always provides an instance of the request’s IHttpContext interface, from which you module can access all other objects associated with the request:

REQUEST_NOTIFICATION_STATUS

CMyHttpModule::OnAcquireRequestState(

    IN IHttpContext *             pHttpContext,

    IN OUT IHttpEventProvider *   pProvider

)

{

    IHttpRequest*  pRequest 
                      = pHttpContext->GetRequest();

    IHttpResponse* pResponse
                      = pHttpContext->GetResponse();


    //

    //  Do stuff with request and response

    //

}

With this model, the code needed to determine whether the client is still connected becomes:

BOOL   fConnected = pHttpContext->GetConnection()->IsConnected();

Each of the classes in the API provide strongly typed methods for interacting with the server and providing request processing, such as:

class IHttpRequest

{

    virtual

    PCSTR

    GetHeader(

        IN PCSTR       pszHeaderName,

        OUT USHORT *   pcchHeaderValue = NULL

    ) const = 0;

    virtual

    PCSTR

    GetHttpMethod(

        VOID

    ) const = 0;

    virtual

    PSOCKADDR

    GetRemoteAddress(

        VOID

    ) const = 0;

    virtual

    HRESULT

    GetClientCertificate(

        OUT HTTP_SSL_CLIENT_CERT_INFO ** ppClientCertInfo,

        OUT BOOL *                       pfClientCertNegotiated

    ) = 0;

    // and many others

};

class IHttpResponse

{

    virtual

    HRESULT

    SetStatus(

        IN USHORT                   statusCode,

        IN PCSTR                    pszReason,

        IN USHORT                   uSubStatus = 0,

        IN HRESULT                  hrErrorToReport = S_OK,

        IN IAppHostConfigException *pException = NULL,

        IN BOOL                     fTrySkipCustomErrors = FALSE

    ) = 0;

    virtual

    HRESULT

    SetHeader(

        IN PCSTR      pszHeaderName,

        IN PCSTR      pszHeaderValue,

        IN USHORT     cchHeaderValue,

        IN BOOL       fReplace

    ) = 0;

    virtual

    VOID

    ResetConnection(

        VOID

    ) = 0;

    // and many others ...

};

Besides being much more intuitive, the IIS7 object model is significantly more powerful / expressive then ISAPI.  The biggest reason for this is the fact that all server features that IIS ships with are written on top of these APIs – as opposed to being built into the server using private APIs like it was the case with previous versions.  While it was acceptable before to expose limited extensibility and have the built in features use private interfaces, now IIS has the requirement of exposing all necessary interfaces needed to build every single feature it ships with.  

All in all, the new IIS7 object model provides 50+ classes and 300+ methods for providing request processing and otherwise interacting with the server.  The object model is fairly similar to the ASP.NET object model, so if you have played with ASP.NET development, you will find most concepts familiar (of course, C++ development experience required).

To learn more:
- To learn more about building IIS7 modules, be sure to check out
Developing a Native (C/C++) Module and the associated IIS7 Module Starter Kit for C++.
- To learn more about the other improvements in the IIS7 core server and extensibility, check out the IIS7 Core Web Server.
- As always, feel free to leave comments or post questions here or on IIS.NET Forums.

Next up - still 8 more ways we have improved IIS development in IIS7 - and these are about to get even better / more specific.

Published 12 October 06 08:53 by Mike Volodarsky
Filed under: , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Sam said on December 19, 2006 2:55 PM:
Posted on 10/12/2006 9:36 PM:

you rule man!

can't wait for the whole countdown of reasons letterman!
# Samoil Samak said on December 19, 2006 2:56 PM:
Posted on 10/13/2006 6:49 PM:

This time i actually read the entry.

you still rule.
# David Wang said on December 19, 2006 2:57 PM:
Posted on 10/15/2006 6:56 PM:

Yo, where is the custom authentication sample that you promised back in reason #10?

Personally, unless I have already written all blog entries of a sequence (10 in this case), I will only start with entries that increment from reason #1... and I never say "in the next post" but rather "in a future post"...

Remember the moto of: UnderPromise, OverDeliver... :)
# Mike Volodarsky said on December 19, 2006 2:58 PM:
Posted on 10/16/2006 7:29 PM:

Well, you know, I like to set unrealistic goals for myself and then achieve them :) Good news is that I dont think Ill have much trouble finding enough things to talk about in this series. Stay tuned to find out whether I make it ...

Leave a Comment

(required) 
(optional)
(required) 
Enter the code you see below


About Mike Volodarsky

For the past 5 years, I was the core Program Manager for Microsoft ASP.NET 2.0 and IIS 7.0 products. I drove the design and development of the IIS 7.0 web server core, the IIS FastCGI support, the AppCmd command line tool, the ASP.NET Integrated pipeline, and other special projects around server security, performance, and scalability. Now, I am working on my own on cutting edge web server tech on top of the Microsoft IIS platform, and continue blogging about it here.

About me



For the past 5 years, I was the core server Program Manager for the IIS 7.0 and ASP.NET 2.0 products at Microsoft.
Now, I work on advanced web server tech using IIS 7.0, .NET, and Windows Server 2008 and write about it in this blog.

View Michael Volodarsky's profile on LinkedIn

Writings



TechNet Magazine
>Top 10 Performance Improvements in IIS 7.0

MSDN Magazine
>IIS 7.0: Build Web Server Solutions with End-To-End Extensibility
>IIS 7.0: Enhance Your Apps with the Integrated ASP.NET Pipeline
>IIS 7.0: Explore The Web Server For Windows Vista And Beyond
>Design and Deploy Secure Web Apps with ASP.NET 2.0 and IIS 6.0
>Fast, Scalable, and Secure Session State Management for Your Web Applications


Tools and Modules

LeechGuard
IconHandler 2.0
DirectoryListing
HttpRedirection
IIS Auth for Wordpress
iisschema.exe
PortCheck.exe v2.0

Popular Posts

- ASP.NET 2.0 Breaking Changes on IIS 7.0
- Develop IIS7 modules and handlers with .NET
- Troubleshoot IIS7 errors like a pro
- Troubleshooting 503 / "service unavailable" errors
- Troubleshooting "server not found" errors
- Create IIS7 sites, applications, and virtual directories
- Run Ruby on Rails with IIS FastCGI
- VS Debugging of ASP.NET applications on Windows Vista
- Stop hot-linking with IIS and ASP.NET

Tags

Search

Go

This Blog

Archives

Good IIS Blogs

Disclaimer

These postings are provided as is with no warranties, and confer no rights. The views expressed in this blog are entirely my own.

Syndication