Question

Currently my web.config looks something like this:

<configuration>
  <connectionStrings configSource="connectionstrings.config"/>
</configuration>

When generating a deployment package (Msbuild.exe + target=Package) it does not 'magically' parameterize my connectionstrings to the parameters.xml file for substitution on deployment.

When i inline my connectionstrings everything is fine and parameters are generated for my connectionstrings..

So:

How can I copy the contents of connectionstrings.config as replacement of my <... configsource="xxx"/> on deployment trough a web.config transformation?

EDIT: I have accepted the answer of Sayed Ibrahim because the 'default' behaviour is really nice (automatic parameterization of connectionstrings in web.config) But in the end its better to specify exactly which stuff needs to be parameterized (either via {projectname}.wpp.targets or parameters.xml file).

Was it helpful?

Solution

You are kind of asking the wrong question, don't worry about how to transform back into web.config. Instead just create parameters for the connection strings in the file where they should be. I just blogged this at http://sedodream.com/2012/05/13/VSWebPublishHowToParameterizeConnectionStringsOutsideOfWebconfig.aspx. I've also pasted the content below for you.

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.

<connectionStrings configSource="connectionStrings.config" />

And here is connectionStrings.config

<connectionStrings>
  <clear/>
  <add name="ApplicationServices"
       connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
       providerName="System.Data.SqlClient" />
  <add name="OtherConnectionString"
       connectionString="data source=.\SQLExpress;Integrated Security=SSPI;Initial Catalog=foo"
       providerName="System.Data.SqlClient"/>
</connectionStrings>

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.

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <ItemGroup>
    <!-- Here we need to declare MSDeploy parameters for connection strings in connectionStrings.config -->
    <MsDeployDeclareParameters Include="ApplicationServices-ConnectionString" >
      <Kind>XmlFile</Kind>
      <Scope>connectionStrings.config$</Scope>
      <Match>/connectionStrings/add[@name='ApplicationServices']/@connectionString</Match>
      <Description>Connection string for ApplicationServices</Description>
      <DefaultValue>data source=(localhost);Initial Catalog=AppServices</DefaultValue>
      <Tags>SqlConnectionString</Tags>
    </MsDeployDeclareParameters>

    <MsDeployDeclareParameters Include="OtherConnectionString-ConnectionString" >
      <Kind>XmlFile</Kind>
      <Scope>connectionStrings.config$</Scope>
      <Match>/connectionStrings/add[@name='OtherConnectionString']/@connectionString</Match>
      <Description>Connection string for OtherConnectionString</Description>
      <DefaultValue>data source=(localhost);Initial Catalog=OtherDb</DefaultValue>
      <Tags>SqlConnectionString</Tags>
    </MsDeployDeclareParameters>
  </ItemGroup>

</Project>

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. enter image description here

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top