CHttpModule::OnAsyncCompletion Method
Represents the method that will handle an asynchronous completion event, which occurs after an asynchronous operation has finished processing.
Syntax
virtual REQUEST_NOTIFICATION_STATUS OnAsyncCompletion(
IN IHttpContext* pHttpContext,
IN DWORD dwNotification,
IN BOOL fPostNotification,
IN OUT IHttpEventProvider* pProvider,
IN IHttpCompletionInfo* pCompletionInfo
);
Parameters
pHttpContext
[IN] A pointer to an IHttpContext interface.
dwNotification
[IN] A DWORD
value that contains the bitmask for the related notification.
fPostNotification
[IN] true
to indicate that the notification is for a post-event; otherwise, false
.
pProvider
[IN] A pointer to an IHttpEventProvider interface.
pCompletionInfo
[IN] A pointer to an IHttpCompletionInfo interface.
Return Value
A REQUEST_NOTIFICATION_STATUS value.
Remarks
Unlike many of the other CHttpModule methods that are called by registering for specific notifications, IIS will call a module's OnAsyncCompletion
method only when an asynchronous operation has finished. For example, when a request-level module calls the IHttpResponse::WriteEntityChunks method and specifies asynchronous completion, IIS will call the module's OnAsyncCompletion
method when the operation is complete.
When IIS calls the OnAsyncCompletion
method, it will specify the notification type in the dwNotification
parameter, and it will indicate whether the notification was for an event or post-event by using the fPostNotification
parameter. IIS also provides an IHttpCompletionInfo
interface that is pointed to by the pCompletionInfo
parameter. You can use this interface to retrieve additional information about the asynchronous completion.
Example
The following code example demonstrates how to create an HTTP module that performs the following tasks:
The module registers for the RQ_BEGIN_REQUEST and RQ_MAP_REQUEST_HANDLER notifications.
The module creates a
CHttpModule
class that contains OnBeginRequest, OnMapRequestHandler, andOnAsyncCompletion
methods.When a Web client requests a URL, IIS calls the module's
OnBeginRequest
method. This method performs the following tasks:Clears the existing response buffer and sets the MIME type for the response.
Creates an example string and returns that to the Web client asynchronously.
Tests for an error or asynchronous completion. If asynchronous completion is pending, the module returns a pending notification status to the integrated request-processing pipeline.
IIS calls the module's
OnMapRequestHandler
method. This method performs the following tasks:Flushes the current response buffer to the Web client.
Tests for an error or asynchronous completion. If asynchronous completion is pending, the module returns a pending notification status to the pipeline.
If asynchronous completion is required, IIS calls the module's
OnAsyncCompletion
method. This method performs the following tasks:Tests for a valid
IHttpCompletionInfo
interface. If a validIHttpCompletionInfo
interface was passed, the method calls the GetCompletionBytes and GetCompletionStatus methods, respectively, to retrieve the completed bytes and return the status for the asynchronous operation.Creates strings that contain the completion information and writes the information as an event to the application log of the Event Viewer.
The module removes the
CHttpModule
class from memory and then exits.
#define _WINSOCKAPI_
#include <windows.h>
#include <sal.h>
#include <httpserv.h>
#include <wchar.h>
// Create the module class.
class MyHttpModule : public CHttpModule
{
public:
REQUEST_NOTIFICATION_STATUS
OnBeginRequest(
IN IHttpContext * pHttpContext,
IN IHttpEventProvider * pProvider
)
{
UNREFERENCED_PARAMETER( pProvider );
// Create an HRESULT to receive return values from methods.
HRESULT hr;
// Buffer to store the byte count.
DWORD cbSent = 0;
// Buffer to store if asyncronous completion is pending.
BOOL fCompletionExpected = false;
// Create an example string to return to the Web client.
char szBuffer[] = "Hello World!";
// Clear the existing response.
pHttpContext->GetResponse()->Clear();
// Set the MIME type to plain text.
pHttpContext->GetResponse()->SetHeader(
HttpHeaderContentType,"text/plain",
(USHORT)strlen("text/plain"),TRUE);
// Create a data chunk.
HTTP_DATA_CHUNK dataChunk;
// Set the chunk to a chunk in memory.
dataChunk.DataChunkType = HttpDataChunkFromMemory;
// Set the chunk to the buffer.
dataChunk.FromMemory.pBuffer =
(PVOID) szBuffer;
// Set the chunk size to the buffer size.
dataChunk.FromMemory.BufferLength =
(USHORT) strlen(szBuffer);
// Insert the data chunk into the response.
hr = pHttpContext->GetResponse()->WriteEntityChunks(
&dataChunk,1,TRUE,TRUE,&cbSent,&fCompletionExpected);
// Test for a failure.
if (FAILED(hr))
{
// Set the HTTP status.
pHttpContext->GetResponse()->SetStatus(
500,"Server Error",0,hr);
// End additional processing.
return RQ_NOTIFICATION_FINISH_REQUEST;
}
// Test for pending asynchronous operations.
if (fCompletionExpected)
{
return RQ_NOTIFICATION_PENDING;
}
// Return processing to the pipeline.
return RQ_NOTIFICATION_CONTINUE;
}
REQUEST_NOTIFICATION_STATUS
OnMapRequestHandler(
IN IHttpContext * pHttpContext,
IN IMapHandlerProvider * pProvider
)
{
// Create an HRESULT to receive return values from methods.
HRESULT hr;
// Buffer to store the byte count.
DWORD cbSent = 0;
// Buffer to store if asyncronous completion is pending.
BOOL fCompletionExpected = false;
// Flush the response to the client.
hr = pHttpContext->GetResponse()->Flush(
TRUE,FALSE,&cbSent,&fCompletionExpected);
// Test for a failure.
if (FAILED(hr))
{
// Set the HTTP status.
pHttpContext->GetResponse()->SetStatus(
500,"Server Error",0,hr);
}
// Test for pending asynchronous operations.
if (fCompletionExpected)
{
return RQ_NOTIFICATION_PENDING;
}
// End additional processing.
return RQ_NOTIFICATION_CONTINUE;
}
REQUEST_NOTIFICATION_STATUS
OnAsyncCompletion(
IN IHttpContext * pHttpContext,
IN DWORD dwNotification,
IN BOOL fPostNotification,
IN IHttpEventProvider * pProvider,
IN IHttpCompletionInfo * pCompletionInfo
)
{
if ( NULL != pCompletionInfo )
{
// Create strings for completion information.
char szNotification[256] = "";
char szBytes[256] = "";
char szStatus[256] = "";
// Retrieve and format the completion information.
sprintf_s(szNotification,255,"Notification: %u",
dwNotification);
sprintf_s(szBytes,255,"Completion Bytes: %u",
pCompletionInfo->GetCompletionBytes());
sprintf_s(szStatus,255,"Completion Status: 0x%08x",
pCompletionInfo->GetCompletionStatus());
// Create an array of strings.
LPCSTR szBuffer[3] = {szNotification,szBytes,szStatus};
// Write the strings to the Event Viewer.
WriteEventViewerLog(szBuffer,3);
}
// Return processing to the pipeline.
return RQ_NOTIFICATION_CONTINUE;
}
MyHttpModule(void)
{
// Open a handle to the Event Viewer.
m_hEventLog = RegisterEventSource( NULL,"IISADMIN" );
}
~MyHttpModule(void)
{
// Test if the handle for the Event Viewer is open.
if (NULL != m_hEventLog)
{
// Close the handle to the Event Viewer.
DeregisterEventSource( m_hEventLog );
m_hEventLog = NULL;
}
}
private:
// Handle for the Event Viewer.
HANDLE m_hEventLog;
// Define a method that writes to the Event Viewer.
BOOL WriteEventViewerLog(LPCSTR * lpStrings, WORD wNumStrings)
{
// Test whether the handle for the Event Viewer is open.
if (NULL != m_hEventLog)
{
// Write any strings to the Event Viewer and return.
return ReportEvent(
m_hEventLog, EVENTLOG_INFORMATION_TYPE,
0, 0, NULL, wNumStrings, 0, lpStrings, NULL );
}
return FALSE;
}
};
// Create the module's class factory.
class MyHttpModuleFactory : public IHttpModuleFactory
{
public:
HRESULT
GetHttpModule(
OUT CHttpModule ** ppModule,
IN IModuleAllocator * pAllocator
)
{
UNREFERENCED_PARAMETER( pAllocator );
// Create a new instance.
MyHttpModule * pModule = new MyHttpModule;
// Test for an error.
if (!pModule)
{
// Return an error if we cannot create the instance.
return HRESULT_FROM_WIN32( ERROR_NOT_ENOUGH_MEMORY );
}
else
{
// Return a pointer to the module.
*ppModule = pModule;
pModule = NULL;
// Return a success status.
return S_OK;
}
}
void Terminate()
{
// Remove the class from memory.
delete this;
}
};
// Create the module's exported registration function.
HRESULT
__stdcall
RegisterModule(
DWORD dwServerVersion,
IHttpModuleRegistrationInfo * pModuleInfo,
IHttpServer * pGlobalInfo
)
{
UNREFERENCED_PARAMETER( dwServerVersion );
UNREFERENCED_PARAMETER( pGlobalInfo );
return pModuleInfo->SetRequestNotifications(
new MyHttpModuleFactory,
RQ_BEGIN_REQUEST | RQ_MAP_REQUEST_HANDLER,
0
);
}
Your module must export the RegisterModule function. You can export this function by creating a module definition (.def) file for your project, or you can compile the module by using the /EXPORT:RegisterModule
switch. For more information, see Walkthrough: Creating a Request-Level HTTP Module By Using Native Code.
You can optionally compile the code by using the __stdcall (/Gz)
calling convention instead of explicitly declaring the calling convention for each function.
Requirements
Type | Description |
---|---|
Client | - IIS 7.0 on Windows Vista - IIS 7.5 on Windows 7 - IIS 8.0 on Windows 8 - IIS 10.0 on Windows 10 |
Server | - IIS 7.0 on Windows Server 2008 - IIS 7.5 on Windows Server 2008 R2 - IIS 8.0 on Windows Server 2012 - IIS 8.5 on Windows Server 2012 R2 - IIS 10.0 on Windows Server 2016 |
Product | - IIS 7.0, IIS 7.5, IIS 8.0, IIS 8.5, IIS 10.0 - IIS Express 7.5, IIS Express 8.0, IIS Express 10.0 |
Header | Httpserv.h |