Warning: What you see below feels hacky to me, but if you find it useful then use it
I have heard a lot of questions and confusion regarding web.debug.config and web.release.config. For example here is just one question on StackOverflow. The question states:
Hello, I want to use the web.config transformation that works fine for publish also for debugging. When i publish a web app, visual studio automatically transforms the web.config based on my
current build configuration. How can i tell visual studio
to do the same when i start debugging. On debug start it simply
uses the default web.config without transformation. Any idea?
First let me explain, as I did to that question, the purpose of the files: web.config/web.debug.config/web.release.config.
web.config
This is the config file which developers should use locally. Ideally you should get this to be standardized. For instance you could use localhost for DB strings, and what not. You should strive for this to work on dev machines without changes.
web.debug.config
This is the transform that is applied when you publish your application to the development staging environment. This would make changes to the web.config which are required for the target environment.
web.release.config
This is the transform that is applied when you publish your application to the "production" environment. Obviously you'll have to be careful with passwords depending on your application/team.
The problem with transforming the web.config that you are currently running is that a transform can perform destructive actions to the web.config. For example it may delete a attributes, delete elements, etc.
Resolution
Ok, with that out the way not let’s see how we can enable what the question asker wants to do. To recap, when he builds on a particular configuration he wants a specific transform to be applied to web.config. So obviously you do not want to maintain a web.config file, because it is going to be overwritten. So what we need to do is to create a new file web.template.config, which is just a copy of web.config. Then just delete web.config by using Windows Explorer (don’t delete using Visual Studio because we do not want to delete it from the project). Note: If you are using a source control provider which is integrated into Visual Studio then you probably want to delete web.config from source control. Also with this we do not want to use web.debug.config or web.release.config because these already have a well defined role in the Web Publishing Pipeline so we do not want to disturb that. So instead we will create two new files, in the same folder as the project and web.template.config, web.dev.debug.config and web.dev.release.config. The ideas is that these will be the transforms applied when you debug, or run, your application from Visual Studio. Now we need to hook into the build/package/publish process to get this all wired up. With Web Application Projects (WAP) there is an extensibility point that you can create a project file in the same folder with the name {ProjectName}.wpp.targets where {ProjectName} is the name of the project. If this file is on disk in the same folder as the WAP then it will automatically be imported into the project file. So I have created this file. And I have placed the following content:
$(PrepareForRunDependsOn); UpdateWebConfigBeforeRun;
Let me explain this a bit. I have created the CopyWebTemplateConfig target which will always copy web.template.config to web.config on build, even if you are not debugging your application in Visual Studio. This is needed because we still need to support the package/publish process of Visual Studio. Then I extended the property PrepareForRunDependsOn to include the UpdateWebConfigBeforeRun target. This property is used to identify the list of targets which needs to be executed before any managed project is run from Visual Studio. In this target I am using the TransformXml task to transform web.template.config, using the correct web.dev.***.config file. After that your app starts up using the correct web.config based on your build configuration.
After that I have another target ExcludeCustomConfigTransformsFiles, which I inject into the package/publish process via the attribute BeforeTargets=”ExcludeFilesFromPackage”. This is needed because we do not want these files to be included when the application is packaged or published.
So that is really all there is to it. To explain the package/publish process a bit more for this scenario. When you package/publish web.debug.config or web.release.config, depending on build configuration, will still be used. But ultimately the file that it is transforming is web.template.config, so you may have to adjust depending on what you have in that file. Questions/Comments?
Sayed Ibrahim Hashimi - @sayedihashimi
Thanks in advance.
I added to the project the Web.dev.***.config files, Web.template.config and the {ProjectName}.wpp.targets. I don't know if there is a step I forgot, I tried to do all the process twice, but with the same results.
The content of Web.dev.***.config files has to be the same as Web.***.config files? Is there any change not explained to do?
Thanks in advance.
***
I followed the instructions here but it doesn't seem to work unless I copy the exact config I need (sans transform stuff) into the web.template.config. Any ideas why this is happening (or how to verify which of my web.dev.*.config files is being used to transform? It is as if the UpdateWebConfigBeforeRun target is either not running or is failing.
***
Thanks for Slowcheetah and this tip. I also needed to let VS use the transformed config file after a build/F5. Maybe it's a good addition to Slowcheetah so that we can use it more easily without altering any build file. I must say I'm not all that good with msbuild and it seems most developers are not really fluent in this. This approach does not sound hacky, I think its the simplest solution.
The normal transform during publish kind of works, it creates a web.template.xml with the values from transform xml, but it doesn't create a web.config with the expected values.
The build transform doesn't work at all. Can you help out?
Removing the target it works.
Comments are closed.