To extend the server, IIS7 provides a new (C++) native core server API, which replaces
ISAPI filter and extension API from previous IIS releases. The new API features
object-oriented development with an intuitive object model, provides more control
over request processing, and uses simpler design patterns to help you write robust
code. Please visit Developing a Native Module for IIS7 for more information about this sample.
NOTE: The IIS7 native (C++) server API is declared in the Platform
SDK httpserv.h header file. You must obtain this SDK
and register it with Visual Studio in order to compile this module.
A native module is a Windows DLL that contains an the following:
In order to compile
the module, you will need to install the Windows Platform SDK that contains the
required IIS header files.
After installing the SDK, you will need to register
the SDK with Visual Studio 2005. You
can do this via Start > Programs > Microsoft
Windows SDK > Visual Studio Registration > Register Windows SDK Directories
with Visual Studio.
A native module must export the RegisterModule function. This function will
be invoked by the server when the module DLL is loaded, so that your DLL can register
for the required server events, and provide a mechanism for the server to create
instances of your module class.
__stdcall
HRESULT RegisterModule( DWORD dwServerVersion, IHttpModuleRegistrationInfo
* pModuleInfo, IHttpServer * pHttpServer );
This function is implemented for you in the main.cpp source file.
It does the following:
The server requires a module factory class in order to obtain instances of your
module. The factory class must implement the IHttpModuleFactory interface
declared in the httpserv.h Windows SDK header file.
This factory is implemented for you in the mymodulefactory.h header
file.
The GetHttpModule method of the factory class is called at the beginning of
each request in order to obtain an instance of your module. The typical implementation
simply returns a new instance of your module class:
class CMyHttpModuleFactory : public IHttpModuleFactory
{
public:
virtual HRESULT GetHttpModule( OUT CHttpModule **ppModule, IN IModuleAllocator
* )
{
...
pModule = new CMyHttpModule();
...
*ppModule = pModule;
...
}
virtual void Terminate() { }
}
The Terminate method of the factory class is called during the shutdown of the worker process, before your module DLL is unloaded. It typically releases any resources you may have initialized inside the RegisterModule function.
The module class inherits from the CHttpModule base class, which
defines an event handler method for each of the server events discussed earlier.
When the server executes each event during the processing of a request, it will
invoke the associated event handler method on each of the module instances that
have registered for that event.
This class is declared in the mymodule.h header file, and its event
handler methods are implemented in the mymodule.cpp source file.
In order to provide processing for each server event, the module class MUST override
the corresponding method. The signature of each event handler method is the
following:
REQUEST_NOTIFICATION_STATUS
On<EVENT NAME>(
IN IHttpContext * pHttpContext,
IN OUT IHttpEventProvider * pProvider
);
Where <EVENT NAME> is one of the server events we listed earlier. For
example, the module in this project initially overrides only the OnAcquireRequestState method since we register only for the RQ_ACQUIRE_REQUEST_STATE event:
class CMyHttpModule : public
CHttpModule
{
public:
// Implementation of the AcquireRequestState
event handler method.
REQUEST_NOTIFICATION_STATUS OnAcquireRequestState( IN IHttpContext
* pHttpContext, IN OUT IHttpEventProvider * pProvider );
// TODO: override additional event handler
methods below.
};
The implementation of this method provides the module functionality desired for
processing the request in the appropriate pipeline stage. This project provides
a dummy implementation in the mymodule.cpp source file.
After you have compiled your module, you need to deploy it on the server.
You can do that by compiling the module, and then copying IIS7NativeModule.dll
(and the IIS7NativeModule.pdb debugging symbols file if desired)
to any location on the machine running IIS7.
You can install your module on the server by running the following command from an Elevated command line prompt:
%systemroot%\system32\inetsrv\APPCMD.EXE install module /name:MyModule /image:<FULL
PATH TO DLL>
Where <FULL_PATH_TO_DLL> is the full path to the module DLL
file.
You can also install your module using the IIS7 Administration Tool. After installation,
your module will be loaded and enabled in all application pools on the server.
To learn ore about installing modules, and selecting modules to execute on your
server and for specific applications, please visit www.iis.net.
You have succesfully built and deployed your own IIS7 module. To learn more about building IIS7 modules, and to download sample modules, be sure to visit www.iis.net.