Renaming Web.config to run transforms breaks nuget

2019-09-02 09:43发布

问题:

I have a simple scenario where I have my Web.config and then a Web.Release.config that I want to use to transform the Web.config when I deploy to production. Transforms only automatically run if you publish within VS, and I am not using VS for deployment, I'm using outside source control along with cruise control to build and deploy.

I've seen several solutions to this problem:

1) use a base web.config file (Web.base.config) so that the Web.config file can be overwritten during the build process by modifying the project file. 2) install an extension that handles this 3) within cruise control import the targets and essentially do the same thing as #1 manually running the transform, but this still requires a base config file separate from the destination web.config

The issue with all of these is that I have to rename my web.config (eg. to Web.base.config) or I have to move the web config to a different directory so that once the transform occurs it doesn't overwrite my base (original) web.config.

If I do this, then when I install/update any nuget packages that modify the web.config, it will add the changes to Web.config which will be overwritten everytime the transform occurs, when the changes really need to be applied to the base web config.

I've searched everywhere and noone has mentioned this as an issue. Am I missing something? I'm hoping someone can offer a solution that doesn't require the original Web.config filename/location to change.

回答1:

It's because transformation task itself locks web.config.

I believe the simplest solution would be to use copy task and copy your config to web.config.copy before transformation. Then transform your web.config.copy to web.config.transformed and then just copy it back into web.config.

Copy task can overwrite readonly so you shouldn't have any problems with source-controlled files