IIISVersionManager for IIS Express
IIISVersionManager is a Component Object Model ( COM ) comes with IIS Express. It is documented at MSDN, however, I feel we do not have enough sample code.
For this blog, I would like to introduce some sample code that you can leverage it for your project.
Please note that this interface is not most likely needed if IIS Express is used by well-known IDE. However, if you wish to write your own tool and use IIS Express as development Web engine, then you may be interested in using this APIs.
The CoClass of this COM is IIISVersionManager, and its ProgID is Microsoft.IIS.VersionManager.
Its responsibility is to create an instance of IIISVersion.
HRESULT
GetVersionObject(
[in, string] BSTR bstrVersion,
[in] IIS_PRODUCT_TYPE dwProductType,
[out, retval] IIISVersion ** ppElement
);
Give version string and product type, it returns an instance of IIISVersion.
It sounds like there might be multiple versions of IIS product coexist in a system. However, we actually get either Full IIS ( i.e. IIS comes with Windows ) and/or a single version of IIS Express.
If you look at IIS_PRODUCT_TYPE enum, there are two valid enum values; IIS_PRODUCT_OS_SERVICE, IIS_PRODUCT_EXPRESS.
Ex 1)
Let's start with sample code which creates an instance of IISVersionManager and IIISVersion, and query some information.
For this blog, I used JScript. However, you know this is COM, so you can use any COM client codes; .NET Interoperability, C++, and etc.
var versionManager = new ActiveXObject("Microsoft.IIS.VersionManager");
var versionObj = versionManager.GetVersionObject( "8.0", // version
2 ); // IIS_PRODUCT_EXPRESS
var installPath = versionObj.GetPropertyValue( "installPath" );
var version = versionObj.GetPropertyValue( "version" );
WScript.Echo( "version: " + version + ", installPath: " + installPath );
RESULT: version: 8.0, installPath: C:\Program Files\IIS Express
Ex 2)
GetPropertyValue is an interesting interface from IIISVersion object, which return anything, you know what VARIANT means :), from a given property key string.
HRESULT
GetPropertyValue(
[in, string] BSTR bstrName,
[out, retval] VARIANT * pvarValue
);
Some of key properties return merely metadata, as shown in above code sample.
Let's see another sample code which obtains some runtime information.
First, you should start IIS Express. For this particular example, we use command line to start IIS Express.
C:\Program Files\IIS Express>iisexpress.exe
Starting IIS Express ...
Successfully registered URL "http://localhost:8080/" for site "WebSite1" application "/"
Registration completed for site "WebSite1"
IIS Express is running.
Enter 'Q' to stop IIS Express
var versionManager = new ActiveXObject("Microsoft.IIS.VersionManager");
var versionObj = versionManager.GetVersionObject( "8.0", 2 );
// Get IIISExpressProcessUtility instance. http://msdn.microsoft.com/en-us/library/gg418413(v=vs.90)
var processUtil = versionObj.GetPropertyValue( "expressProcessHelper" );
// Find IIS Express process ID running with Site: WebSite1 and Application Pool: Clr4IntegratedAppPool
var pid = processUtil.GetRunningProcessForSite( "WebSite1", "", "Clr4IntegratedAppPool", "" );
// Get IIISUserData instance. http://msdn.microsoft.com/en-us/library/gg418417(v=vs.90)
var userData = versionObj.GetPropertyValue( "userInstanceHelper" );
var userDir = userData.IISDirectory
WScript.Echo( "IIS Express running " + "WebSite1, " + "Pid: " + pid + ", User Directory: " + userDir );
RESULT: IIS Express running WebSite1, Pid: 3068, User Directory: C:\Users\tim\Documents\IISExpress
Ex 3)
IIS Express ships with some registry-free COM. We can create those via CreateObjectFromProgId.
HRESULT
CreateObjectFromProgId(
[in, string] BSTR bstrObjectName,
[out, retval] VARIANT * pObject
);
The first parameter is ProgID which we wish to create an instance.
Here is sample code.
var versionManager = new ActiveXObject("Microsoft.IIS.VersionManager");
// Gets IIS Express version object
var versionObj = versionManager.GetVersionObject( "8.0", 2 );
// Create AhAdmin instance of IIS Express
var iisExpressAdminMgr = versionObj.CreateObjectFromProgId("Microsoft.ApplicationHost.AdminManager");
// Read user profile configuration.
var defDocSection = iisExpressAdminMgr.GetAdminSection("system.webServer/defaultDocument", "MACHINE/WEBROOT/APPHOST/WebSite1");
var property = defDocSection.GetPropertyByName("enabled");
WScript.Echo( "defaultDocument at WebSite1 is " + ( property ? "enabled" : "disabled" ) );
RESULT: defaultDocument at WebSite1 is enabled
Note that this is a Microsoft.ApplicationHost.AdminManager instance of IIS Express, which reads configuration defined at user profile directory. For more information about Microsoft.ApplicationHost.AdminManager, you can refer to here.
The main difference between two is which configuration file it reads. The full IIS deals with single one per system typically located at %windir%\system32\inetsrv\config, but the IIS Express deals with one located at user profile.