IHttpModuleContextContainer 인터페이스
IHttpStoredContext 포인터의 컬렉션을 유지 관리합니다.
구문
class IHttpModuleContextContainer
메서드
다음 표에서는 클래스에서 노출하는 메서드를 나열합니다 IHttpModuleContextContainer
.
속성 | 설명 |
---|---|
GetModuleContext | 컨텍스트 컨테이너에서 저장된 컨텍스트를 반환합니다. |
SetModuleContext | 컨텍스트 컨테이너에 저장된 컨텍스트를 설정합니다. |
파생 클래스
속성 | 설명 |
---|---|
IDispensedHttpModuleContextContainer | 컨테이너를 IHttpModuleContextContainer 해제하는 기능을 제공하여 확장됩니다. |
설명
많은 IIS 7 클래스는 포인터를 private
IDispensedHttpModuleContextContainer
멤버 변수로 유지 관리합니다. 이러한 클래스는 IHttpApplication, IHttpConnection, IHttpContext, IHttpFileInfo, IHttpSite, IHttpUrlInfo 및 IMetadataInfo를 비롯한 다양한 인터페이스를 구현합니다.
이러한 각 인터페이스는 인수를 허용하지 않고 포인터를 GetModuleContextContainer
반환하는 메서드를 IHttpModuleContextContainer
정의합니다. 다양한 GetModuleContextContainer
메서드가 호출되면 대부분의 구현자는 데이터를 업캐스트IHttpModuleContextContainer
로 반환 private
합니다. 이렇게 하면 인터페이스 구현자가 해당 컨테이너의 수명을 유지하면서 사용자 지정 컨테이너를 노출할 수 있습니다.
인터페이스를 구현하는 사용자 지정 클래스를 IHttpStoredContext
정의한 다음 연산자를 호출하여 이 IHttpStoredContext
클래스 구현자에 대한 포인터를 new
만들 수 있습니다. 그런 다음 SetModuleContext 및 GetModuleContext 메서드를 각각 호출하여 포인터에 IHttpModuleContextContainer
이 포인터를 추가하고 검색할 수 있습니다.
포인터가 IHttpStoredContext
더 이상 필요하지 않으면 IHttpStoredContext::CleanupStoredContext 메서드가 내부적으로 호출됩니다. 여기서 인터페이스 메서드의 IHttpStoredContext
구현자는 일반적으로 호출 delete``this
됩니다.
주의
dynamic_cast 연산자를 사용하여 다운캐스트 IHttpModuleContextContainer
하는 IDispensedHttpModuleContextContainer
것이 안전한 작업일 수 있지만 이 캐스트를 수행하지 않아야 합니다. 인터페이스는 IDispensedHttpModuleContextContainer
기본 인터페이스에 하나의 메서드 ReleaseContainer
만 추가하고 이 메서드는 내부적으로만 호출해야 합니다.
호출자 참고 사항
경우에 따라 확장된 동작에 IHttpModuleContextContainer
액세스하기 위해 포인터가 인터페이스로 IDispensedHttpModuleContextContainer
다운캐스트될 수 있습니다.
주의
다운캐스트 작업을 수행하기 전에 항상 올바른 프로그램 동작을 보장하기 위해 캐스트가 올바른지 확인합니다.
참고 항목
다운캐스트 작업을 수행할 때 가능하면 언제든지 dynamic_cast 연산자를 사용하는 것이 좋습니다.
예시
다음 코드 예제에서는 GL_TRACE_EVENT 이벤트를 수신 대기한 다음 사용자 지정 IHttpStoredContext 정보를 이벤트 뷰어 쓰는 전역 모듈을 만드는 방법을 보여 줍니다.
주의
IIS 7은 이벤트 뷰어 많은 수의 이벤트를 생성합니다. 프로덕션 환경에서 로그 오버플로 오류를 방지하려면 일반적으로 이벤트 로그에 캐시 정보를 쓰지 않아야 합니다. 데모를 위해 이 코드 예제에서는 디버그 모드에서만 이벤트 뷰어 항목을 씁니다.
#pragma warning( disable : 4290 )
#pragma warning( disable : 4530 )
#define _WINSOCKAPI_
#include <windows.h>
#include <sal.h>
#include <tchar.h>
#include <initguid.h>
#include <httptrace.h>
#include <httpserv.h>
#include <httpcach.h>
#include <string>
using namespace std;
// The CConvert class mirrors the Convert class that is
// defined in the .NET Framework. It converts primitives
// and other data types to wstring types.
class CConvert
{
public:
// The ToByteString converts a double-byte
// character string to a single-byte string.
// str: the double-byte string to convert.
// return: a single-byte string copied from str.
static string ToByteString(const wstring& str)
{
// Get the length of the
// double-byte string.
size_t length = str.length();
// Create a temporary char pointer.
char* byteChar = new char[length+1];
byteChar[0] = '\0';
// Copy the double-byte character string
// into the single-byte string.
size_t charsReturned = 0;
wcstombs_s(&charsReturned, byteChar,
length+1, str.c_str(), length+1);
// Create a string to return.
string retString = byteChar;
// Delete the temporary string and
// set that string to NULL.
delete[] byteChar;
byteChar = NULL;
// Return the single-byte string.
return retString;
}
};
// The CEventWriter class writes XML
// documents and strings to the event log.
class CEventWriter
{
public:
// Creates the CEventWriter class.
// name: the name of the
// event log to open.
CEventWriter(const wstring& name)
{
#ifdef UNICODE
m_eventLog = RegisterEventSource(NULL, name.c_str());
#else
string multiName = CConvert::ToByteString(name);
m_eventLog = RegisterEventSource(NULL, multiName.c_str());
#endif
}
// Creates the destructor for the
// CEventWriter class. This destructor
// closes the HANDLE to the event
// log if that HANDLE is open.
virtual ~CEventWriter()
{
// If the HANDLE to the event
// log is open, close it.
if (NULL != m_eventLog)
{
// Deregister the event log HANDLE.
DeregisterEventSource(m_eventLog);
// Set the HANDLE to NULL.
m_eventLog = NULL;
}
}
// The ReportInfo method writes
// a wstring to the event log.
// info: the wstring to write.
// return: true if the event log is written.
BOOL ReportInfo(const wstring& info)
{
return ReportEvent(EVENTLOG_INFORMATION_TYPE, info);
}
protected:
// The ReportEvent method accepts an event type
// and a wstring, and attempts to write that
// event to the event log.
// type: the type of the event.
// data: the wstring to write to the event log.
// return: true if the event log is written;
// otherwise, false.
BOOL ReportEvent(WORD type, const wstring& data)
{
// If the m_eventLog HANDLE
// is NULL, return false.
if (NULL == m_eventLog)
{
return FALSE;
}
#ifndef _DEBUG
// If the current build is not debug,
// return so the event log is not written.
return TRUE;
#endif
#ifdef UNICODE
// The unicode version of the ReportEvent
// method requires double-byte strings.
PCWSTR arr[1];
arr[0] = data.c_str();
return ::ReportEvent(m_eventLog,
type,
0, 0, NULL, 1,
0, arr, (void*)arr);
#else
// The non-unicode version of the ReportEvent
// method requires single-byte strings.
string multiByte =
CConvert::ToByteString(data);
LPCSTR arr[1];
arr[0] = multiByte.c_str();
return ::ReportEvent(m_eventLog,
type,
0, 0, NULL, 1,
0, arr, (void*)arr);
#endif
}
private:
// Specify the HANDLE to the
// event log for writing.
HANDLE m_eventLog;
};
// The CStoredContext class implements
// the IHttpStoredContext interface.
class CStoredContext : public IHttpStoredContext
{
public:
// The constructor for the CStoredContext
// class. Initializes a CEventWriter to write
// to the Event Log.
CStoredContext() : m_eventWriter(L"IISADMIN")
{
// Create a string to write.
wstring constructor(L"CStoredContext::Constructor");
// Write the string to the Event Log.
m_eventWriter.ReportInfo(constructor);
}
// The Display method writes
// L"CStoredContext::Display"
// to the Event Log.
virtual void Display()
{
// Create the string to write.
wstring display(L"CStoredContext::Display");
// Write the string to the Event Log.
m_eventWriter.ReportInfo(display);
}
// The CleanupStoredContext is the pure virtual
// method that all non-abstract classes implementing
// the IHttpStoredContext must implement.
virtual void CleanupStoredContext()
{
wstring cleanup(L"CStoredContext::CleanupStoredContext");
m_eventWriter.ReportInfo(cleanup);
delete this;
}
protected:
// The protected destructor for the
// CStoredContext class. This method is
// protected because the clients of this
// class should dispose of a class instance
// by calling the CleanupStoredContext method.
virtual ~CStoredContext()
{
wstring destructor(L"CStoredContext::Destructor");
m_eventWriter.ReportInfo(destructor);
}
private:
// Specify the private CEventWriter
// for writing events.
CEventWriter m_eventWriter;
};
// The CGlobalTraceModule class creates the CGlobalModule
// class and registers for GL_TRACE_EVENT events.
class CGlobalContainerModule : public CGlobalModule
{
public:
// Creates the destructor for the
// CGlobalTraceModule class.
virtual ~CGlobalContainerModule()
{
}
// The RegisterGlobalModule method creates and registers
// a new CGlobalTraceModule for GL_TRACE_EVENT events.
// dwServerVersion: the current server version.
// pModuleInfo: the current IHttpModuleRegistrationInfo pointer.
// pGlobalInfo: the current IHttpServer pointer.
// return: ERROR_NOT_ENOUGH_MEMORY if the heap is out of
// memory; otherwise, the value from the call to the
// SetGlobalNotifications method on the pModuleInfo pointer.
static HRESULT RegisterGlobalModule
(
DWORD dwServerVersion,
IHttpModuleRegistrationInfo* pModuleInfo,
IHttpServer* pGlobalInfo
)
{
// The IHttpModuleRegistrationInfo
// pointermust not be NULL.
if (NULL == pModuleInfo)
{
return E_INVALIDARG;
}
// Get the HTTP_MODULE_ID from the
// IHttpModuleRegistrationInfo pointer.
HTTP_MODULE_ID moduleId =
pModuleInfo->GetId();
// The HTTP_MODULE_ID pointer
// must not be NULL.
if (NULL == moduleId)
{
return E_INVALIDARG;
}
// Create a new CGlobalContainerModule pointer
// using the HTTP_MODULE_ID from the
// IHttpModuleRegistrationInfo pointer.
CGlobalContainerModule* containerModule =
new CGlobalContainerModule(moduleId);
// Return an out-of-memory error if the containerModule
// is NULL after the call to the new operator.
if (NULL == containerModule)
{
return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
}
// Attempt to set global notification
// for an GL_TRACE_EVENT event by using
// the traceModule as a listener.
HRESULT hr = pModuleInfo->SetGlobalNotifications
(containerModule, GL_TRACE_EVENT);
// Return the HRESULT from the call to
// the SetGlobalNotifications method.
return hr;
}
// The OnGlobalTraceEvent method is the callback
// method for GL_TRACE_EVENT events in the pipeline.
// pProvider: the IGlobalTraceEventProvider pointer.
// return: GL_NOTIFICATION_CONTINUE.
virtual
GLOBAL_NOTIFICATION_STATUS
OnGlobalTraceEvent
(
IN IGlobalTraceEventProvider* pProvider
)
{
// If the IGlobalTraceEventProvider pointer
// is NULL, return GL_NOTIFICATION_CONTINUE.
if (NULL == pProvider)
{
return GL_NOTIFICATION_CONTINUE;
}
// Declare an IHttpContext pointer.
IHttpContext* httpContext = NULL;
// Declare an HRESULT and initialize
// the HRESULT to E_FAIL.
HRESULT hr = E_FAIL;
// Call the GetCurrentHttpRequestContext
// method on the IGlobalTraceEventProvider
// pointer.
hr = pProvider->GetCurrentHttpRequestContext(&httpContext);
// If the GetCurrentHttpRequestContext
// method failed, or the IHttpContext
// pointer is NULL, return GL_NOTIFICATION_CONTINUE.
if (FAILED(hr) || (NULL == httpContext))
{
return GL_NOTIFICATION_CONTINUE;
}
// Get the IHttpModuleContextContainer
// pointer from the IHttpContext pointer.
IHttpModuleContextContainer* container =
httpContext->GetModuleContextContainer();
// If the IHttpModuleContextContainer is
// NULL, return GL_NOTIFICATION_CONTINUE.
if (NULL == container)
{
return GL_NOTIFICATION_CONTINUE;
}
// Get the IHttpStoredContext pointer
// from the IHttpModuleContextContainer
// pointer.
IHttpStoredContext* storedContext =
container->GetModuleContext(m_moduleId);
// If the IHttpStoredContext pointer is
// non-NULL, use the dynamic_cast operator
// to retrieve the CStoredContext pointer
// from the storedContext.
if (NULL != storedContext)
{
// Attempt to cast the IHttpStoredContext
// pointer to a CStoredContext pointer.
CStoredContext* customContext =
dynamic_cast<CStoredContext*>(storedContext);
// If the cast does not return
// NULL, ask the CStoredContext
// poitner to display itself.
if (NULL != customContext)
{
customContext->Display();
}
}
if (NULL == storedContext)
{
// Create a custom CStoredContext pointer.
IHttpStoredContext* customContext =
new CStoredContext;
// Call the SetModuleContext method using
// the IHttpModuleContextContainer pointer.
container->SetModuleContext(customContext, m_moduleId);
}
// Return GL_NOTIFICATION_CONTINUE.
return GL_NOTIFICATION_CONTINUE;
}
// The Terminate method is required for
// non-abstract CGlobalTraceModule classes.
// This method calls delete on this.
virtual VOID Terminate(VOID)
{
delete this;
}
protected:
// Creates the constructor for the CGlobalTraceModule
// class. This constructor initializes the CEventWriter
// to write to the application event log.
// moduleId: the current module identifier.
CGlobalContainerModule(HTTP_MODULE_ID moduleId)
{
m_moduleId = moduleId;
}
private:
// Specify the HTTP_MODULE_ID
// for this module.
HTTP_MODULE_ID m_moduleId;
};
// The RegisterModule method is the
// main entry point for the DLL.
// dwServerVersion: the current server version.
// pModuleInfo: the current
// IHttpModuleRegistrationInfo pointer.
// pGlobalInfo: the current IHttpServer pointer.
// return: the value returned by calling the
// CGlobalContainerModule::RegisterGlobalModule
// method.
HRESULT
__stdcall
RegisterModule(
DWORD dwServerVersion,
IHttpModuleRegistrationInfo* pModuleInfo,
IHttpServer* pGlobalInfo
)
{
// Call the static method for initialization.
return CGlobalContainerModule::RegisterGlobalModule
(dwServerVersion,
pModuleInfo,
pGlobalInfo);
}
위의 코드는 데이터 상자에 다음과 유사한 연속 문자열이 포함된 이벤트 뷰어 새 이벤트를 씁니다.
CStoredContext::Destructor
CStoredContext::CleanupStoredContext
CStoredContext::Display
CStoredContext::Constructor
모듈은 RegisterModule 함수를 내보내야 합니다. 프로젝트에 대한 모듈 정의(.def) 파일을 만들어 이 함수를 내보내거나 스위치를 사용하여 /EXPORT:RegisterModule
모듈을 컴파일할 수 있습니다. 자세한 내용은 연습: 네이티브 코드를 사용하여 요청 수준 HTTP 모듈 만들기를 참조 하세요.
필요에 따라 각 함수에 대한 호출 규칙을 명시적으로 선언하는 대신 호출 규칙을 사용하여 __stdcall (/Gz)
코드를 컴파일할 수 있습니다.
상속 계층 구조
IHttpModuleContextContainer
IDispensedHttpModuleContextContainer
요구 사항
Type | 설명 |
---|---|
클라이언트 | - Windows Vista의 IIS 7.0 - Windows 7의 IIS 7.5 - Windows 8의 IIS 8.0 - Windows 10의 IIS 10.0 |
서버 | - Windows Server 2008의 IIS 7.0 - Windows Server 2008 R2의 IIS 7.5 - Windows Server 2012의 IIS 8.0 - Windows Server 2012 R2의 IIS 8.5 - Windows Server 2016의 IIS 10.0 |
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 |
헤더 | Httpserv.h |