How to deploy different App.config values (with Wi

2019-07-06 10:32发布

问题:

I've been looking for a best practice recommendation on how to deploy an application with a WiX installer for different values in its App.config file. For example.

On my local development machine, I use App.config settings for our test environment:

<configuration>
    <appSettings>
        <WorkingDirectory>C:\Working</WorkingDirectory>
    </appSettings>
    <connectionStrings>
        <add name="ApplicationEntities" 
             connectionString="[TestingConnectionString]" 
             providerName="System.Data.EntityClient" />
    </connectionStrings>
</configuration>

When I deploy to a test environment, those settings are acceptable. However, when we deploy to a production environment, I'd like them to be different. For example:

<configuration>
    <appSettings>
        <WorkingDirectory>\\prodserver\Working</WorkingDirectory>
    </appSettings>
    <connectionStrings>
        <add name="ApplicationEntities" 
             connectionString="[ProductionConnectionString]" 
             providerName="System.Data.EntityClient" />
    </connectionStrings>
</configuration>

The answer to my question may very well be independent of WiX. But just in case, here is my WiX Product.wxs file's relavent fragment:

<Fragment>
    <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
        <Component Id="ProductComponent" Guid="{MY-GUID}">    
            <File Id="Application.exe"
                  Name="Application.exe"
                  Source="..\Application.exe"
                  Vital="yes"
                  KeyPath="yes"
                  DiskId="1" />
            <File Id="Application.exe.config"
                  Name="Application.exe.config"
                  Source="..\Application.exe.config"
                  Vital="yes"
                  KeyPath="no"
                  DiskId="1" />
        </Component>
    </ComponentGroup>
</Fragment>

This setup ends with a manual edit of the App.config on the production server, which invites manual error. What would be a better way to handle this to accommodate an automated deployment?

回答1:

I can think of two options, either deploy the app.config file and edit it using the XmlFile Element, or maintain multiple app.config files each representing your target environment, then deploy the appropriate file.

Here's an exmaple of both options, notice that I copy the file rather than just placing it on the file system. This serves two purposes, firstly you can see by the filename which one has been deployed, secondly if development.app.config and production.app.config are placed in the same location you will get an ICE30 validation error, by copying the file afterwards it avoids this error.

Notice also that I have a condition associated with the component, you'll need to decide how you identify which environment you are deploying to. Some ideas could be to use the machine name, the OU or simply pass it in on the command line as a property.

<Component Id="development.app.config" Guid="*">

    <Condition>DEVELOPMENT</Condition>
    <File Name="development.app.config" KeyPath="yes">
        <CopyFile Id="development.app.config" DestinationName="app.config" />
    </File>

    <util:XmlFile
        Id="WorkingDirectory"
        Action="setValue"
        File="app.config"
        ElementPath="/configuration/appSettings"
        Name="WorkingDirectory"
        Value="C:\Working"
        Permanent="no" />

</Component>

<Component Id="production.app.config" Guid="*">

    <Condition>PRODUCTION</Condition>
    <File Name="production.app.config" KeyPath="yes">
        <CopyFile Id="production.app.config" DestinationName="app.config" />
    </File>

    <util:XmlFile
        Id="WorkingDirectory"
        Action="setValue"
        File="app.config"
        ElementPath="/configuration/appSettings"
        Name="WorkingDirectory"
        Value="\\prodserver\Working"
        Permanent="no" />

</Component>