Compartilhar via


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

Comments

  • Anonymous
    March 05, 2008
    I apologize right away that it's been a while since I've blogged about the interop forms toolkit although

  • Anonymous
    March 05, 2008
    That works great!  However, do you know how to run the VB6 app in debug mode and still use the config file?  I've tried several names and locations for the file with no luck accessing it from my .NET interop DLL. By the way, your videos and articles have helped me IMMENSELY to acquire the knowledge to use interop (albeit C#) to bridge a VB6 to .NET gap in my current project!  Many thanks!!

  • Anonymous
    March 26, 2008
    When running in debug mode, your app isn't truly executing as a process, it is being interpreted by the development environment. That is, the VB 6.0 IDE is the host executable so your config file needs to "attached" to it. To do this copy the config file to "C:Program FilesMicrosoft Visual StudioVB98VB6.exe.config".  

  • Anonymous
    June 05, 2008
    The comment has been removed

  • Anonymous
    May 11, 2012
    This is wonderful info, thank you.  But I'm trying to consume the data from the .Net DLL in a COM DLL hosted in COM+.  Where would the .config file go then?  I've read about putting the info in the machine.config on the COM+ server.  Not sure if I'd want to do that.