Custom App.Config file for ServicedComponent

I have come across this question few times this week so I though to start this whole 'blogging' with. Developers want to take advantage of the app.config file for the COM+ applications, but they are not available for dll? What now? Well as the hosting process is dllhost.exe, one could create app.config file for dllhost and put it in the %windir%\System32. Not really good solution is it? What is now you install something on the machine and someone thought about the same thing and they copy there app.config file there. Your application breaks...

Windows XP and 2003 provides a nice solution for this using the 'Application Base Directory' property of the COM+ Application.

So how to use it now? First step is to create an application.manifest file and copy it to the folder that you want to use as the base for your application. The content of the file is very simple, just copy the text from below to a notepad and save it as 'application.manifest'.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"/>

Now, create your app.config file for example something like below.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <appSettings>
  <add key="ConfigData" value="My Custom AppSetting!" />
 </appSettings>
</configuration>

Save this file to the same folder as the manifest file, NOTE the name of this file needs to be application.config!!!!

Now you are ready to configure your COM+ application. So, open you Component Services MMC and find your application.

After you have located your COM+ application, right click the application and go to 'Properties' and 'Activation' Tab.

At the bottom of this tab, you should be able to find an option for 'Application Root Directory'. Browse or copy paste the path that you saved the 2 files created earlier, click Apply, click OK and you are done!

Note: If you application is running you need to stop it for the change to take place and the AppDomain of the components BaseDirectory will return you this value!

Now, in your serviced component you can access the application specific configuration file by simply using System.Configuration.ConfigurationSettings.AppSettings["ConfigData"].