Custom Installer Actions: Edit Connection Strings, IIS Directory Security Settings, etc.

When I need to deliver a web application to a client, I create a Setup/Deployment Project and then send them the .MSI file.  This makes it super easy to make sure that all the dependencies are there and that the web directory gets set up in IIS.  It eliminates 80% of the deployment hassle. 

The remaining 20% of the deployment hassle comes from having to tweek database connection strings in different files and updating other configuration settings.  In order to do this with the Deployment Project in VS2005, you need to implement a Custom Installer Action.

I finally got around to doing writing one this week.  

Here's what it can do:

  • Edit database connection strings through a dialog that pops up during the installation
  • Types of connection strings supported: NHibernate (hibernate.cfg.xml), Stand-alone Enterprise Library (*.config) files, Web.config "connectionString" values, Web.config appSettings-based "add" connection strings
  • Modify IIS Directory Security Settings (Allow NTLM, Allow Basic, Allow Anonymous)
  • Update paths to configuration files referenced in Web.config (eg. custom external configuration files)

I've posted two zip files: the source code and the binaries.  The source code includes the code for the installer actions, the VS2005 unit tests, a sample web application, and a sample installer for the web application.  If you're looking for an example of how to edit IIS from C#, IISActiveDirectoryUtility.cs inside of the Com.Benday.InstallerActions project is a great place to start. 

If you want to use the custom action binaries in your own installer project, you need to:

  1. In Solution Explorer, click on your web application deployment project
  2. Click the "File System Editor" button (at the top of Solution Explorer)
  3. Go to the "bin" directory inside the "Web Application Folder"
  4. Right-click the "bin" folder and choose Add --> File...
  5. Choose Com.Benday.InstallerActions.dll (from wherever you put it)
  6. Now click on the "Custom Actions Editor" button (at the top of Solution Explorer)
  7. Right click on the "Install" folder and add one or more references to Com.Benday.InstallerActions.dll
  8. Edit the CustomActionData value in the properties for each reference to Com.Benday.InstallerActions.dll according to the documentation later in this post

Your Custom Actions screen should now look something like this:

And after you've configured it, the properties for each should look something like this:

CustomActionData arguments to edit the IIS directory security settings:

  • /action=VirtualDirectorySecurity
  • /virtualDirectory="[TARGETVDIR]"
    [TARGETVDIR] is a reserved variable that is supplied by the installer that tells you the name of the IIS directory
  • /AllowNTLM=1 or /AllowNTLM=0
    To enable NTLM on the directory set this value to 1.  To disable NTLM, set this value to 0 or omit /AllowNTLM
  • /AllowBasic
    To enable basic authentication on the directory set this value to 1.  To disable, set this value to 0 or omit.
  • /AllowAnonymous
    To enable anonymous access on the directory set this value to 1.  To disable, set this value to 0 or omit.

CustomActionData arguments for editing NHibernate database connection strings:

  • /action=ConfigurationFiles
  • /targetDirectory="[TARGETDIR]\"
    [TARGETDIR] is a reserved variable that is supplied by the installer that points to the filesystem path for the directory. HINT: make sure that you put this in quotes and that you have a "\" after [TARGETDIR].  If you see weird FileNotFound errors, this is probably the culprit. 
  • /hibernateConfigFile=hibernate.cfg.xml
    This is the name of the NHibernate configuration file.  This value will always be "hibernate.cfg.xml".

CustomActionData arguments for editing connection strings stored in Web.config's "connectionStrings" element:

  • /action=ConfigurationFiles
  • /targetDirectory="[TARGETDIR]\"
    [TARGETDIR] is a reserved variable that is supplied by the installer that points to the filesystem path for the directory. HINT: make sure that you put this in quotes and that you have a "\" after [TARGETDIR].  If you see weird FileNotFound errors, this is probably the culprit. 
  • /webConfigConnectionString=WebConfigConnectionString
    This is the "name" of the connection string that you want to edit

CustomActionData arguments for editing connection strings stored in Web.config's "appSettings" element:

  • /action=ConfigurationFiles
  • /targetDirectory="[TARGETDIR]\"
    [TARGETDIR] is a reserved variable that is supplied by the installer that points to the filesystem path for the directory. HINT: make sure that you put this in quotes and that you have a "\" after [TARGETDIR].  If you see weird FileNotFound errors, this is probably the culprit. 
  • /webConfigDatabaseKeys=DB_CONNECTION;ANOTHER_DB_CONNECTION
    The value is the name of the "add" element that has the connection string in it.  If you have multiple "add" elements with database connection strings in them, separate the values by semi-colon.

CustomActionData arguments for editing connection strings stored in a separate Enterprise Library Database Configuration file:

  • /action=ConfigurationFiles
  • /targetDirectory="[TARGETDIR]\"
    [TARGETDIR] is a reserved variable that is supplied by the installer that points to the filesystem path for the directory. HINT: make sure that you put this in quotes and that you have a "\" after [TARGETDIR].  If you see weird FileNotFound errors, this is probably the culprit. 
  • /enterpriseLibraryDbConfigFile=dataConfiguration.config
    This is the name of the file that has the enterprise library configuration data

CustomActionData arguments for editing paths to files referenced in Web.config's appSettings:

  • /action=ConfigurationFiles
  • /targetDirectory="[TARGETDIR]\"
    [TARGETDIR] is a reserved variable that is supplied by the installer that points to the filesystem path for the directory. HINT: make sure that you put this in quotes and that you have a "\" after [TARGETDIR].  If you see weird FileNotFound errors, this is probably the culprit. 
  • /webConfigUpdatePathToFilenamesKey=PATH_TO_A_FILE;SomeImportantFile.txt
    This is a semi-colon delimited value.  The first part of the value is the name of the "add" key in "appSettings".  The second part is the name of the file that needs to have it's full filesystem path updated.  If you need to update more than one file reference, you can add more semi-colon delimited pairs. 

More Hints:

If you have several database config values that that all need to point to the same database -- for example, some codee connects via Enterprise Library and some code uses hibernate.cfg.xml -- put all of these into the same /action=ConfigurationFiles reference.  A CustomActionData that looks like this "/action=ConfigurationFiles /targetDirectory="[TARGETDIR]\" /enterpriseLibraryDbConfigFile=dataConfiguration.config /hibernateConfigFile=hibernate.cfg.xml" would allow you to set the enterprise library db config file and the nhibernate config file to the same database connection at the same time. 

Definitely take a look at the custom action configuration in the sample installer that's included in the source code zip.  After that, feel free to post your questions.

-Ben

posted @ Tuesday, January 02, 2007 8:20 AM

Print

Comments on this entry:

# Interesting Finds: January 2, 2007

Left by Jason Haley at 1/2/2007 7:01 PM
Gravatar

# re: Custom Installer Actions: Edit Connection Strings, IIS Directory Security Settings, etc.

Left by The Other Steve at 1/9/2007 10:37 AM
Gravatar
I was just looking for something like this. I think you could probably get a more accurate path to the website by using the TARGETSITE property rather then looping through all sites looking for the virtual directory.

# re: Custom Installer Actions: Edit Connection Strings, IIS Directory Security Settings, etc.

Left by Benjamin Day at 1/9/2007 10:43 AM
Gravatar
Hey "Other Steve",

I had a feeling that there might be a better way. Thanks for the tip.

Can you elaborate a little on why TARGETSITE would be better?

I'm looping thru the virtual directories to make sure that I get the web site (in case there are multiple websites configured for the same instance of IIS).

-Ben

# re: Custom Installer Actions: Edit Connection Strings, IIS Directory Security Settings, etc.

Left by Marcos at 1/15/2007 4:27 AM
Gravatar
Hum, trying to configure the web.config connString, i'm getting the following error:

"Object not set to an instance of an object"

My CustomActionData is:

/action=ConfigurationFiles /targetDirectory="[TARGETDIR]\" /webConfigConnectionString=WebConfigConnectionString

I think it's right.

# re: Custom Installer Actions: Edit Connection Strings, IIS Directory Security Settings, etc.

Left by Benjamin Day at 1/15/2007 6:48 AM
Gravatar
Marcos,

Could you post or email your web.config file?

Thanks,
-Ben

# re: Custom Installer Actions: Edit Connection Strings, IIS Directory Security Settings, etc.

Left by Marcos at 1/15/2007 7:50 AM
Gravatar
It's the same that is in your example :)

# re: Custom Installer Actions: Edit Connection Strings, IIS Directory Security Settings, etc.

Left by André at 4/20/2007 11:47 AM
Gravatar
A better way to update the web.config is using the class System.Configuration.Configuration webConfig = WebConfigurationManager.OpenWebConfiguration(webConfigVirtualPath);

if (webConfig.AppSettings.Settings.Count > 0)
{
KeyValueConfigurationElement appValue = webConfig.AppSettings.Settings[appKey];

if (appValue != null)
{
appValue.Value = "New Value";

if (!webConfig.AppSettings.SectionInformation.IsLocked)
{
webConfig.Save();//** Configuration updated.
}
else
{
MessageBox.Show("** Could not update, section is locked.");
}
}
}
}


# re: Custom Installer Actions: Edit Connection Strings, IIS Directory Security Settings, etc.

Left by Paulo Cunha at 7/12/2007 3:44 PM
Gravatar
I have the same problem as Marcos.
Did you find a solution?

# Paxil.

Left by Effects of alcoholism and paxil xanax. at 7/31/2007 10:56 PM
Gravatar
Paxil 10mg dose. Paraethesia and paxil. Paxil.

# hot teens

Left by http://hot-teens.ispfaq.info at 12/1/2007 3:40 PM
Gravatar
h88K3t0 | <a href="http://rough-amateur-sex.softcentral.info">rough amateur sex</a>

# re: Custom Installer Actions: Edit Connection Strings, IIS Directory Security Settings, etc.

Left by japorms at 3/5/2008 3:42 AM
Gravatar
the quick brown fox jumps over the lazy dog.
the quick brown fox jumps over the lazy dog.
the quick brown fox jumps over the lazy dog.
the quick brown fox jumps over the lazy dog.
the quick brown fox jumps over the lazy dog.
the quick brown fox jumps over the lazy dog.
the quick brown fox jumps over the lazy dog.

Your comment:



 (will not be displayed)


 
 
 
Please add 7 and 5 and type the answer here:
 

Live Comment Preview:

 
«March»
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910