Web.config Build vs Release transform not working

佐手、 提交于 2020-01-09 10:35:07

问题


I have an ASP.NET Web Application project that connects to a remote database via the Entity Framework. During debugging (eg running the project on my local computer), the IP address to the database is different than during release (eg after uploading the project to my webserver and running it from the 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 connection strings, one named 'Debug' and one 'Release' and I just swapped around the names whenever I deployed).

Now I just noticed that it should be possible to let this happen automatically via the Web.config Transformation Syntax where you put the modified connection string in the Web.Release.config version and it should then use that when the DLL is built under Release configuration.

However it does not seem to work for me...

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

<?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 'DatabaseEntities' connection string "A" with "B" if the DLL is under 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 place-holders for my real connection strings)

When I debug the application (e.g. just press F5) the default Web.config is used and I can access the database. I then change the build configuration to Release via the Configuration Manager. All the projects in the solution are set to Release configuration. Then I Build the solution (just via Build or even via a complete rebuild (e.g. Clean, Rebuild)). I upload the newly built DLLs to the webserver, as well as the Web.config and Web.Release.config files, and when I try to access the database I am unable, it is still trying to access the database via the debug IP address and hence cannot find it...

It seems the Web.Release.config file is completely ignored, or at least the connection string is not being replaced.

What am I doing wrong? Is the transformation syntax wrong? Am I not building the application under Release mode correctly?


回答1:


Then I Build the solution (just via Build or even via a complete rebuild (e.g. Clean, Rebuild)). I upload the newly built DLLs to the webserver, as well as the Web.config and Web.Release.config files

There is your error: Web config transforms won't work for your local environment, if you simply build. You need to publish.

Your deployment process seems weird: You are only copying DLLs, Web.config and web.Release.config. To me it seems, that you copy your source code and not a compiled application. A published WebApplication doesn't contain a web.release.config.

You should publish your project (rightclick on your WebApplication -> Publish) to your local filesystem and copy the files from there, or use another deployment method of your choice.

2 years ago I wrote an article about web.config transforms. It gives you a step-by-step tutorial for VS 2010 (The publish dialog changed in VS 2012): http://www.tomot.de/en-us/article/5/asp.net/how-to-use-web.config-transforms-to-replace-appsettings-and-connectionstrings




回答2:


You can try the Slow Cheetah plugin:

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

This will let you see the transformations 'live' by giving you an extra context menu option. Right-click and choose Preview Transform to see the transformation without having to do a build. Its also really handy for implementing app.config transformations




回答3:


I thought transformation is only done when you publish the site/app. It is not done when building an application. The latter would constantly change the web.config under source control (which would be a real hassle)




回答4:


If it's only connections strings that are not overwritten during web.config transformation, then this is what I did: I cleared the "Use this connection string at runtime" check-box in the "Settings" section of "Publish Web" wizard. This setting was overwriting web.config transformation of the connection string.




回答5:


Inside your csproj file, you can add an action to execute before every build and perform the web.config transformations:

<Target Name="BeforeBuild">
    <TransformXml Source="web.config" Transform="web.$(Configuration).config" Destination="web.config" />
</Target>



回答6:


It's pretty flexible, you should be able to make a few tweaks to apply custom transforms on build (and without having to publish)

We implemented this in our (Windows Service) project, applying transforms on build

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

Here we're telling msbuild to apply transform after finish compiling, but only if condition is true (see https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-conditions?view=vs-2017)

Notice we are using an build prop (self defined msbuild prop) "Env", e.g. msbuild ... /p:Env=Prod would result in 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>


来源:https://stackoverflow.com/questions/14415614/web-config-build-vs-release-transform-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!