ISAPI on Microsoft IIS » History » Version 1
Wim Dumon, 08/05/2010 08:31 PM
1 | 1 | Wim Dumon | h1. ISAPI on Microsoft IIS |
---|---|---|---|
2 | |||
3 | This page is work in progress. The ISAPI interface is not yet fully released - it will be in the upcoming Wt 3.1.4. |
||
4 | |||
5 | From Wt 3.1.4 onward, Wt is delivered with an ISAPI connector for easy integration with Microsoft IIS. ISAPI was preferred above FCGI on Windows because existing FCGI implementations (client libraries and servers) turned out to result in suboptimal solutions. Note that Wt does not support FCGI on Windows. |
||
6 | |||
7 | Wt integrates beautifully with IIS's asynchronous architecture, and even specialized features such as server push, the internal path API, ... work perfectly scalable with IIS. The Wt ISAPI connector is fully asynchronous, and therefore causes no scalability problems for IIS's internal architecture. |
||
8 | |||
9 | h2. Using the connector |
||
10 | |||
11 | h3. Building the application - General |
||
12 | |||
13 | To use the connector, the following steps are required: |
||
14 | * Configure to build a DLL |
||
15 | * Link to @wtisapi.lib@, in addition to @wt.lib@ and other Wt libraries such as dbo. |
||
16 | * Export the ISAPI symbols (HttpExtensionProc, GetExtensionVersion, TerminateExtension) through a .def file or linker options |
||
17 | * Write a @main()@ function just like you would in a normal Wt application. The connector will invoke @main()@ and @argv[0]@ contains the path of the DLL. |
||
18 | * Optionally, write a @DllMain()@ function |
||
19 | |||
20 | The source code needs no changes to use the ISAPI connector. |
||
21 | |||
22 | h3. Using CMake to build your application |
||
23 | |||
24 | We suggest the use of the following CMake function to quickly switch between the httpd and the isapi connector. |
||
25 | <pre> |
||
26 | FUNCTION(WT_ADD_APPLICATION name) |
||
27 | IF(${WT_CONNECTOR} STREQUAL "wtisapi") |
||
28 | LIST(INSERT ARGV 1 "SHARED") |
||
29 | ADD_LIBRARY(${ARGV}) |
||
30 | SET_TARGET_PROPERTIES(${name} |
||
31 | PROPERTIES |
||
32 | LINK_FLAGS |
||
33 | "/EXPORT:HttpExtensionProc /EXPORT:GetExtensionVersion /EXPORT:TerminateExtension" |
||
34 | ) |
||
35 | ELSE(${WT_CONNECTOR} STREQUAL "wtisapi") |
||
36 | ADD_EXECUTABLE(${ARGV}) |
||
37 | ENDIF(${WT_CONNECTOR} STREQUAL "wtisapi") |
||
38 | TARGET_LINK_LIBRARIES(${name} wt ${WT_CONNECTOR}) |
||
39 | ENDFUNCTION(WT_ADD_APPLICATION) |
||
40 | </pre> |
||
41 | |||
42 | Wt applications can then be defined as follows: |
||
43 | <pre> |
||
44 | SET(WT_CONNECTOR |
||
45 | # "wthttp" # uncomment if you want to use the standalone connector instead |
||
46 | "wtisapi" |
||
47 | ) |
||
48 | WT_ADD_APPLICATION(MyApp src1.C src2.C src3.C) |
||
49 | </pre> |
||
50 | |||
51 | h3. Debugging your WtIsapi program |
||
52 | |||
53 | Generally, it is recommended to debug your Wt application using the http connector. This is very natural (just run it in MSVC) and easy to interact with. Your development cycle will be shorter than when you take IIS in the picture. Remember, it is easy to switch between the http and isapi connector: just link to a different library. |
||
54 | |||
55 | For some problems, you may need to debug your application in the same way as it is deployed in IIS. A google search will yield many options to debug an ISAPI application. In essence, the easiest way to debug an ISAPI extension is to attach your debugger to a running process (MSVC: Debug->Attach to process. Note that this feature is available in MSVC Express Editions version 2005 and 2008, but was removed from the 2010 version). |
||
56 | |||
57 | The big question is of course: what process should I attach to? This depends on your IIS version and deployment configuration. You can look this up in the manual of your IIS, and still gamble between processes with identical names, or you could let your application tell you what the correct process ID is. I particularly liked the following code snippet for this purpose: |
||
58 | <pre> |
||
59 | std::stringstream ss; |
||
60 | ss << "Please attach a debugger to the process " << GetCurrentProcessId() << " and click OK" << std::endl; |
||
61 | char title[] = "Wt/ISAPI Debug Time!"; |
||
62 | MessageBox(NULL, ss.str().c_str(), "Wt/ISAPI Debug Time!", MB_OK|MB_SERVICE_NOTIFICATION); |
||
63 | </pre> |
||
64 | You can add this to your @main()@ function, but if it is added to @GetExtensionVersion()@ in @src/isapi/Isapi.C@ (you'll have to recompile Wt then), this even waits for you to connect at the moment that your DLL is first loaded. |
||
65 | |||
66 | On Vista and Windows 7, this will send the message to the event log rather than a dialog on the screen. You may have more success using @WTSSendMessage()@ on those systems. |
||
67 | |||
68 | h2. Architecture overview |
||
69 | |||
70 | IIS expects a DLL to run the web application, whereas Wt's other connectors (httpd, fcgi) result in an executable. This means that there are a few important points to take into account when using this connector (e.g. build a DLL instead of an EXE). On the other hand, we made sure that the connectors can still be interchanged, meaning that you're not required to make any changes to your Wt application in order to use the new connector. |
||
71 | |||
72 | In Wt's architecture, the connector is a library that is separate from the rest of the system. The same is true for ISAPI: the connector library is @wtisapi.lib@. This is a static library which provides the entry functions for ISAPI (@GetExtensionVersion()@, @TerminateExtension()@, @HttpExtensionProc()@). IIS will search for these functions in your DLL so you must make sure that your DLL exports them. That is also one of the reasons why wtisapi is only available as a static library: the symbols must be embedded in your DLL. |
||
73 | |||
74 | Upon initialization, IIS calls @GetExtensionVersion()@. The connector starts a thread, which invokes the @main()@ function of your Wt application. During this invocation, argc will be 1 and argv[0] contains the path of the ISAPI DLL. This thread keeps running until the server is terminated and main() returns. |
||
75 | When IIS is shut down, IIS calls @TerminateExtention()@ and the Wt server will be terminated. This means that all applications will be properly destroyed when IIS shuts down in a normal way. |
||
76 | |||
77 | The Wt ISAPI application runs inside a server process. Whereas it is unimportant whether the extension runs inside the servers process or in a separate process, it is important to ensure that all HTTP requests for a single application will be served by the same process. It is therefore mandatory to properly configure IIS6/7 to avoid the use of a so-called Web Garden for the Wt application. If you do use a Web Garden, requests will end up in processes that did not initiate the session, causing failing (restarting) web applications. This is not a problem, as a stable Wt application has no benefit from a web garden. If you want a Wt application to benefit from multi-core servers, modify the num-threads value in wt_config.xml. |
||
78 | |||
79 | IIS does not foresee a method to unload an ISAPI extension: once loaded, it is loaded until the server shuts down. |
||
80 | |||
81 | h2. Configuring Microsoft Internet Information Server |
||
82 | |||
83 | h3. IIS 7.5 (Windows 7) |
||
84 | |||
85 | h3. IIS 7 (Vista) |
||
86 | |||
87 | h3. IIS 5.1 (Windows XP) |
||
88 | |||
89 | h2. Compatibility with other web servers |
||
90 | |||
91 | The connector was not designed to work with other web servers. @wtisapi.lib@ will not work with Apache due to how Apache handles processes and threads. |