Bagikan melalui


Metode CHttpModule::OnAsyncCompletion

Mewakili metode yang akan menangani peristiwa penyelesaian asinkron, yang terjadi setelah operasi asinkron selesai diproses.

Sintaks

virtual REQUEST_NOTIFICATION_STATUS OnAsyncCompletion(  
   IN IHttpContext* pHttpContext,  
   IN DWORD dwNotification,  
   IN BOOL fPostNotification,  
   IN OUT IHttpEventProvider* pProvider,  
   IN IHttpCompletionInfo* pCompletionInfo  
);  

Parameter

pHttpContext
[IN] Penunjuk ke antarmuka IHttpContext .

dwNotification
[IN] DWORD Nilai yang berisi bitmask untuk pemberitahuan terkait.

fPostNotification
[IN] true untuk menunjukkan bahwa pemberitahuan adalah untuk pasca-peristiwa; jika tidak, false.

pProvider
[IN] Penunjuk ke antarmuka IHttpEventProvider .

pCompletionInfo
[IN] Penunjuk ke antarmuka IHttpCompletionInfo .

Tampilkan Nilai

Nilai REQUEST_NOTIFICATION_STATUS .

Keterangan

Tidak seperti banyak metode CHttpModule lain yang dipanggil dengan mendaftar untuk pemberitahuan tertentu, IIS akan memanggil metode modul OnAsyncCompletion hanya ketika operasi asinkron telah selesai. Misalnya, ketika modul tingkat permintaan memanggil metode IHttpResponse::WriteEntityChunks dan menentukan penyelesaian asinkron, IIS akan memanggil metode modul OnAsyncCompletion saat operasi selesai.

Ketika IIS memanggil metode , IIS akan menentukan jenis pemberitahuan dalam dwNotification parameter , dan itu akan menunjukkan apakah pemberitahuan itu untuk peristiwa atau pasca-peristiwa OnAsyncCompletion dengan menggunakan fPostNotification parameter . IIS juga menyediakan IHttpCompletionInfo antarmuka yang ditujukkan oleh pCompletionInfo parameter . Anda dapat menggunakan antarmuka ini untuk mengambil informasi tambahan tentang penyelesaian asinkron.

Contoh

Contoh kode berikut menunjukkan cara membuat modul HTTP yang melakukan tugas berikut:

  1. Modul ini mendaftar untuk pemberitahuan RQ_BEGIN_REQUEST dan RQ_MAP_REQUEST_HANDLER .

  2. Modul membuat CHttpModule kelas yang berisi OnBeginRequest, OnMapRequestHandler, dan OnAsyncCompletion metode.

  3. Saat klien Web meminta URL, IIS memanggil metode modul OnBeginRequest . Metode ini melakukan tugas-tugas berikut:

    1. Menghapus buffer respons yang ada dan mengatur jenis MIME untuk respons.

    2. Membuat contoh string dan mengembalikannya ke klien Web secara asinkron.

    3. Pengujian untuk kesalahan atau penyelesaian asinkron. Jika penyelesaian asinkron tertunda, modul mengembalikan status pemberitahuan yang tertunda ke alur pemrosesan permintaan terintegrasi.

  4. IIS memanggil metode modul OnMapRequestHandler . Metode ini melakukan tugas-tugas berikut:

    1. Menghapus buffer respons saat ini ke klien Web.

    2. Pengujian untuk kesalahan atau penyelesaian asinkron. Jika penyelesaian asinkron tertunda, modul mengembalikan status pemberitahuan yang tertunda ke alur.

  5. Jika penyelesaian asinkron diperlukan, IIS memanggil metode modul OnAsyncCompletion . Metode ini melakukan tugas-tugas berikut:

    1. Pengujian untuk antarmuka yang valid IHttpCompletionInfo . Jika antarmuka yang valid IHttpCompletionInfo diteruskan, metode memanggil metode GetCompletionBytes dan GetCompletionStatus , masing-masing, untuk mengambil byte yang telah selesai dan mengembalikan status untuk operasi asinkron.

    2. Membuat string yang berisi informasi penyelesaian dan menulis informasi sebagai peristiwa ke log aplikasi Penampil Peristiwa.

  6. Modul menghapus CHttpModule kelas dari memori lalu keluar.

#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
    );
}

Modul Anda harus mengekspor fungsi RegisterModule . Anda dapat mengekspor fungsi ini dengan membuat file definisi modul (.def) untuk proyek Anda, atau Anda dapat mengkompilasi modul dengan menggunakan sakelar /EXPORT:RegisterModule . Untuk informasi selengkapnya, lihat Panduan: Membuat Modul HTTP Request-Level Dengan Menggunakan Kode Asli.

Anda dapat secara opsional mengkompilasi kode dengan menggunakan __stdcall (/Gz) konvensi panggilan alih-alih secara eksplisit mendeklarasikan konvensi panggilan untuk setiap fungsi.

Persyaratan

Jenis Deskripsi
Klien - IIS 7.0 di Windows Vista
- IIS 7.5 di Windows 7
- IIS 8.0 di Windows 8
- IIS 10.0 pada Windows 10
Server - IIS 7.0 di Windows Server 2008
- IIS 7.5 di Windows Server 2008 R2
- IIS 8.0 di Windows Server 2012
- IIS 8.5 di Windows Server 2012 R2
- IIS 10.0 di Windows Server 2016
Produk - 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

Lihat juga

Kelas CHttpModule