Using IMetadataInfo::GetModuleContextContainer to store configuration data
If you write an IIS7 module, you would typically want to control its behavior based on some configuration data. IIS7 makes it really easy for developers to extend configuration schema so that their customers can put configuration data for their modules with other IIS configuration in applicationHost.config and web.config. Native module developers can then use AhAdmin APIs to read this configuration data. Reading configuration data for each request can be expensive. Developers would typically think of improving the performance by reading the configuration data once and then keeping the data in memory which can be used for other requests. To do this right, you need to worry about following things.
- Keep unique data only for the paths where configuration changes and not for all possible configuration paths.
- Detect configuration changes so that module always behave as per the latest configuration.
- Only delete the configuration data of paths which are affected by the change and not throw everything.
- Make sure this configuration store is accessed in thread safe manner.
Instead of implementing this from scratch, you can use IMetadata interface available in httpserv.h. Call IHttpContext::GetMetadata() to get IMetadata object which is different for each unique configuration path and then use the IMetadataInfo::GetModuleContextContainer()->SetModuleContext() to store any data you like for your module. Configuration data is a perfect example of kind of data you would like to store for a unique configuration path. Stored data can be retrieved using IMetadataInfo::GetModuleContextContainer()->GetModuleContext(). In addition to making sure that a unique metadata object is available only for paths where configuration changes, IIS will also take care of deleting proper stored contexts when a change notification arrives. Here is how doing this will look like in code.
//
HRESULT
class MODULE_CONFIG : public IHttpStoredContext
// Always call this method to get configuration data
//
//static
pModuleConfig = NULL;
//
HRESULT
)
BSTR bstrSectionName = NULL;
pAdminManager = g_pGlobalInfo->GetAdminManager();
Hope this helps.
Kanwal