Can you do a web.config transforms on another file like connections.config, nlog.config, app.config in the same way? I can't seem to find an answer.
<connectionStrings xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<add name="ApplicationServices" connectionString="Data Source=ConnectionString" xdt:Transform="SetAttributes(mode)"/>
</connectionStrings>
I have created a Visual Studio add in for just this. It's called SlowCheetah and you can find it at http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5.
Related
Our web.config has this section:
<connectionStrings configSource="connections.config" />
Our connections.config looks (something) like this:
<connectionStrings>
<add name="conn" connectionString="Data Source=source;Initial Catalog=db;Trusted_connection=True" />
</connectionStrings>
I think the reason we do this is to make it easier for our clients to change the database configuration. But this means that connections.config is checked into source control, and as a developer, I have to change it every time I am developing, and remember not to check those changes in.
As a result, I would like to use the configSource attribute again in connections.config, something like this:
<connectionStrings configSource="connections.local.config">
<add name="conn" connectionString="Data Source=source;Initial Catalog=db;Trusted_connection=True" />
</connectionStrings>
But when I do this, I get the error:
Parser Error Message: Unrecognized attribute 'configSource'. Note that
attribute names are case-sensitive.
Is it not possible to do this? It seems odd that the configSource attribute would only work in web.config itself.
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.
Ok, I am using an external config file for my connection strings so each individual developer can have their own strings while developing. Normally, each dev has a different environment but we all publish to the same servers via web.release.config transforms.
However, when I publish from VS, it's not transforming from the web.release.config for the conn strings. I think it's because if you have the configSource attribute set to use an external config it ignores the transform.
Here's my web.config:
<connectionStrings configSource="userConn.config" />
And here's my userConn.config:
<?xml version="1.0"?>
<connectionStrings>
<add name="DefaultConnection"
providerName="System.Data.SqlClient"
connectionString="Data Source=XXXX;Initial Catalog=XXXX;user id=XXXX;password=XXXX;" />
<add name="ExtVariablesEntities"
providerName="System.Data.EntityClient" connectionString="metadata=res://*/Models.XXXX.csdl|res://*/Models.XXXX.ssdl|res://*/Models.ExtVariables.msl;provider=System.Data.SqlClient;provider connection string="data source=XXXX;initial catalog=XXXX;user id=XXXX;password=XXXX;MultipleActiveResultSets=True;App=EntityFramework;"" />
</connectionStrings>
After publishing, opening the actual web.config that made it to the server, it still has:
<connectionStrings configSource="userConn.config" />
Is there a workaround for this? I've had this setup before I just don't remember what the trick is.
Just an FYI that I was able to solve this by following this article: https://jshowers.com/simple-web-config-transforms-for-configuration-elements-that-use-configsource-and-external-files/
You end up with 3 custom config files - whatever you want to call them for dev, test and prod lets say. Then you will have 3 web config files dev, test and prod (these are your web config transform files) where you simply say:
<appSettings xdt:Transform="Replace" configSource="path.to.custom.config.file.depending.on.env">
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"
I was wondering why can't I use a custom environment variable in an ASP.NET web.config file like so?
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="ConnectionName" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename='%MyProjectsFolder%\WebAppName\App_Data\Database1.mdf';User Instance=true" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
I checked through Start -> Run that the file path to .mdf file is valid.
When I run my C# code to connect to database I get the following error:
An attempt to attach an auto-named database for file
%MyProjectsFolder%\WebAppName\App_Data\Database1.mdf failed. A
database with the same name exists, or specified file cannot be
opened, or it is located on UNC share.
Environment variables may not be used in a config file.
You can use: DataDirectory like this:
“Data Source = |DataDirectory|\Mydb.sdf”
And you can change where DataDirectory points: "To set the DataDirectory property, call the AppDomain.SetData"
http://social.msdn.microsoft.com/Forums/en/sqlce/thread/dc31ea59-5718-49b6-9f1f-7039da425296
Caveat, I've never tried this.
I don't suppose you've tried:
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename='~/App_Data/Database1.mdf';User Instance=true"