asp.net web.config transform not working - asp.net

I've made several configurations inside a project for different environments, one is debug and another is release
Web.config:
<configuration>
<connectionStrings>
<add name="InvitationEntity" connectionString="metadata=res://*/Models.InvitationSystem.csdl|res://*/Models.InvitationSystem.ssdl|res://*/Models.InvitationSystem.msl;provider=System.Data.SqlClient;provider connection string="data source=192.168.7.131;initial catalog=Invitation_Debug;persist security info=True;user id=invitation;password=debugMe;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<appSettings>
<add key="FilePath" value="D:\temp"/>
<add key="debugMode" value="Y"/>
</appSettings>
</configuration>
Web.debug.config:
<?xml version="1.0" encoding="utf-8"?>
<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
In the example below, the "SetAttributes" transform will change the value of
"connectionString" to use "ReleaseSQLServer" only when the "Match" locator
finds an attribute "name" that has a value of "MyDB".
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
-->
<system.web>
<!--
In the example below, the "Replace" transform will replace the entire
<customErrors> section of your web.config file.
Note that because there is only one customErrors section under the
<system.web> node, there is no need to use the "xdt:Locator" attribute.
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
-->
</system.web>
<connectionStrings xdt:Transform="Replace">
<add name="InvitationEntity" connectionString="metadata=res://*/Models.InvitationSystem.csdl|res://*/Models.InvitationSystem.ssdl|res://*/Models.InvitationSystem.msl;provider=System.Data.SqlClient;provider connection string="data source=192.168.7.131;initial catalog=Invitation_Debug;persist security info=True;user id=invitation;password=debugMe;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
Web.live.config:
<?xml version="1.0" encoding="utf-8"?>
<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
In the example below, the "SetAttributes" transform will change the value of
"connectionString" to use "ReleaseSQLServer" only when the "Match" locator
finds an attribute "name" that has a value of "MyDB".
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
-->
<system.web>
<!--
In the example below, the "Replace" transform will replace the entire
<customErrors> section of your web.config file.
Note that because there is only one customErrors section under the
<system.web> node, there is no need to use the "xdt:Locator" attribute.
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
-->
</system.web>
<connectionStrings xdt:Transform="Replace">
<add name="InvitationEntity" connectionString="metadata=res://*/Models.InvitationSystem.csdl|res://*/Models.InvitationSystem.ssdl|res://*/Models.InvitationSystem.msl;provider=System.Data.SqlClient;provider connection string="data source=192.168.7.133;initial catalog=Invitation_Debug;persist security info=True;user id=invitation;password=Invi#2014;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
Here's the problem:
Whenever I use debug or release config, the connection string is still the one in web.config instead of that in web.release.config
Is there anything that I did wrong?

It is sometimes better to use item-by-item transformations. Instead of section replacement use
<add name="InvitationEntity" connectionString="metadata=res://*/Models.InvitationSystem.csdl|res://*/Models.InvitationSystem.ssdl|res://*/Models.InvitationSystem.msl;provider=System.Data.SqlClient;provider connection string="data source=192.168.7.133;initial catalog=Invitation_Debug;persist security info=True;user id=invitation;password=Invi#2014;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" xdt:Transform="Replace" xdt:Locator="Match(name)"/>
It is very useful to use this approach when you have lots of attributes in node and need to change only one, you just use SetAttributes instead of Replace for example.

Is your release build setup to use the Web.live.config file? by default Release expects a file named Web.Release.Config I'm just mentioning this because the file is listed as Web.live.config but you are calling it "Release". If it's just a typo and the names are consistent or if your release build configuration is set to "live" then ignore this.

For me, going to properties of the transformation file and changing the property; Copy to Output Directory to Copy Always did the trick. Got a hint from Matt Kogaj's answer

You have to remove property xdt:Transform in ConnectionString Tag. Like below;
<connectionStrings>
<add name="InvitationEntity" connectionString="metadata=res://*/Models.InvitationSystem.csdl|res://*/Models.InvitationSystem.ssdl|res://*/Models.InvitationSystem.msl;provider=System.Data.SqlClient;provider connection string="data source=192.168.7.133;initial catalog=Invitation_Debug;persist security info=True;user id=invitation;password=Invi#2014;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>

Related

How to Update the .config file using Azure DevOps "Update config" Task

I want to Update the Web.config file using Azure DevOps Release Pipeline.
I am using [Update Config][1] Task from MarketPlace.
[1]: https://marketplace.visualstudio.com/items?itemName=digitalmedia34.updateconfig
Here Is the Web.config File
<!-- Some Contents -->
<configuration>
<appSettings>
<add key="Setting1" value="local setting"/>
<add key="CommonSetting" value="local common setting"/>
</appSettings>
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=LocalSQLServer;Initial Catalog=MyReleaseDB;User ID=xxxx;Password=xxxx" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.7.2"/>
<httpRuntime targetFramework1="4.7.2"/>
</system.web>
</configuration>
I am able to change the value of some configuration by adding variables like [Variables]
[2]: https://i.stack.imgur.com/uq85Y.png
My Question is how can I change the values of Nested Property. For Example if I want to change the targetFramework in
<system.web>
<compilation debug="true" targetFramework="4.7.2"/>
<httpRuntime targetFramework1="4.7.2"/>
</system.web>
Please let me know how can I update these values
Thanks in advance
How to Update the .config file using Azure DevOps "Update config" Task
You could use the task Replace Tokens to update the key's values,
the format of variable in .config file is #{TestVersion}#:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="Setting1" value="local setting"/>
<add key="CommonSetting" value="local common setting"/>
</appSettings>
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=LocalSQLServer;Initial Catalog=MyReleaseDB;User ID=xxxx;Password=xxxx" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="#{TestVersion}#"/>
<httpRuntime targetFramework1="#{TestVersion}#"/>
</system.web>
</configuration>
Use Replace Tokens task to update the key's values:
And define the key's values on the Variables.

Transforming Connection String in Web.Release.config

I am trying to use the Web.Release.config file to store an alternative MySQL connection string ready for the live environment, but I just can't seem to get it to work.
In my Web.config I have my local db set up like so:
<connectionStrings>
<add name="MySQLConnection" connectionString="Server=localhost;Database=local;Uid=userid;Pwd=12345678;" providerName="System.Data.MySqlClient" />
</connectionStrings>
Then my Web.Release.config looks like this:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="MySQLConnection" connectionString="Server=localhost;Database=live;Uid=userid;Pwd=12345678;" providerName="System.Data.MySqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>
</configuration>
And I am grabbing my connection string in code using:
ConfigurationManager.ConnectionStrings["MySQLConnection"].ConnectionString;
I have tried switching the config version and running and I get the exact same data being pulled out, all from the local db. If anyone could point me in the right direction I'd really appreciate it!

How to swap Connection String when publishing a project?

I can see that Web.config file contains two files:
-web.Debug.config
-web.Release.config
Inside this config files there is the following comment:
In the example below, the "SetAttributes" transform will change the
value of "connectionString" to use "ReleaseSQLServer" only when the
"Match" locator finds an atrribute "name" that has a value of "MyDB".
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
What is the "Match" locator ?
I already have a connection String in Web.config So how do I set this up?
should the main web.config file contain the production connection string or the other way around?
I'm looking for adivices from people that have done similar things
We are using xdt:Transform="Replace" which basically replaces the connection string of our development DB and it works perfectly. See below:
Development connection string (in your case web.Debug.config):
<connectionStrings>
<add name="MyDB" connectionString="Data Source=DebugSQLServer;Initial Catalog=MyDebugDB;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
Production connection string (in your case web.Release.config):
<connectionStrings xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<add name="MyDB" connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" xdt:Transform="Replace" xdt:Locator="Match(name)" />
</connectionStrings>
Match(name) means that the name of the connectionString in your case MyDB if it is same as in web.config that it will set the connectionString attributes to what you have in your web.debug.config file's connectionString when you publish the website. A complete documentation can be found at MSDN. And a basic how to do can be found here.

Web.Config not transforming as expected

When I build my application using Visual Studio in either debug or release mode the connection string that I'm trying to transform doesn't get inserted into the finalised web.config file
Relevant section of web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
</connectionStrings>
</configuration>
web.debug.config file:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings xdt:Transform="Replace" />
<connectionStrings>
<add name="DefaultConnection" xdt:Transform="Insert" providerName="System.Data.SqlClient"
connectionString="string value" />
</connectionStrings>
</configuration>
I've tried the files with this site and it seems to work ok
It turns out that what I had was fine but that the modified web.config file only gets generated when you publish the website.

Creating a string in a web.config and use it in a web.api

I am new in the web development world and I would like to create a variable in the web.config file so that I can use it in the .NET portion of the web.api
I found the following tutorials on how to do that :
Setting up connection string in ASP.NET to SQL SERVER
And
http://www.connectionstrings.com/Articles/Show/store-connection-string-in-web-config
I have the following question , I don t have a database to connect the string to(I will only use it in the web config so that I can easily change the string without having to go through code . so assuming that I am using it in the following way :
<add name="ConnStringDb1" connectionString="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />
What should I have in the connectionString and the providerName ?
If I understand what you want to do, it sounds like you don't want to use a connection string at all. Instead, use the Application Settings sections of your web.config file. For example
<configuration>
<system.web> ... </system.web>
<appSettings>
<add key="MyAppSetting" value="A test value." />
</appSettings>
</configuration>
This can then be used in your code by getting the value of
System.Configuration.ConfigurationManager.AppSettings["MyAppSetting"]
(C#) or
System.Configuration.ConfigurationManager.AppSettings("MyAppSetting")
(VB)
See MSDN for more information, or just search online for "asp.net AppSettings".
If you don't have a database to connect to (which is what I understood from your question), then you don't even need to have the <connectionStrings> section in your Web.config. That section is only needed if you are going to connect to a database.
If you do use a database, then the connectionString varies depending on several factors such as type of authentication, database product (MS SQL Server, MySQL), type of driver (ODBC, .NET), etc.
The "Provider Name" will depend on the database product that you are using. For example for SQL Server is "System.Data.SqlClient"
You can look at this site for a comprehensive list of database products and connection strings appropriate for each product for different authentication types, drivers used, etc.
For ASP.NET 4.5 Application I'm using appSettings for email configuration. I'm also using connectionStrings
appSettings needs to be included before connectionStrings not before configSections
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<appSettings>
<add key="ContactEmail0" value="service#davincispainting.com" />
<add key="ContactEmail1" value="estimate#davincispainting.com" />
</appSettings>
<connectionStrings>
<!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-DAV3-20150302043828.mdf;Initial Catalog=aspnet-DAV3-20150302043828;Integrated Security=True" providerName="System.Data.SqlClient" />-->
<!--<add name="connectionString" connectionString="data source=localhost;Initial Catalog=*****;User ID=sa;Password=*****;" providerName="System.Data.SqlClient" />-->
<!--<add name="connectionString" connectionString="data source=localhost;Initial Catalog=Davincis3;User ID=*****;Password=*****;" providerName="System.Data.SqlClient" />-->
<!--<add name="connectionString" connectionString="data source=DELLLAPTOP-PC\SQLSERVEREXPRESS;Initial Catalog=Davincis3;User ID=sa;Password=*****;" providerName="System.Data.SqlClient" />-->
<add name="connectionString" connectionString="data source=DELLLAPTOP-PC\SQLEXPRESS;Initial Catalog=Davincis3;User ID=sa;Password=*****;" providerName="System.Data.SqlClient" />
</connectionStrings>
...

Resources