How to automatically change the connection string when creating with Nant

Is it possible to change the connection string in my web.config automatically when the build type is released using Nant? if so, how? thanks

+8
nant
source share
4 answers

I think you could use the xmlpoke task. For example, if your web.config

<?xml version="1.0"?> <configuration> <connectionStrings> <add name="myDb" connectionString="blah" providerName="blah"/> </connectionStrings> </configuration> 

Then you can add the task to your build file like this.

 <xmlpoke file="path_to_your_web_root\Web.config" xpath="/configuration/connectionStrings/add[@name='myDb']/@connectionString" value="your_connection_string" /> 

Oh, here is the xmlpoke task documentation. http://nant.sourceforge.net/release/latest/help/tasks/xmlpoke.html

+18
source share

I assume that you want to do this so that the connection string points to the production environment, and not to the development or testing environment when Nant builds the release code. I usually have a different approach to solving this scenario; save the connection strings in a separate file. You can do this using the configSource attribute:

 <!-- point out a file containing the connectionStrings config section --> <connectionStrings configSource="connections.config"></connectionStrings> 

The connections.config file should look something like this:

 <?xml version="1.0"?> <connectionStrings> <add name="myDb" connectionString="{your connection string}"/> </connectionStrings> 

Since connection strings rarely change in a production environment, then the connections.config file can usually be excluded from deployment.

+5
source share

Another alternative is to use a template in which you have a token instead of a connection string, for example.

 <connectionString> <add name="myDb" connectionString="@CONNECTION_STRING@" /> </connectionStrings> 

Then use filters to replace them with the appropriate string.

 <copy file="Web.config.template" tofile="Web.config" overwrite="true"> <filterchain> <replacetokens> <token key="CONNECTION_STRING" value="${ConnectionString}" /> </replacetokens> </filterchain> </copy> 

The value of the ConnectionString property will change depending on the type of assembly.

Filter chains are described in the Nant Documentation.

+2
source share

I am using a simple method. I prepared many versions of the configuration file, each of which contains its own connection strings (DEV, TEST, PRODUCTION). In a NANT script, when I create different targets, I copy a specific configuration file to overwrite it by default.

+1
source share

Source: https://habr.com/ru/post/639903/


All Articles