Another reason you may want to create a native module is if you want to convert your ISAPI components into new native code modules.. A native code module contains the following: ■ The Re
Trang 1performance; native code runs faster than managed code Although native and managed code strings have access to the same request pipeline, native code directly accesses it Another reason you may want to create a native module is if you want to convert your ISAPI components into new native code modules
A native code module contains the following:
■ The RegisterModule function, which is responsible for creating a module factory and registering the module for server events
■ The implementation of the module class inheriting from the CHttpModule base class, which provides the main functionality for your created module
■ The implementation of the module factory class that implements the IHttpModuleFactory interface It is responsible for creating instances of your newly created module
The modules used in this example can be downloaded from Microsoft’s IIS.net Web site at
www.iis.net/downloads/default.aspx?tabid=34&g=6&i=1301 This module will be deployed as a global module in our demonstration, although any native module can be deployed at the application level just as managed modules are
In developing a native module, the developer must implement the RegisterModule function that is started by the server when the module is loaded In short, there are three tasks that are accomplished when implementing the RegisterModule function:
■ Saving the Global State This is done by saving the global server instance for future use
■ Creating the Module Factory The module factory is responsible for creating instances
of the native module for each request
■ Registering for Server Events This registers the module factory for the desired request processing events
The implementing of the RegisterModule and the three tasks that are required to do so are shown in Figure 14.2 through Visual Studio 2005
Trang 2The Extensible Core Server • Chapter 14 753 Figure 14.2 Implementing RegisterModule
Registering a module factory is done through SetRequestNotifi cations This tells the server to
create the native module instance for each request using the module factory previously created, and
to use the appropriate event handlers for each of the request processing stages Once the developer
has fi nished implementing the RegisterModule, he or she must export it to the server using a module defi nition fi le (.def ) Afterward, the developer implements the module factory class The module
factory class implements the IHttpModuleFactory interface, which serves to create instances of the
module on each request Figure 14.3 shows the code for the module factory class
Trang 3Figure 14.3 Module Factory Class
Once this code is complete, the developer implements the module class, which is responsible for the main functionality of the module during any server events The code for the main module is shown in Figure 14.4 Once complete, the developer can compile the module Managed code, however, does not have to be compiled (we discuss managed code later in this chapter)
Trang 4The Extensible Core Server • Chapter 14 755 Figure 14.4 Main Native Module
Adding Native Modules to IIS 7.0
In the previous section, we walked through what developers do to create a native module In this
section, we walk though what, as administrators, you will need to do to add these native modules to IIS 7.0 There are three ways to install a native module in IIS 7.0:
1 APPCMD.exe
2 IIS Manager
3 Manual Installation
Before adding the native module, you have to copy its dll to the IIS server There is no required location for the newly developed dll, which in this case is called IIS7NativeModule.dll Figure 14.5
shows that the new native module has been copied to the C:\ Native directory, which was created on
the IIS box Now you need to deploy the module First we will walk through using AppCmd, then
IIS Manager, and fi nally manual deployment
Trang 5Using APPCMD.exe To Deploy
Native Modules
As was introduced in Chapter 12, APPCMD.exe provides administrators with a new powerful
command-line tool capable of managing IIS 7.0 To deploy a native module using APPCMD.exe, follow these steps:
1 Open a command prompt and go to the %systemroot%\ system32\inetsrv directory.
2 As shown in Figure 14.6, type the command appcmd.exe install module /name:
MyModule /image:c:\Native\iis7nativemodule.dll Once the command has been executed, you should see a screen similar to that of Figure 14.7
Figure 14.5 Location of the Native Module