Freigeben über


IHttpModuleFactory::Terminate-Methode

Beendet eine IHttpModuleFactory-Schnittstelle .

Syntax

virtual VOID Terminate(  
   VOID  
) = 0;  

Parameter

Diese Methode nimmt zwei Parameter entgegen.

Rückgabewert

VOID.

Bemerkungen

Wenn Sie ein HTTP-Modul entwerfen, muss Ihre IHttpModuleFactory Schnittstelle eine Terminate Methode bereitstellen. Ihr Modul verwendet diese Methode, um alle Bereinigungsaufgaben auszuführen, bevor es beendet wird. Beispielsweise sollte Ihre Terminate Methode zumindest Ihre IHttpModuleFactory Schnittstelle aus dem Arbeitsspeicher entfernen. In einer aufwendigeren Implementierung verwendet Ihr Modul möglicherweise die Referenzzählung, bevor eine IHttpModuleFactory Schnittstelle aus dem Arbeitsspeicher entfernt wird. Dies ist jedoch nicht erforderlich.

Beispiel

Im folgenden Codebeispiel wird veranschaulicht, wie ein einfaches HTTP-Modul "Hallo Welt" erstellt wird. Das Modul definiert eine exportierte RegisterModule-Funktion, die eine instance einer IHttpModuleFactory Schnittstelle an die IHttpModuleRegistrationInfo::SetRequestNotifications-Methode übergibt und sich für die RQ_BEGIN_REQUEST-Benachrichtigung registriert. IIS verwendet die IHttpModuleFactory::GetHttpModule-Methode, um eine instance einer CHttpModule-Klasse zu erstellen und eine erfolgreiche status zurück. IIS verwendet auch die Terminate Methode der IHttpModuleFactory Schnittstelle, um die Factory aus dem Arbeitsspeicher zu entfernen.

Wenn eine RQ_BEGIN_REQUEST Benachrichtigung ausgelöst wird, ruft IIS die CHttpModule::OnBeginRequest-Methode des Moduls auf, um die aktuelle Anforderung zu verarbeiten. OnBeginRequest löscht den Antwortpuffer und ändert den MIME-Typ für die Antwort. Die -Methode erstellt dann einen Datenblock, der eine "Hallo Welt"-Zeichenfolge enthält, und gibt die Zeichenfolge an einen Webclient zurück. Schließlich gibt das Modul einen status Indikator zurück, der IIS benachrichtigt, dass alle Benachrichtigungen abgeschlossen sind und dann beendet wird.

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

// Create the module class.
class CHelloWorld : 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;
        
        // Retrieve a pointer to the response.
        IHttpResponse * pHttpResponse = pHttpContext->GetResponse();

        // Test for an error.
        if (pHttpResponse != NULL)
        {
            // Clear the existing response.
            pHttpResponse->Clear();
            // Set the MIME type to plain text.
            pHttpResponse->SetHeader(
                HttpHeaderContentType,"text/plain",
                (USHORT)strlen("text/plain"),TRUE);

            // Create a string with the response.
            PCSTR pszBuffer = "Hello World!";
            // Create a data chunk.
            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 = pHttpResponse->WriteEntityChunks(
                &dataChunk,1,FALSE,TRUE,&cbSent);

            // Test for an error.
            if (FAILED(hr))
            {
                // Set the HTTP status.
                pHttpResponse->SetStatus(500,"Server Error",0,hr);
            }

            // End additional processing.
            return RQ_NOTIFICATION_FINISH_REQUEST;
        }

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

// Create the module's class factory.
class CHelloWorldFactory : public IHttpModuleFactory
{
public:
    HRESULT
    GetHttpModule(
        OUT CHttpModule ** ppModule, 
        IN IModuleAllocator * pAllocator
    )
    {
        UNREFERENCED_PARAMETER( pAllocator );

        // Create a new instance.
        CHelloWorld * pModule = new CHelloWorld;

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

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

    // Set the request notifications and exit.
    return pModuleInfo->SetRequestNotifications(
        new CHelloWorldFactory,
        RQ_BEGIN_REQUEST,
        0
    );
}

Ihr Modul muss die RegisterModule Funktion exportieren. Sie können diese Funktion exportieren, indem Sie eine Moduldefinitionsdatei (.def) für Ihr Projekt erstellen, oder Sie können das Modul mithilfe des /EXPORT:RegisterModule Switches kompilieren. Weitere Informationen finden Sie unter Exemplarische Vorgehensweise: Erstellen eines Request-Level HTTP-Moduls mithilfe von nativem Code.

Sie können den Code optional kompilieren, indem Sie die __stdcall (/Gz) aufrufende Konvention verwenden, anstatt die aufrufende Konvention für jede Funktion explizit zu deklarieren.

Anforderungen

type BESCHREIBUNG
Client – IIS 7.0 unter Windows Vista
– IIS 7.5 unter Windows 7
– IIS 8.0 unter Windows 8
– IIS 10.0 auf Windows 10
Server – IIS 7.0 unter Windows Server 2008
– IIS 7.5 unter Windows Server 2008 R2
– IIS 8.0 unter Windows Server 2012
– IIS 8.5 unter Windows Server 2012 R2
– IIS 10.0 auf Windows Server 2016
Produkt – 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

Weitere Informationen

CHttpModule-Klasse
IHttpModuleFactory-Schnittstelle