Using My.Settings and WCF Configuration with the Interop Forms Toolkit

Recently I received a question from a customer asking how to get an Interop User control developed with the Interop Forms Toolkit to be able to read the application configuration settings (app.config) in order to call a WCF service. (If you're unfamiliar with how to develop Interop user controls you can read these posts and watch these videos.) Since Interop user controls are compiled into library (.dll) assemblies the configuration file that is generated is named after the assembly, not the VB 6 application, which means that the Interop User Control isn't able to read the settings from this file.

image

The trick to solve this is simple, just rename the .dll.config file generated in your bin folder to the name of your VB 6 EXE and place that file in the same folder as your VB 6 EXE. Once you do that, the WCF service client configuration settings will be read properly at runtime.

You can easily set this to happen automatically using Build Events. Just double-click on My Project in the Solution Explorer to open the project properties, select the Compile tab, click the "Build Events" button, then click "Edit Post-build...". In the window type the copy command you want to execute. I.e.

copy /Y "$(TargetDir)$(ProjectName).dll.config" "$(ProjectDir)..\..\VB6App\Project1.exe.config"

image

Now every time the app.config file is modified in your Interop user control project, the .config settings will get renamed and copied to the right place, your VB6 application folder.

But what about using My.Settings? My.Settings allow you to easily store application-scoped and user-scoped settings and access them easily in your .NET programs. They are also stored in the application config file. But just renaming the file in this case doesn't work alone. This is because My.Settings uses what's called a Settings Provider. In Visual Studio 2005 the default settings provider loads and saves settings using the configuration system, thus they appear in the .config file, however they are read differently than the WCF settings.

So in addition to renaming the config file, we need to also modify the section headers in the app.config file to also specify the name of our VB 6 application (the bolded sections were added): 

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

    <configSections>

        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >

            <section name="AMyInteropUserControlLibrary1.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />

          <!--

          Using userSettings in a VB6 application:

          Change the name here to the name of your VB6 .EXE -->

            < sectionname = "Project1.My.MySettings"type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"allowExeDefinition="MachineToLocalUser"requirePermission="false" />

        </sectionGroup>

        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >

            <section name="AMyInteropUserControlLibrary1.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

          <!--

          Using applicationSettings in a VB6 application:

          Change the name here to the name of your VB6 .EXE -->

          < sectionname = "Project1.My.MySettings"type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"requirePermission="false" />

        </sectionGroup>

    </configSections>

These section groups will not be lost if you add new settings via the Settings tab on your project properties. Now you can freely change the setting in the .exe.config file after deployment and your Interop user control will be able to read the settings properly.  I've attached a complete example, including the WCF service, for you to play with in Visual Studio 2005. Please read the Readme.txt included for proper set-up.

Enjoy,
-B

SettingsAndInteropUserControl.zip