Publish is not transforming web.config? - asp.net

I made a web.config (full file, it doesn't show XML errors)
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<configSections>
...
<location path="." inheritInChildApplications="false">
<connectionStrings>
<add name="ElmahLog" connectionString="data source=~/App_Data/Error.db" />
<add name="database" connectionString="w" providerName="System.Data.EntityClient"/>
</connectionStrings>
</location>
...
with a transform file (web.Staging.config)
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="database"
connectionString="c"
providerName="System.Data.EntityClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
</connectionStrings>
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<customErrors defaultRedirect="error.aspx"
mode="RemoteOnly" xdt:Transform="Replace">
</customErrors>
</system.web>
</configuration>
I am publishing in Staging mode (right click website > Publish > Method: File System ...)
------ Build started: Project: Drawing, Configuration: Staging Any CPU ------
Drawing -> D:\Project\bin\Staging\Drawing.dll
------ Build started: Project: MySystem, Configuration: Staging Any CPU ------
MySystem -> D:\Project\bin\Staging\MySystem.dll
...
But when I look at the web.config in the output folder it isn't changed.
I found the following on the Build log:
D:\Project\Web.Staging.config(3,2): Warning : No element in the source document matches '/configuration'
D:\Project\Web.Staging.config(3,2): Warning : No element in the source document matches '/configuration'
D:\Project\Web.Staging.config(3,2): Warning : No element in the source document matches '/configuration'
Transformed web.config using Web.Staging.config into obj\Staging\TransformWebConfig\transformed\web.config.
What could be the problem? Am I doing this right?

Answering late but perhaps I can save someone a headache. In Visual Studio 2013, there are two places to select configuration for your build and deploy. The Configuration Manager and then again with Publish Web where the third step in the Wizard entitled Settings allows you to select Config you want to use. If you don't select your new configuration it will use the transform for the selected configuration instead of yours.

I found out two things:
You cannot set a namespace on the <configuration> tag (ex: for <location path="." inheritInChildApplications="false">)
You have to watch for the correct hierarchy in the transform file.
Like
<configuration>
<location>
<connectionStrings>
Instead of
<configuration>
<connectionStrings>

Ensure that in the properties of the Web.Config file Build Action is set to Content.
If the build action is set to None, it will not be transformed, even if it is being copied to the output directory.

Make sure to include InsertIfMissing if the section you are trying to add does not already appear in the output.
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<location>
<system.webServer>
<security xdt:Transform="InsertIfMissing">
<requestFiltering allowDoubleEscaping="true" />
</security>
</system.webServer>
</location>
</configuration>

Don't forget to copy all the other attributes of "configuration" from the original "web.config", as it seems that VS2012 doesn't do it automatically and of course there will be no match...

Answering late as well, but this may help someone.
I realized that if you have two websites in the same solution, when you try to publish one of them the transformation might not work if you have one only configuration for both projects.
One of my websites was always transforming, but the other sometimes was and sometimes wasn't.
For example, I had the configuration "Auto" in the solution, and had web.Auto.config for both websites.
I resolved that by creating a new configuration with a different name - "AutoAdmin" - creating also its web.AutoAdmin.config file for the second project, and when I published it again the transformation finally occurred.

I followed the below steps to fix this issue. Thanks, #michaelhawkins for pointing in the right direction. You need to make sure you change the configuration to release in two places.
And right click on your project and select "Properties". IF not working try selecting x86 in CPU Architecture

#Karthikeyan VK your post resolved my issue. Although I was selecting Production configuration in my publish profile, in configuration manager it was set to dev therefore It didn't transform my settings.
Microsoft needs to fix this bug. Once you pick a configuration in the publishing profile it should automatically update the configuration manager as well.

Related

dotnet restore NuGet.Config environment variables for ClearTextPassword doesn't work

To do CI I'd like to checkin a NuGet.Config file with my own nuget feed (an VSTS one). Like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="myfeed" value="https://xxx.pkgs.visualstudio.com/_packaging/myfeed/nuget/v3/index.json" />
</packageSources>
<packageSourceCredentials>
<myfeed>
<add key="ClearTextPassword" value="%PASSWD%" />
</myfeed>
</packageSourceCredentials>
</configuration>
Because I don't want to check in the password, I read you can use Environment Variables in this configuration. I tried it and it works in the PackageSources value. But it seems it doesn't work in the PackageSourceCredentials like shown above.
Does someone has a solution for this?
I had to add the key Username to the PackageSourceCredentials. I thought it was of no use. But it cached the first time I removed it and later I added the env var and after this it broke.

Web.Config Transformation Not Working

Using VS 2k15, ASP.NET 4.5
My Transform is not working. I was looking in preview and the files are the same. Then I figured maybe I had to deploy to see transformation. So I set up a custom Deploy to my desktop, using the Deploy Configuration. Checked the web.config after publishing that, still matching the original.
Any idea what I'm doing wrong?
I know when I do the preview I get a warning at the top of the preview that says..."
These files have different encodings. Left file: Unicode (UTF-8) without signature. Right file: Unicode (UTF-8) with signature. You can resolve the difference by saving the right file with the encoding Unicode (UTF-8) without signature.
However, when I choose Save Options and save the Deploy file with no signature (so they're matching) the message still comes up. Just not sure what's going on. Any ideas why I can't this transform to work?
Web.Config
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings>
<add key="fileLibrary" value="c:\vsoProjects\localFiles\rlFileLibrary" />
</appSettings>
<connectionStrings>
<add name="appConnString" connectionString="Data Source=(LocalDb);Initial Catalog=DevDB;User ID=*****;Password=*****;Connect Timeout=300" />
</connectionStrings>
</configuration>
Web.ContDeploy.Config
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="fileLibrary" value="R:\rlFileLibrary" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
</appSettings>
<connectionStrings>
<add name="appConnString" connectionString="Data Source=myserver.test.com;Initial Catalog=DeployDB;User ID=*****;Password=*****;Connect Timeout=300" xdt:Transform="SetAttributes(connectionString)" xdt:Locator="Match(name)"/>
</connectionStrings>
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>
</configuration>
I'm not an expert on the transform, but my (working) transform for the appSettings key has this signature:
<appSettings>
<add key="datafolder" value="D:\sites\removedpath\App_Data\" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
</appSettings>
which is slightly different than yours
I found the problem. This project was converted a while back from asp.net 2.x to 4.5.
The web.config still had an attribute up in the configuration section.
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
...
</configuration>
You can see it up top in the code I posted. I didn't realize it was still there. After all that time banging my head against my desk I just removed that attribute, preview transform, and voila it was there! Also deploys properly as well.

How to keep private data separate in an open source ASP.NET MVC application

In an open source ASP.NET application I'm working on, I need to keep certain data in the configuration file private, while still keeping it easy for people to build and debug it on their own machine. This is data such as API keys, Mail Settings, etc..
How would I keep this data separate and out of the git repository while still allowing people to just pull and build without having to set up a bunch of stuff?
In your config file you can define configSource:
<configuration>
<appSettings configSource="filepath1.config" />
<connectionStrings configSource="filepath2.config" />
<!--etc-->
</configuration>
Put the configurations that you need to keep private in a separate config file, then exclude them in your .gitignore.
Keep in mind that this will ignore the whole section and overwrite it with the context you have in the referenced file.
You can also do Configuration Transform, which allows you to only overwrite a small set of variables in sections. For example:
In your main Web.config:
<configuration>
<appSettings>
<add key="Key1" value="Something I dont't Care"/>
<add key="Key2" value="Something dummy"/>
</appSettings>
</configuration>
And in your Web.Release.config:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="Key2" value="Something I want to keep secret"
xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
</configuration>
In this case the "Key2" value that you want to keep private will be in a separate file, and you can exclude the Web.Release.config through .gitignore.
Also there's another approach that I never tried, which can also overwrite config using external file.

How to set debug false for release mode

I have this web.config file with the compilation option set as below
Web.config
<configuration>
...
<system.web>
<compilation debug="true" targetFramework="4.5" />
...
</system.web>
</configuration>
And here is what Visual Studio puts for release mode by default.
Web.Release.config
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>
</configuration>
I am using this for MVC4 project. Based on this tutorial, I was expecting that minified versions of js and css would be served, when the application is run under Release Mode. But this doesn't seem to be working and non-minified versions of js and css are being served. On the other hand, if I explicitly set debug to false in web.config, then the min versions are served correctly.
It seems like compilation tag Transform issue when the application is run under Release Mode, but I don't understand what's wrong with the same in Web.Release.config.
In short, I am unable to get bundling and minification working, by running application under Release Mode.
Web.config transformations as they are defined in the Web.Release.config are only done when deploying/publishing the project for the relevant configuration.
Just changing the active configuration in Visual Studio to Release and running the application does not run the transformations. Therefore, the web.config remains unchanged. This behavior is reasonable by the way as a web application is run from the project directory that contains the original web.config. If Visual Studio were to transform the web.consign, your original web.config would be changed.
If you haven't created a deployment profile yet, you can publish your application to the file system to verify the behavior. Choose Release as the configuration to run the deployment for. The transformations should be executed as expected.
My answer might be late, however this what worked with me:
I've changed the line :
<compilation xdt:Transform="RemoveAttributes(debug)" />
to :
<compilation xdt:Transform="Replace" debug="false" targetFramework="4.5" />
this basically did the trick, as I think the optimizer is looking for Debug value to be be present and == "false".
Hope this helps people who don't want to manage this from code.
If you'd like to test your .NET bundling and minification that you've got set up in your Global.asax file, you can also use precompilation notation...for example
#if DEBUG
BundleTable.EnableOptimizations = false;
#else
BundleTable.EnableOptimizations = true;
#endif
With this, your application won't need to trigger the transform in the build operation and will run just like you want it to.
Possibly an improvement to Omar.Alani's answer:
In the Release transform, replace the line:
<compilation xdt:Transform="RemoveAttributes(debug)" />
with the following:
<compilation debug="false" xdt:Transform="SetAttributes" />
compilation debug="false" xdt:Transform="SetAttributes"
comes in handy when you are activating XML transformations at release azure deployment time to make configuration transformations specific to a target environment.
When you check XML transform at azure deployment time, the following sequence is done:
Apply web.release.config on web.config
Apply web.stageName.config on modified web.config.
But web.release.config has already been applied on web.base.config so if
compilation xdt:Transform="RemoveAttributes(debug)"
is used in web.release.config file, the web.config has already debug attribute been removed at compilation time and when it deploys it transform again with web.release.config, trying to apply the above command but it fails.
Removing debug attribute is sufficient RemoveAttributes(debug).
It will work similar to debug=false.

Can't figure out web.debug.config vs. web.config substituation in VS2010

Can someone point me to what am I doing wrong here?
I'm trying to set up an ASP.NET web app project to compile with two versions of web.config file for Release and Debug builds. So for simplicity sake, here's my web.config:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="WhyMicrosoftSucksSoMuch" connectionString="" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
And then I do the following in web.debug.config:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="WhyMicrosoftSucksSoMuch"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename='C:\FilePath\Database1.mdf';User Instance=true"
providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(connectionString)" />
</connectionStrings>
</configuration>
If I publish it under Debug configuration the resulting web.config looks good, but when I try to run my project from VS2010 also under Debug configuration I get an error when my logic attempts to access database:
The ConnectionString property has not been initialized.
So what's the trick here?
PS. And please don't point me to this document. I tried reading it several times but I get a headache from so much superfluous information. I guess MS doesn't know what brief is.
When running it under debug mode it doesn't apply any transformations.
It only applies them during publishing. You can put your debug connection string in the main web.config, and add your production connection string to the web.release.config
also, you probably will want to use
xdt:Transform="Replace"

Resources