IHttpContext::GetNextNotification 方法

检索当前模块主机的下一个通知。

语法

virtual BOOL GetNextNotification(  
   IN REQUEST_NOTIFICATION_STATUS status,  
   OUT DWORD* pdwNotification,  
   OUT BOOL* pfIsPostNotification,  
   OUT CHttpModule** ppModuleInfo,  
   OUT IHttpEventProvider** ppRequestOutput  
) = 0;  

parameters

status
[IN]要从当前通知返回 的REQUEST_NOTIFICATION_STATUS 枚举值。

pdwNotification
[OUT]指向 的指针 DWORD ,其中包含下一个通知的位掩码值。

pfIsPostNotification
[OUT]指向布尔值的指针。 true 指示通知为后通知;否则为 false

ppModuleInfo
[OUT]指向负责处理返回的通知的 CHttpModule 类的地址的指针。

ppRequestOutput
[OUT]指向返回的通知的 IHttpEventProvider 接口地址的指针。

返回值

true 如果对 GetNextNotification 的调用成功,则为 ;否则为 false

备注

HTTP 模块可以使用 GetNextNotification 方法在同一模块主机中合并通知。 将处理返回到集成的请求处理管道需要少量的开销。 若要绕过该开销,HTTP 模块可以调用 GetNextNotification 方法来跳到下一个通知并继续处理,前提是这两个通知位于同一模块主机中,并且没有注册其他通知处理程序来处理通知之间的请求。

例如,HTTP 模块可能包含 OnResolveRequestCache 方法,同一模块主机中的另一个 HTTP 模块可能包含 OnPostResolveRequestCache 方法。 第一个模块可以调用 GetNextNotification 方法来继续处理,而不是将处理返回到管道,就像模块已注册 OnPostResolveRequestCache 事件后通知方法一样。

注意

如果对 GetNextNotification 的调用返回 false,则可以启用失败的请求跟踪规则,以便检查 IIS 正在处理的通知。

示例

下面的代码示例演示如何创建执行以下任务的 HTTP 模块:

  1. 注册多个通知。

  2. 创建一个调用 方法的 GetNextNotification 帮助程序方法,该方法尝试跳到下一个通知。

  3. 对于每个已注册的通知,定义调用帮助程序方法的通知处理程序,然后向客户端显示返回状态。

#define _WINSOCKAPI_
#include <windows.h>
#include <sal.h>
#include <httpserv.h>

// Create the module class.
class MyHttpModule : public CHttpModule
{
public:
    REQUEST_NOTIFICATION_STATUS
    OnBeginRequest(
        IN IHttpContext * pHttpContext,
        IN IHttpEventProvider * pProvider
    )
    {
        UNREFERENCED_PARAMETER( pProvider );

        // 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);

        // Return processing to the pipeline.
        return RQ_NOTIFICATION_CONTINUE;
    }

    REQUEST_NOTIFICATION_STATUS
    OnAuthenticateRequest(
        IN IHttpContext * pHttpContext,
        IN IAuthenticationProvider * pProvider
    )
    {
        UNREFERENCED_PARAMETER( pProvider );
        // Attempt to retrieve the next notification and display the result.
        GetNotificationAndDisplayResult(
            pHttpContext,"OnAuthenticateRequest\n");
        // Return processing to the pipeline.
        return RQ_NOTIFICATION_CONTINUE;
    }

    REQUEST_NOTIFICATION_STATUS
    OnPostAuthenticateRequest(
        IN IHttpContext * pHttpContext,
        IN IHttpEventProvider * pProvider
    )
    {
        UNREFERENCED_PARAMETER( pProvider );
        // Attempt to retrieve the next notification and display the result.
        GetNotificationAndDisplayResult(
            pHttpContext,"\nOnPostAuthenticateRequest\n");
        // Return processing to the pipeline.
        return RQ_NOTIFICATION_CONTINUE;
    }

    REQUEST_NOTIFICATION_STATUS
    OnAuthorizeRequest(
        IN IHttpContext * pHttpContext,
        IN IHttpEventProvider * pProvider
    )
    {
        UNREFERENCED_PARAMETER( pProvider );
        // Attempt to retrieve the next notification and display the result.
        GetNotificationAndDisplayResult(
            pHttpContext,"\nOnAuthorizeRequest\n");
        // Return processing to the pipeline.
        return RQ_NOTIFICATION_CONTINUE;
    }

    REQUEST_NOTIFICATION_STATUS
    OnPostAuthorizeRequest(
        IN IHttpContext * pHttpContext,
        IN IHttpEventProvider * pProvider
    )
    {
        UNREFERENCED_PARAMETER( pProvider );
        // Attempt to retrieve the next notification and display the result.
        GetNotificationAndDisplayResult(
            pHttpContext,"\nOnPostAuthorizeRequest\n");
        // Return processing to the pipeline.
        return RQ_NOTIFICATION_CONTINUE;
    }

    REQUEST_NOTIFICATION_STATUS
    OnMapRequestHandler(
        IN IHttpContext * pHttpContext,
        IN IMapHandlerProvider * pProvider
    )
    {
        UNREFERENCED_PARAMETER( pHttpContext );
        UNREFERENCED_PARAMETER( pProvider );
        // End additional processing.        
        return RQ_NOTIFICATION_FINISH_REQUEST;
    }

private:

    // Create a helper method that attempts to retrieve the next
    // notification and returns the status to a Web client.
    void GetNotificationAndDisplayResult(
        IHttpContext * pHttpContext,
        PCSTR pszBuffer
    )
    {
        DWORD dwNotification = 0;
        BOOL fPostNotification = FALSE;
        CHttpModule * pHttpModule = NULL;
        IHttpEventProvider * pEventProvider = NULL;
        char szBuffer[256]="";

        // Attempt to retrive the next notification.
        BOOL fReturn = pHttpContext->GetNextNotification(
            RQ_NOTIFICATION_CONTINUE,
            &dwNotification,&fPostNotification,
            &pHttpModule,&pEventProvider);

        // Return the name of the notification to a Web client.
        WriteResponseMessage(pHttpContext,pszBuffer);

        // Return the status of the GetNextNotification method to a Web client.
        sprintf_s(szBuffer,255,"\tGetNextNotification return value: %s\n",
            fReturn==TRUE?"true":"false");
        WriteResponseMessage(pHttpContext,szBuffer);

        // Return the notification bitmask to a Web client.
        sprintf_s(szBuffer,255,"\tNotification: %08x\n",dwNotification);
        WriteResponseMessage(pHttpContext,szBuffer);

        // Return whether the notification is a post-notification.
        sprintf_s(szBuffer,255,"\tPost-notification: %s\n",
            fPostNotification==TRUE?"Yes":"No");
        WriteResponseMessage(pHttpContext,szBuffer);
    }

    // Create a utility method that inserts a string value into the response.
    HRESULT WriteResponseMessage(
        IHttpContext * pHttpContext,
        PCSTR pszBuffer
    )
    {
        // Create an HRESULT to receive return values from methods.
        HRESULT hr;
        
        // Create a data chunk. (Defined in the Http.h file.)
        HTTP_DATA_CHUNK dataChunk;
        // Set the chunk to a chunk in memory.
        dataChunk.DataChunkType = HttpDataChunkFromMemory;
        // Buffer for bytes written of data chunk.
        DWORD cbSent;

        // Set the chunk to the buffer.
        dataChunk.FromMemory.pBuffer =
            (PVOID) pszBuffer;
        // Set the chunk size to the buffer size.
        dataChunk.FromMemory.BufferLength =
            (USHORT) strlen(pszBuffer);
        // Insert the data chunk into the response.
        hr = pHttpContext->GetResponse()->WriteEntityChunks(
            &dataChunk,1,FALSE,TRUE,&cbSent);
        // Test for an error.
        if (FAILED(hr))
        {
            // Return the error status.
            return hr;
        }

        // Return a success status.
        return S_OK;
    }
};

// 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_AUTHENTICATE_REQUEST | 
        RQ_AUTHORIZE_REQUEST | RQ_MAP_REQUEST_HANDLER,
        RQ_AUTHENTICATE_REQUEST | RQ_AUTHORIZE_REQUEST
    );
}

模块必须导出 RegisterModule 函数。 可以通过为项目创建模块定义 (.def) 文件导出此函数,也可以使用 开关编译模块 /EXPORT:RegisterModule 。 有关详细信息,请参阅 演练:使用本机代码创建Request-Level HTTP 模块

可以选择使用调用约定编译代码, __stdcall (/Gz) 而不是为每个函数显式声明调用约定。

要求

类型 说明
客户端 - Windows Vista 上的 IIS 7.0
- Windows 7 上的 IIS 7.5
- Windows 8 上的 IIS 8.0
- Windows 10 上的 IIS 10.0
服务器 - Windows Server 2008 上的 IIS 7.0
- Windows Server 2008 R2 上的 IIS 7.5
- Windows Server 2012 上的 IIS 8.0
- Windows Server 2012 R2 上的 IIS 8.5
- Windows Server 2016 上的 IIS 10.0
产品 - 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

另请参阅

IHttpContext 接口