Share via


IHttpModuleRegistrationInfo::SetPriorityForRequestNotification Method

 

Sets the request-level priority for a module.

Syntax

virtual HRESULT SetPriorityForRequestNotification(  
   IN DWORD dwRequestNotification,  
   IN PCWSTR pszPriority  
) = 0;  

Parameters

dwRequestNotification
[IN] A bitmask value that contains the request notifications to set for the priority level. (Defined in Httpserv.h.)

pszPriority
[IN] A pointer to a string that contains the priority alias. (Defined in Httpserv.h.)

Return Value

An HRESULT. Possible values include, but are not limited to, those in the following table.

Value Description
S_OK Indicates that the operation was successful.

Remarks

The SetPriorityForRequestNotification method sets the priority level for a list of request-level notifications for which an HTTP module has registered. IIS uses the priority level to determine the order within a notification that modules should be organized. The priority levels are inverted for CHttpModule::OnSendResponse notifications. For example, an HTTP module that has registered for the CHttpModule::OnBeginRequest notification by using the PRIORITY_ALIAS_HIGH alias would be prioritized before a module that has registered for the OnBeginRequest notification by using the PRIORITY_ALIAS_LOW alias. An HTTP module that has registered for the OnSendResponse notification by using the PRIORITY_ALIAS_HIGH alias would be run after a module that has registered for the OnSendResponse notification by using the PRIORITY_ALIAS_LOW alias.

Note

The bitmask values for request-level notifications and priority aliases are defined in the Httpserv.h file.

Note

The priority levels are inverted for RQ_SEND_RESPONSE notifications.

Example

The following example demonstrates how to create an HTTP module that uses the RegisterModule function and the following methods to register a module for global-level and request-level notifications.

The module responds to the registered notifications and writes entries to the application log in the Event Viewer.

Note

The entries in the Event Viewer will display "IISADMIN" as the event source.

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

// Create a global handle for the Event Viewer.
HANDLE g_hEventLog;

// Define the method that writes to the Event Viewer.
BOOL WriteEventViewerLog(LPCSTR szBuffer[], WORD wNumStrings);

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

        // Create an array of strings.
        LPCSTR szBuffer[2] = {"MyHttpModule","OnBeginRequest"};
        // Write the strings to the Event Viewer.
        WriteEventViewerLog(szBuffer,2);

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

// Create the module's global class.
class MyGlobalModule : public CGlobalModule
{
public:
    GLOBAL_NOTIFICATION_STATUS
    OnGlobalPreBeginRequest(
        IN IPreBeginRequestProvider * pProvider
    )
    {
        UNREFERENCED_PARAMETER( pProvider );
        
        // Create an array of strings.
        LPCSTR szBuffer[2] = {"MyGlobalModule","OnGlobalPreBeginRequest"};
        // Write the strings to the Event Viewer.
        WriteEventViewerLog(szBuffer,2);

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

    VOID Terminate()
    {
        // Remove the class from memory.
        delete this;
    }

    MyGlobalModule()
    {
        // Open a handle to the Event Viewer.
        g_hEventLog = RegisterEventSource( NULL,"IISADMIN" );
    }

    ~MyGlobalModule()
    {
        // Test whether the handle for the Event Viewer is open.
        if (NULL != g_hEventLog)
        {
            DeregisterEventSource( g_hEventLog );
            g_hEventLog = NULL;
        }
    }
};

// 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 the factory 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;
    }
};

// Define a method that writes to the Event Viewer.
BOOL WriteEventViewerLog(LPCSTR szBuffer[], WORD wNumStrings)
{
    // Test whether the handle for the Event Viewer is open.
    if (NULL != g_hEventLog)
    {
        // Write any strings to the Event Viewer and return.
        return ReportEvent(
            g_hEventLog,
            EVENTLOG_INFORMATION_TYPE,
            0, 0, NULL, wNumStrings,
            0, szBuffer, NULL );
    }
    return FALSE;
}

// Create the module's exported registration function.
HRESULT
__stdcall
RegisterModule(
    DWORD dwServerVersion,
    IHttpModuleRegistrationInfo * pModuleInfo,
    IHttpServer * pGlobalInfo
)
{
    UNREFERENCED_PARAMETER( dwServerVersion );
    UNREFERENCED_PARAMETER( pGlobalInfo );

    // Create an HRESULT to receive return values from methods.
    HRESULT hr;

    // Set the request notifications.
    hr = pModuleInfo->SetRequestNotifications(
        new MyHttpModuleFactory,
        RQ_BEGIN_REQUEST, 0 );

    // Test for an error and exit if necessary.
    if (FAILED(hr))
    {
        return hr;
    }

    // Set the request priority.
    hr = pModuleInfo->SetPriorityForRequestNotification(
        RQ_BEGIN_REQUEST,PRIORITY_ALIAS_MEDIUM);

    // Test for an error and exit if necessary.
    if (FAILED(hr))
    {
        return hr;
    }

    // Create an instance of the global module class.
    MyGlobalModule * pGlobalModule = new MyGlobalModule;
 
    // Test for an error.
    if (NULL == pGlobalModule)
    {
        return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
    }
 
    // Set the global notifications.
    hr = pModuleInfo->SetGlobalNotifications(
        pGlobalModule, GL_PRE_BEGIN_REQUEST );

    // Test for an error and exit if necessary.
    if (FAILED(hr))
    {
        return hr;
    }

    // Set the global priority.
    hr = pModuleInfo->SetPriorityForGlobalNotification(
        GL_PRE_BEGIN_REQUEST,PRIORITY_ALIAS_LOW);

    // Test for an error and exit if necessary.
    if (FAILED(hr))
    {
        return hr;
    }

    // Return a success status;
    return S_OK;
}

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 Technical Preview
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

See Also

IHttpModuleRegistrationInfo Interface
IHttpModuleRegistrationInfo::SetGlobalNotifications Method
IHttpModuleRegistrationInfo::SetPriorityForGlobalNotification Method
IHttpModuleRegistrationInfo::SetRequestNotifications Method
PFN_REGISTERMODULE Function