Search code examples
asp.netdeploymentmsbuildmsdeployweb-publishing

How to override ASP.NET deployment parameters when publishing


I've seen some questions, read all the posts, still I can't get this done. Either I'm missing something, or it cannot be done..

So I have a rather simple (ASP.NET MVC) project. In the root of my project there is a parameters.xml file with parameter definitions, such as:

<?xml version="1.0" encoding="utf-8" ?>
<parameters>
  <parameter 
   name="Realm"
   description="Realm for ADFS authentication"
   defaultValue="http://somewebsite">
   <parameterEntry 
      kind="XmlFile"
      scope="Web.config"
      match="/configuration/appSettings/add[@key='ida:Realm']/@value" />
   <parameterEntry 
      kind="XmlFile" 
      scope="Web.config"
      match="/configuration/system.identityModel/identityConfiguration/audienceUris/add/@value" />
<parameterEntry 
      kind="XmlFile" 
      scope="Web.config"
      match="/configuration/system.identityModel.services/federationConfiguration/wsFederation/@realm" />

So far so good. This structure is very well understood, you a name of a deployment parameter (e.g. "Realm") a default value for it, and a few XCLT options where it can be found in the Web.config file.

Now when I create a deployment package, by running Publish, I get a directory that contains:

  1. An auto generated cmd file for the deployment
  2. A readme file
  3. A manifest XML file
  4. A zip file with all the files that have to be deployed to my server.
  5. A .SetParameters.xml file

That last file contains the parameter values which were set in the parameters.xml file I quoted above, and those values are set to be the default ones which were defined there.
e.g. if the default value for Realm was set to be http://somewebsite, in the generated deployment SetParameters.xml, I will get this record:

<setParameter name="Realm" value="http://somewebsite" />

When the package gets deployed, these values will be taken from this xml file and replace the values in my Web.Config, according to the XPaths defined in the original parameter.xml file.

Now, what I want is to override this parameters (as well as many others) when I build my deployment package, because I need different values for different deployment profiles.

The key seems to be my .pubxml file, which is in charge of the publishing settings. I tried many options, including what Sayed Hashimi said here, previous answers given here and here, here too (and others too)... nothing seems to make a difference for the damn parameters!

Am I missing something? Is there some magic hack?
Should I ignore the params altogether and just rely on a matching web.config transformation? Any other advice? Maybe add custom targets to the .csproj file? (even tried that...)

Thank you! Alon.


Solution

  • WebDeploy Parameterization occurs at deploy time not build time (like config transforms). We use Parameterization for 50+ products at work with great success. For each product we have 4 SetParameters files:

    • SetParameters.DEV.xml
    • SetParameters.QA.xml
    • SetParameters.UAT.xml
    • SetParameters.PROD.xml

    Each has the parameter values which are valid for its environment. We use a PowerShell script and Thoughtworks GO to execute the MSDeploy the package in a pipeline with the appropriate SetParameters file.

    One note, make sure to not deploy your SetParameters files with your application - http://dotnetcatch.com/2016/04/02/webdeploy-parameterization-tip-dont-publish-your-parameterization-files/

    Also, if you want to preview your parameterization in VS this may be helpful - http://dotnetcatch.com/2014/09/08/parameterizationpreview-visual-studio-extension/