I have a C++ application that needs to retrieve an IIS 7 site’s properties (such as metabase properties similar to those in IIS6 – Path, AppFriendlyName etc).
With IIS 7, my code does this:
- Get the
AppHostWritableAdminManagerand commit the pathMACHINE/WEBROOT/APPHOST/Default Web Site/. - Call
GetAdminSectionwith the section nameappSettings. - Then look at the returned collection and look for the property (
Pathfor example).
This works in IIS 6 but not on IIS7/7.5.
What changes do I need to make in order to make this work?
In IIS7 the configuration data is not stored in a “metabase” and also the metabase properties that we’re accustomed to in IIS6 aren’t the same. IIS7 stores the bulk of its configuration data in the following file:
There are other files, but for the purposes of answering this question, this is the file we’re interested in.
The documentation for
applicationHost.configcan be found here:You can find a list of IIS6 metabase -> IIS7 XML configuration mappings here:
For example in IIS6 the path to a site’s
/rootis stored in thePathattribute ofIIsWebVirtualDir. i.e.:But in IIS7 it’s stored differently:
However, if your code must work with both IIS6 and IIS7 then you can install the IIS6 Management Compatibility components. This will allow you to access IIS7 site properties using traditional IIS6 metabase API’s such as ADSI, System.DirectoryServices etc. The compatibility layer will map these properties to the new IIS7 schema for you.
The first part of this article explains how to install this on Vista/Windows7/Windows 2008:
Update:
Unfortunately C++ isn’t my strong point. However I put together an example in C# using COM Interop to demonstrate using the
AppHostWritableAdminManager:The code above locates a site named “MySite” in the
<sites>collection. It then retrieves the site’s<bindings>collection and print’s each bindingsprotocolandbindingInformationattributes.Your should be able to convert this to C++ fairly easily.
To answer the question in your comment –
When using the
AppHostWritableAdminManagerthere isn’t a shortcut to getting directly to the site you want to inspect/modify or it’s properties. In the example above, you’ll see that I need to loop through the sites collection to find the site I’m interested in. The reason for this is thatAppHostWritableAdminManagersees everything as elements and collections of elements. It’s a fairly basic API. Even when using the managedMicrosoft.Web.AdministrationAPI you find that whilst there are some nice properties such asSite.Bindings, these are thinly disguised wrappers aroundAppHostWritableAdminManager.In fact if I want to find a site I still have to search the
Sitescollection either in a for loop or by adding some LINQ sugar if I’m using C#3.5 or later:Site‘s base class isConfigurationElementwhich under the bonnet wraps access toIAppHostElement.Once you’re past some basic shortcut wrapper properties much of what we do in managed code to configure IIS (for example IIS FTP) is elements, attributes and collections of elements.
Update 2:
Please bear in mind I’ve never written a line of C++ in my life. There’s no cleanup of strings or objects: