Condividi tramite


Metodo IHttpContext::ReleaseClonedContext

Rilascia un'istanza IHttpContext clonata.

Sintassi

virtual HRESULT ReleaseClonedContext(  
   VOID  
) = 0;  

Parametri

Questo metodo non accetta parametri.

Valore restituito

Oggetto HRESULT. I valori possibili includono, ma non sono limitati a, quelli indicati nella tabella seguente.

Valore Descrizione
S_OK Indica che l'operazione è riuscita.
ERROR_INVALID_PARAMETER Indica che il contesto padre per la richiesta corrente non è valido, ad esempio un contesto figlio viene rilasciato dopo il rilascio dell'elemento padre.

Commenti

Il ReleaseClonedContext metodo rilascia un'istanza di un'interfaccia IHttpContext . Ad esempio, se si crea un contesto figlio usando il metodo IHttpContext::CloneContext , chiamare il metodo del ReleaseClonedContext figlio per rilasciare il contesto figlio al termine dell'utilizzo.

Importante

La chiamata al ReleaseClonedContext metodo per rilasciare una richiesta figlio dopo il rilascio del contesto padre restituirà ERROR_INVALID_PARAMETER. Ad esempio, se si crea un contesto figlio e si usa tale elemento figlio per creare una richiesta figlio nidificata, sarà necessario rilasciare i contesti figlio in ordine inverso della creazione.

Esempio

L'esempio di codice seguente illustra come creare un modulo HTTP che esegue le attività seguenti:

  1. Il modulo esegue la registrazione per la notifica di RQ_MAP_PATH .

  2. Il modulo crea una classe CHttpModule che contiene metodi OnMapPath e OnAsyncCompletion .

  3. Quando un client Web richiede un URL, IIS chiama il metodo del OnMapPath modulo. Questo metodo esegue le attività seguenti:

    1. Verifica se l'URL per la richiesta corrente ha una barra finale o termina con /default.aspx. Se l'URL termina con uno degli elementi, il modulo usa il IHttpContext::CloneContext metodo per creare un clone della richiesta corrente.

    2. Chiama il metodo del IHttpRequest::SetUrl clone per impostare l'URL per il clone su /example/default.aspx.

    3. Chiama il metodo IHttpContext::ExecuteRequest per eseguire la richiesta figlio.

    4. Verifica il completamento asincrono. Se il completamento asincrono è in sospeso, il modulo restituisce l'elaborazione alla pipeline integrata di elaborazione delle richieste. In caso contrario, il modulo rilascia il contesto clonato.

  4. Se è necessario il completamento asincrono, IIS chiama il metodo del OnAsyncCompletion modulo. Questo metodo rilascia il contesto clonato.

  5. Il modulo rimuove la CHttpModule classe dalla memoria e quindi viene chiusa.

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

Il modulo deve esportare la funzione RegisterModule . È possibile esportare questa funzione creando un file di definizione del modulo (con estensione def) per il progetto oppure è possibile compilare il modulo usando l'opzione /EXPORT:RegisterModule . Per altre informazioni, vedere Procedura dettagliata: Creazione di un modulo HTTP Request-Level tramite codice nativo.

Facoltativamente, è possibile compilare il codice usando la __stdcall (/Gz) convenzione di chiamata anziché dichiarare in modo esplicito la convenzione di chiamata per ogni funzione.

Requisiti

Tipo Descrizione
Client - IIS 7.0 in Windows Vista
- IIS 7.5 in Windows 7
- IIS 8.0 in Windows 8
- IIS 10.0 in Windows 10
Server - IIS 7.0 in Windows Server 2008
- IIS 7.5 in Windows Server 2008 R2
- IIS 8.0 in Windows Server 2012
- IIS 8.5 in Windows Server 2012 R2
- IIS 10.0 in Windows Server 2016
Prodotto - 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
Intestazione Httpserv.h

Vedere anche

Interfaccia IHttpContext
Metodo IHttpContext::CloneContext
Metodo IHttpContext::ExecuteRequest