If you have used the Visual Studio web publish in either VS 2010 or VS 11 to create Web Deploy packages then you probably know that we parameterize connection strings in web.config automatically. In case you are not familiar with Web Deploy parameters, they are a way to declare that you want to easily be able to update a value of something when publishing the package later on. Connection strings are good examples of something which typically needs to be updated during publish.
As I previously stated if you create a Web Deploy package in Visual Studio we will automatically create Web Deploy parameters for all your connection strings in web.config. Earlier today I saw a question on StackOverflow asking how to parameterize connection strings in non-web.config files (question actually asked something else, but I think this is what he’s really wanting). I created a sample showing how to do this. Below is what the connectionStrings element looks like in web.config.
And here is connectionStrings.config
In order to parameterize these connection strings you will have to extend the Web Publish Pipeline. To do that create a file named {project-name}.wpp.targets in the root of the project in which you are working (for VS 11 projects you can place all this directly inside of the .pubxml files). This will be an MSBuild file which will get imported into the build/publish process. Below is the file which needs to be created.
XmlFile connectionStrings.config$ /connectionStrings/add[@name='ApplicationServices']/@connectionString Connection string for ApplicationServices data source=(localhost);Initial Catalog=AppServices SqlConnectionString XmlFile connectionStrings.config$ /connectionStrings/add[@name='OtherConnectionString']/@connectionString Connection string for OtherConnectionString data source=(localhost);Initial Catalog=OtherDb SqlConnectionString
Here you can see that I am creating values for MSDeployDeclareParameters. When you package/publish this item list is used to create the MSDeploy parameters. Below is an explanation of the metadata values each contain.
- Kind = for this case it will always be Xmlfile, learn more
- Scope = a regular expression to the file which needs to be modified
- Match = an XPath expression to the attribute/element to be updated
- Description = optional description (this will show up in the IIS manager if the pkg is imported)
- DefaultValue = optional default value for the for the parameter
- Tags = optional, for connection strings use SqlConnectionString
After you create this file you will need to close/re-open VS (it caches imported .targets files). Then you can create a web deploy package. When you do so these new parameters will be declared. In my case I then imported this in the IIS manager and here is the dialog which shows up for the parameters.
As you can see the Application Path parameter is shown there as well as my custom connection string values. When I update the values in the text box and opened connectionStrings.config on my web server they were the values I entered in the dialog box.
FYI I have uploaded this sample to my github account at ParameterizeConStringConfig.
Sayed Ibrahim Hashimi @SayedIHashimi
Comments are closed.