Implementing a Custom ConnectionString Installer for Setup
Inserting a Breakpoint for Debugging
First, if you write a custom action/installer, like all code, you will want to debug it. The key to debugging installers is to add this statement:
If you try attaching to a running install process—one way to debug a running process—Visual Studio seems incapable of breaking into the installer. However, with the Debugger.Break() call you will be prompted to attach an instance of Visual Studio.
Creating the DataLinksClass
Reading Context Parameters
The Context.Parameters dictionary lets you get values from the Setup process. You want the installation target directory and the name of the configuration file to modify. (You will set these when you define the setup project and custom action.)
If, for some reason, the config file can't be found, the code throws an exception that will roll back the install. You could, in practice, let the install continue because you could always set the connection string manually, too.
Using the ConfigurationManager to Overwrite the ConnectionString
The code wraps by using the ConfigurationManager.OpenExeConfiguration, reading the connection string section, and setting the connection string dynamically. The call to GetAdjustedConnectionString strips out the Provider, which you don't need.
Finally, you use the ConfigurationSection class and the ProtectSection method to encrypt the connection string before saving. .NET includes a provider statement to the config section and automatically "knows" how to unencrypt and encrypted config section. When finished, you save the changes.
Defining the Setup Project
Visual Studio defines a setup project template and includes a wizard. You can add the setup project by using the wizard. The wizard will include the application and the custom installer.
To add the custom action, follow these steps:
- Select the setup project.
- Click the View|Editor|Custom Actions menu.
- Right-click the install item and select add custom action.
- Pick the output from MyInstaller as the custom action.
- Press F4 to open the Properties window and, in the CustomActionData field, add the parameters your installer needs: /configFile="UsesConnection.exe"/targetDir="[TARGETDIR]\"
Figure 6 shows approximately what Visual Studio will look like after you complete the steps listed above. (Hint: Pay close attention to the use of quotes and slashes in the CustomActionData.)
Figure 6: The CustomActionData contains our context parameters, configFile and targetDir.
Run and Test
Finish up by running the install project by selecting the setup project and clicking the Project|Install menu item. After running the install, open the UsesConnection.exe.config file and you will see the encrypted connection string and the Rsa provider information. Run the deployed UsesConnection project, and you will see that the connection string works, implying clearly that .NET has unencrypted the string for you.
If you need some more help, check these supporting links:
MSBuild is powerful. The setup integration in .NET is sometimes a little confusing. However, when you figure out the Custom Actions and installer behaviors you can add just about any behavior you want. In ther example, you used an existing COM feature through Interop to define a connection string. You also used the .NET framework to encrypt the connection string. (This is a good idea in most cases, falling under the auspices of protecting secrets.)
About the Author
Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming and .NET. Look for his upcoming book LINQ Unleashed for C# from Sams. You may contact him for technology questions at email@example.com.
If you are interested in joining or sponsoring a .NET Users Group, check out www.glugnet.org. Glugnet has two groups, Glugnet in East Lansing and Glugnet-Flint (in Flint, of course) and some of the best sponsors (and some of the best companies) in the free world. If you are interested in attending, check out the www.glugnet.org web site for updates or contact me.
Copyright © 2007 by Paul T. Kimmel. All Rights Reserved.
Page 4 of 4