App.config: appSettings "file" attribute is not reading from the Local.config - c#

I have a test project, with an App.config that sets up default values for some settings. I want to override these settings at the local level so each developer can, for instance, use their own credentials.
In my App.config I have the following:
<appSettings file="Local.config">
<add key="Username" value="USERNAME"/>
<add key="Password" value="PASSWORD"/>
</appSettings>
in the Local.config (in the same directory) I have the following:
<appSettings>
<add key="Username" value="wayne"/>
<add key="Password" value="secret"/>
</appSettings>
When I run my test I expect that getting the value of Username would return "wayne" from the Local.config; instead, it's "USERNAME" from the App.config - it seems as though it's not actually detecting that I want to override settings in another file.
What am I doing wrong?

I hate to ask, but are you sure the Local.config is being copied to the output directory?

Related

App Settings to reference ADO Library via Variable Substitution. XML App.Config

Is there a method of swapping the AppSettings on the XML on the app.config file, so that it gets the settings from the variables.
This is for a VSTEST 2019, EasyRepo, UI Test.
I have a bunch of sensitive settings on my app.config which i want to hide, and reference via my Azure Library.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="OnlineUsername" value="MyUserName" />
<add key="OnlinePassword" value="myPassword" />
<add key="OnlineCrmUrl" value="www.url.com" />
<add key="MfaSecretKey" value="" />
<add key="AzureKey" value="" />
etc...
I have attempted a file-transform task, and set the library for the reference of my App.Config file to be transformed. I also put the variables on the YAML it self, but did not work.
Is there a method of swapping the AppSettings on the XML on the app.config file, so that it gets the settings from the variables.

App Settings from Web.config in WCF Service returning null

I am having an issue retrieving app settings from my web.config file in a WCF service I am creating. When calling my settings, I am retrieving a null value. Digging into the object also shows that there are no keys in AppSettings list.
This issue has a few posts around SO/Google, and I have incorporated all the suggestions, but to no avail. Here is my current position and what I have tried:
web.config:
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
<!-- CRM Creds -->
<add key="CrmUrl" value="[removed]"/>
<add key="CrmUser" value="[removed]"/>
<add key="CrmPass" value="[removed]"/>
</appSettings>...
Methods of calling the settings:
System.Web.Configuration.WebConfigurationManager.AppSettings["CrmUrl"]
System.Configuration.ConfigurationManager.AppSettings["CrmUrl"]
The file I am calling from is within a folder, whereas web.config is in the root. I understand WebConfigurationManager could find this regardless, but I am having no luck.
Code:
%projectfolder%/Client/Client.cs
Config: %projectfolder%/web.config
I have run out of things to try and have used up all my Google-fu. Any suggestions are appreciated.

Centralised Connection String for multiple application asp.net

I have a web server with multiple application running. All the application have their web.config file. If the database password changes due to Policy I have to manually change the password in each of web.config files in the app setting section.
I was reading about the connection string setting in machine.config file.
Now my question is if I put connection string in appsetting section of machine.config with name ConnectionString and same in my web.config file will it overwrite the machine.config file values.
In my machine.config following is the setting
<configuration>
....
<appSettings>
<add key="ConnectionString" value="value"/>
</appSettings>
</configuration>
similarly in my web.config file
<configuration>
....
<appSettings>
<add key="ConnectionString" value="value"/>
</appSettings>
</configuration>
And I get the value in my code as below
string conString=ConfigurationManager.AppSettings["ConnectionString"];
will I get the overloaded value?
What's going to help you out here is to store your connection string(s) in .config file and then reference them either using the file="" attribute or the configSource="" attribute.
Here's an excellent question and answer that talks about the differences between the two and shows you how to implement them:
ASP.NET web.config: configSource vs. file attributes

Difference between my local and production web.config

I get this error message
The connection string 'MyConnection' in the application's
configuration file does not contain the required providerName
attribute."
I read this question The connection string 'MyConnection' in the application's configuration file does not contain the required providerName attribute." about this same issue.
In my case the providerName="System.Data.SqlClient" is correctly in my web.config file in local but disappear when I do a publish on my production server (build in release). I don't have the problem on my local server.
EDIT
Here is my release config file
<?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>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<!--
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>
</configuration>
Use this for your release config file:
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)" providerName="System.Data.SqlClient" />
</connectionStrings>
ConnectionString for your local is being change by release config file. So if you declare providerName in your local config, that will be no use if you publish your release files. you need to declare the providerName in your release config file also.

MSBuild web.config transforms not working

I am setting up my application for CI&D. I created a DEV-Deploy web.config transform which contains the connection strings for the dev testing environment.
Here are the contents of the Web.DEV-Deploy.config connection string section:
<connectionStrings xdt:Transform="RemoveAttributes(configSource)">
<add name="DbContext"
providerName="MySql.Data.MySqlClient"
connectionString="CXN_STRING"
xdt:Transform="Insert" xdt:Locator="Match(name)"/>
<add name="elmah"
connectionString="CXN_STRING"
xdt:Transform="Insert" xdt:Locator="Match(name)"/>
</connectionStrings>
It should look like:
<connectionStrings>
<add name="DbContext" providerName="MySql.Data.MySqlClient"
connectionString="CXN_STRING"/>
<add name="elmah" connectionString="CXN_STRING"/>
</connectionStrings>
I'm building using the command line and I have tried the following commands, neither of which work:
msbuild web\web.csproj /T:Package /P:Configuration=DEV-Deploy /P:TransformConfigFiles=true
msbuild web\web.csproj /T:Package /P:Configuration=DEV-Deploy /t:TransformWebConfig
The deploy task looks like this:
web.deploy.cmd /Y /M:https://MACHINEIP:8172/msdeploy.axd -allowUntrusted /U:USERNAME /P:PASSWORD /A:Basic
The web.config looks like this upon deployment:
<connectionStrings configSource="connectionStrings.config"></connectionStrings>
I have tested to the best of my ability on my local machine and have not been able to duplicate the issue. What do I need to do to get the transform working correctly on the build?
Our CI&D team put the build/deploy scripts into source control and after looking them over, everything above was correct, the problem was the path for the build command was wrong while the command itself was correct.
Once that was updated the web.config transformed correctly.

Categories