Bagikan melalui


Metode IHttpContext::ReleaseClonedContext

Merilis instans IHttpContext yang dikloning.

Sintaks

virtual HRESULT ReleaseClonedContext(  
   VOID  
) = 0;  

Parameter

Metode ini tidak memerlukan parameter.

Tampilkan Nilai

Sebuah HRESULT. Nilai yang mungkin termasuk, tetapi tidak terbatas pada, yang ada dalam tabel berikut.

Nilai Deskripsi
S_OK Menunjukkan bahwa operasi berhasil.
ERROR_INVALID_PARAMETER Menunjukkan bahwa konteks induk untuk permintaan saat ini tidak valid (misalnya, konteks anak dirilis setelah induk dirilis).

Keterangan

Metode ini ReleaseClonedContext merilis instans IHttpContext antarmuka. Misalnya, jika Anda membuat konteks anak dengan menggunakan metode IHttpContext::CloneContext , Anda akan memanggil metode anak ReleaseClonedContext untuk merilis konteks anak ketika Anda selesai menggunakannya.

Penting

Memanggil ReleaseClonedContext metode untuk merilis permintaan anak setelah merilis konteks induknya akan mengembalikan ERROR_INVALID_PARAMETER. Misalnya, jika Anda membuat konteks anak lalu menggunakan anak tersebut untuk membuat permintaan anak berlapis, Anda harus merilis konteks anak dalam urutan terbalik pembuatannya.

Contoh

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

  1. Modul mendaftar untuk pemberitahuan RQ_MAP_PATH .

  2. Modul membuat kelas CHttpModule yang berisi metode OnMapPath dan OnAsyncCompletion .

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

    1. Pengujian untuk melihat apakah URL untuk permintaan saat ini memiliki garis miring berikutnya atau diakhir dengan /default.aspx. Jika URL berakhir dengan salah satu elemen, modul menggunakan IHttpContext::CloneContext metode untuk membuat kloning permintaan saat ini.

    2. Memanggil metode kloning IHttpRequest::SetUrl untuk mengatur URL untuk kloning ke /example/default.aspx.

    3. Memanggil metode IHttpContext::ExecuteRequest untuk menjalankan permintaan anak.

    4. Pengujian untuk penyelesaian asinkron. Jika penyelesaian asinkron tertunda, modul mengembalikan pemrosesan ke alur pemrosesan permintaan terintegrasi. Jika tidak, modul merilis konteks kloning.

  4. Jika penyelesaian asinkron diperlukan, IIS memanggil metode modul OnAsyncCompletion . Metode ini merilis konteks kloning.

  5. Modul menghapus CHttpModule kelas dari memori lalu keluar.

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

// Create the module class.
class MyHttpModule : public CHttpModule
{

private:

    // Create a pointer for a child request.
    IHttpContext * m_pChildRequestContext;

public:

    MyHttpModule(void)
    {
        m_pChildRequestContext = NULL;
    }

    REQUEST_NOTIFICATION_STATUS
    OnMapPath(
        IN IHttpContext * pHttpContext,
        IN IMapPathProvider * pProvider
    )
    {
        UNREFERENCED_PARAMETER( pProvider );

        HRESULT hr;
        BOOL fCompletionExpected;

        // Retrieve a pointer to the URL.
        PCWSTR pwszUrl = pProvider->GetUrl();

        // Only process requests for the root.
        if (0 == wcscmp(pwszUrl,L"/") || 0 == wcscmp(pwszUrl,L"/default.aspx"))
        {            
            // Clone the current context.
            hr = pHttpContext->CloneContext(
                CLONE_FLAG_BASICS, &m_pChildRequestContext );
            
            // Test for a failure.
            if (FAILED(hr))
            {
                goto Failure;
            }
            
            // Test for an error.
            if ( NULL != m_pChildRequestContext )
            {
                // Set the URL for the child request.
                hr = m_pChildRequestContext->GetRequest()->SetUrl(
                    "/example/default.aspx",
                    (DWORD)strlen("/example/default.aspx"),false);
            
                // Test for a failure.
                if (FAILED(hr))
                {
                    goto Failure;
                }
                
                // Execute the child request.
                hr = pHttpContext->ExecuteRequest(
                    TRUE, m_pChildRequestContext,
                    0, NULL, &fCompletionExpected );
                
                // Test for a failure.
                if (FAILED(hr))
                {
                    goto Failure;
                }
                
                // Test for pending asynchronous operations.
                if (fCompletionExpected)
                {
                    return RQ_NOTIFICATION_PENDING;
                }

            }

 Failure:
            // Test for a child request.
            if (NULL != m_pChildRequestContext)
            {
                // Release the child request.
                m_pChildRequestContext->ReleaseClonedContext();
                m_pChildRequestContext = NULL;
            }
        }
        
        // Return processing to the pipeline.
        return RQ_NOTIFICATION_CONTINUE;
    }
    
    REQUEST_NOTIFICATION_STATUS
        OnAsyncCompletion(
        IN IHttpContext * pHttpContext,
        IN DWORD dwNotification,
        IN BOOL fPostNotification,
        IN IHttpEventProvider * pProvider,
        IN IHttpCompletionInfo * pCompletionInfo
        )
    {
        // Test for a child request.
        if (NULL != m_pChildRequestContext)
        {
            // Release the child request.
            m_pChildRequestContext->ReleaseClonedContext();
            m_pChildRequestContext = NULL;
        }
        // Return processing to the pipeline.
        return RQ_NOTIFICATION_CONTINUE;
    }

};

// 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_MAP_PATH,
        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

Antarmuka IHttpContext
Metode IHttpContext::CloneContext
IHttpContext::ExecuteRequest Method