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.
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"
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 -->
<section name="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 -->
<section name="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
Beth is a Program Manager on the Visual Studio Community Team at Microsoft and is responsible for producing and managing content for business application developers, driving community features and team participation onto MSDN Developer Centers (http://msdn.com), and helping make Visual Studio one of the best developer tools in the world. She also produces regular content on her blog (http://blogs.msdn.com/bethmassi), Channel 9, and a variety of other developer sites and magazines. As a community champion and a long-time member of the Microsoft developer community she also helps with the San Francisco East Bay .NET user group and is a frequent speaker at various software development events. Before Microsoft, she was a Senior Architect at a health care software product company and a Microsoft Solutions Architect MVP. Over the last decade she has worked on distributed applications and frameworks, web and Windows-based applications using Microsoft development tools in a variety of businesses. She loves teaching, hiking, mountain biking, and driving really fast.