Web.config Build vs conversion does not work

I have an ASP.NET web application project that connects to a remote database through the Entity Framework. During debugging (for example, when starting a project on my local computer), the IP address in the database is different from when it was released (for example, after loading a project on my web server and launching it from a browser). Until now, I have always manually changed the database connection string in the Web.config file to switch between the two (basically I had to use connection strings, one with the name “Debug” and one with “Release”), and I just exchanged names when every deployment).

Now, I just noticed that you can let this happen automatically using the Web.config conversion syntax, where you put the changed connection string in the version of Web.Release.config, and it should then use it when building the DLL in the release configuration.

However, this does not seem to work for me ...

Here is the relevant part of my regular Web.config file (which contains the Debug connection string for local use):

<?xml version="1.0"?> <configuration> <connectionStrings> <!-- Debug connection string. Release connection string is in Web.Release.config file --> <add name="DatabaseEntities" connectionString="A" providerName="System.Data.EntityClient" /> </connectionStrings> </configuration> 

Here is the Web.Release.config file, which, according to the examples, should replace the connection string "DatabaseEntities" "A" with "B" if the DLL is in Release mode:

 <?xml version="1.0"?> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <!-- Replace the DatabaseEntities connection string with the Release version (local IP address) --> <connectionStrings> <add name="DatabaseEntities" connectionString="B" xdt:Transform="Replace" xdt:Locator="Match(name)"/> </connectionStrings> </configuration> 

(Obviously, “A” and “B” are just placeholders for my real join strings)

When I debug the application (for example, just press F5), the default is Web.config and I can access the database. Then I change the build configuration to Release through Configuration Manager. All projects in the solution are configured to release the configuration. Then I build the solution (simply through Build or even through a complete rebuild (e.g. Clean, Rebuild)). I upload the newly created DLLs to the web server, as well as the Web.config and Web.Release.config files, and when I try to access the database, I cannot, it is still trying to access the database via IP debug address. and therefore cannot find it ...

The Web.Release.config file seems to be completely ignored, or at least the connection string is not replaced.

What am I doing wrong? Is the conversion syntax incorrect? Am I not correctly building the application in Release mode?

+29
build web-config release web-config-transform
Jan 19 '13 at 15:02
source share
7 answers

Then I create a solution (just through Build or even through a complete rebuild (for example, clean, rebuild)). I load the newly created web server DLLs, as well as the Web.config and Web.Release.config files

Your error has occurred: web configuration conversions will not work for your local environment if you just create one. You need to publish.

The deployment process seems strange: you only copy the DLL, Web.config and web.Release.config. It seems to me that you are copying the source code, not the compiled application. Published WebApplication does not contain web.release.config.

You must publish your project (rightclick on the WebApplication → Publish) to your local file system and copy the files there or use a different deployment method of your choice.

2 years ago I wrote an article about web.config transformations. It provides a step-by-step guide for VS 2010 (a publication dialog published in VS 2012): http://www.tomot.de/en-us/article/5/asp.net/how-to-use-web.config-transforms- to-replace-appsettings-and-connectionstrings

+51
Jan 19 '13 at 16:03
source share

You can try the Slow Cheetah plugin:

http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5

This will allow you to see live broadcasts by providing you with an additional context menu option. Right-click and select Preview Transform to see the transformation without the need for assembly. Its also very convenient for implementing transformations app.config

+3
Jun 12 '14 at 8:27
source share

I thought the conversion only happens when the site / application is published. This is not done when creating the application. The latter constantly changed web.config under source control (which would be a real problem)

+3
Aug 14 '14 at 6:47
source share

If these are only connection strings that are not overwritten when converting web.config, then this is what I did: I cleared the "Use this connection string at runtime" checkbox in the Settings section of the Publish to Internet wizard. This parameter overwrite the web.config conversion of the connection string.

+1
May 04 '15 at 19:20
source share

Inside your csproj file, you can add an action that will be performed before each build and perform web.config conversions:

 <Target Name="BeforeBuild"> <TransformXml Source="web.config" Transform="web.$(Configuration).config" Destination="web.config" /> </Target> 
+1
Sep 21 '18 at 9:01
source share

This is quite flexible, you should be able to make several settings for applying custom transformations during assembly (and without having to publish)

We implemented this in our (Windows Service) project, applying transformations during assembly

You will need to modify your project file and add something similar to below

Here we tell msbuild to apply the conversion after compilation is complete, but only if the condition is true (see https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-conditions?view=vs-2017 ).

Please note that we use the assembly support (self-defined msbuild option) "Env", for example, msbuild.../p:Env=Prod will lead to App.Prod.config

 <UsingTask TaskName="TransformXml" AssemblyFile="C:\Some\Path\Microsoft.Web.Publishing.Tasks.dll" /> <Target Name="AfterCompile" Condition="Exists('some condition')"> <!--Generate transformed app config in the intermediate directory--> <TransformXml Source="App.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="App.$(Env).config" /> <!--Force build process to use the transformed configuration file from now on.--> <ItemGroup> <AppConfigWithTargetPath Remove="App.config" /> <AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config"> <TargetPath>$(TargetFileName).config</TargetPath> </AppConfigWithTargetPath> </ItemGroup> </Target> 
0
Oct 24 '18 at 17:53
source share

To make the conversion work in development (using F5 or CTRL + F5), try this extension:

https://github.com/Microsoft/slow-cheetah

0
Jun 25 '19 at 14:12
source share



All Articles