I've been trying to read an app.config file during unit testing following the instructions in this Using a Configuration File to Define a Data Source using Visual Studio 2010. I've read numerous posts here in the forum as well, but I'm still getting a null value when trying to read the app.config values. Below are snippets of my app.config and unit test code.
App.config
<configuration>
<configSections>
<section name="microsoft.visualstudio.testtools" type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</configSections>
<connectionStrings>
<add name="DatabaseConnectionString" connectionString="Data Source=***;User Id=***;Password=***;Integrated Security=no" providerName="Oracle"/>
</connectionStrings>
<microsoft.visualstudio.testtools>
<dataSources>
<add name="MyOracleConnection" connectionString="DatabaseConnectionString" dataTableName="***" dataAccessMethod="Sequential"/>
</dataSources>
</microsoft.visualstudio.testtools>
</configuration>
Unit Test:
[TestMethod()]
[DeploymentItem("app.config")]
public void S_ConstructorTest(){
myObject m = new myObject();
...}
In my object code I'm attempting to read the config file via:
public static readonly string sConnectionString = System.Web.Configuration.WebConfigurationManager.AppSettings["sConnectionString"];
Based on what I've read in other posts here, MSDN seems to add more than is necessary for this to work, however I still keep getting null values when reading the config file. I know that there's a debate as to whether or not unit testing should access a real DB but for now I just need to get it to read the config file.
Any help would be appreciated.
Related
This tutorial makes what I'm trying to do look dead easy. All I want to do is read a custom attribute out of my web.config. Here's the relevant part:
<configSections>
<section name="Authentication.WSFedShell" type="System.Configuration.DictionarySectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<Authentication.WSFedShell>
<add key="Authentication.PrincipalType" value="ClientCertificate" />
</Authentication.WSFedShell>
In the immediate window I can execute:
System.Configuration.ConfigurationManager.GetSection("Authentication.WSFedShell")
which returns the string
["Authentication.PrincipalType"]: "ClientCertificate"
However, when I try to cast it (with as NameValueCollection), as this tutorial says to do, I get null returned and my code blows up. There's gotta be a cleaner way to get the value "ClientCertificate" than manually parsing the string result.
How do I read "ClientCertificate" from app.config?
Why can't you use AppSetting like
<configuration>
<appSettings>
<add key="Authentication.PrincipalType" value="ClientCertificate"/>
</appSettings>
</configuration>
System.Configuration.ConfigurationManager.AppSettings["Authentication.PrincipalType"]
Most probably the issue with your section is the Type attribute. But anyways, you need to cast the result of GetSection() to your type defined for section like
System.Configuration.DictionarySectionHandler config = (System.Configuration.DictionarySectionHandler)System.Configuration.ConfigurationManager.GetSection("Authentication.WSFedShell");
I hope you can help me.
I'm supposed to add a new type of values to an AppSettings file (already existing with some values). Those values are a whole list of special folders so I thought the best way would be to have a new section for those folder values so that the file would look like this:for
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="usPath" value="folderName1" />
<add key="tcPath" value="folderName2" />
<add key="usGUID" value="folderID1" />
<add key="tcGUID" value="folderID2" />
</appSettings>
<updateFolders>
<add key="folderID3" value="folderName3">
<add key="folderID4" value="folderName4">
</updateFolders>
</configuration>
Reading and writing within the already existing appSettings-tag is no problem but I haven't find a way yet to modify the updateFolders section. I'm really new to using AppSettings in this way so I don't know too much about what's possible and what's not. In addition to that I think the AppSettings file might have been set up in a wrong way from the very beginning (it gets created by using a System.IO.File-Writer).
see ConfigurationManager.GetSection
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.getsection(v=vs.110).aspx
Our testmethods connect to an Excel workbook through the DataSource attribute. It looks like:
[TestMethod, Priority(3)]
[DataSource("System.Data.OleDB",
"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=|DataDirectory|\\TestDataWorkbook.xlsx; Extended Properties='Excel 12.0;HDR=yes';",
"TestDataSheet$",
DataAccessMethod.Sequential)]
And this works flawlessly. But when I try to add the same connection string in the App.Config like
<configuration>
<configSections>
<section name="microsoft.visualstudio.qualitytools" type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</configSections>
<connectionStrings>
<add name="TestDataConnectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0; Data Source=|DataDirectory|\\TestDataWorkbook.xlsx; Extended Properties='Excel 12.0;HDR=yes';" providerName="System.Data.OleDb" />
</connectionStrings>
<microsoft.visualstudio.qualitytools>
<dataSources>
<add name="MyTestData" connectionString="TestDataConnectionString" dataTableName="TestDataSheet$" dataAccessMethod="Sequential"/>
</dataSources>
</microsoft.visualstudio.qualitytools>
... other configuration settings
and try to connect to it with:
[TestMethod, Priority(3)]
[DataSource("MyTestData")]
it results in the following error:
The unit test adapter failed to connect to the data source or to read the data. For more information on troubleshooting this error, see "Troubleshooting Data-Driven Unit Tests" (http://go.microsoft.com/fwlink/?LinkId=62412) in the MSDN Library.
Error details: The Microsoft Office Access database engine could not find the object 'MyTestData$'. Make sure the object exists and that you spell its name and the path name correctly.
The error resides in the sheetname. I have an connection to the workbook (checked it by renaming the workbook and the connectionstring), but the sheetname does not get recognized as a table name as it did in the first example.
Does someone recognize this situation or does someone have an hint in the right direction?
Just paste this code on top of your test method:
[TestMethod, Priority(3)]
[DataSource("MyTestData"), TestMethod]
This will solve your problem :)
I have created a console application and an app.config file and Connections.config file.
The app.config file has a connectionstring property source pointing to the Connections.config
When I tried to read the connection string in the application, I get a ConfigurationErrorException
This is my main method.
static void Main(string[] args)
{
var settings = ConfigurationManager.ConnectionStrings;
if (settings != null)
{
foreach (ConnectionStringSettings setting in settings)
{
Console.WriteLine(setting.ConnectionString);
}
}
}
App.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings configSource="Connections.config"></connectionStrings>
</configuration>
Connections.config file
<?xml version="1.0" encoding="utf-8" ?>
<connectionStrings>
<add name="SQLDBConnecion"
providerName="System.Data.ProviderName"
connectionString="" />
</connectionStrings>
Here I observed two things.
First: If I specify configSource I am unable to read the connection string (throwing exception.)
Second: If I put same connection string in App.config file and tried to read then the code is working but getting two connection string (which supposed to be return only one which is empty string)
The first connection string is sqlexpress connection string like this
data source=.\SQLEXPRESS;Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
second connection string it returning is empty string (This is expected).
I want to read connection string from external file like in my scenario. How to do that? What am I missing here?
MSDN says:
Do not include any additional elements, sections, or attributes.
You need to remove the XML encoding.
Edit
Also, you need to set the properties of your config file to Copy to Output Directory = Copy if newer or Copy always.
Edit 2
To build on what Dave said, you add the clear element to your external file. Your final Connections.config file should look exactly like this:
<connectionStrings>
<clear/>
<add name="Name"
providerName="System.Data.ProviderName"
connectionString="Valid Connection String;" />
</connectionStrings>
Your Connections.config file should be as shown below without the xml header
<connectionStrings>
<add name="SQLDBConnecion"
providerName="System.Data.ProviderName"
connectionString="" />
</connectionStrings>
Also for it to correctly locate the file in your console application, please set the Copy to Output Directory to Copy Always or Copy If Newer.
That first connection string you are getting is inherited from the machine.config. This is described in the MSDN documentation. http://msdn.microsoft.com/en-us/library/bf7sd233(v=vs.90).aspx
You can use the Clear tag in your config file to remove inherited connection strings.
http://msdn.microsoft.com/en-us/library/ayb15wz8(v=vs.90).aspx
<connectionStrings>
<clear/>
<add name="SQLDBConnecion"
providerName="System.Data.ProviderName"
connectionString="" />
</connectionStrings>
There is a nice article on MSDN: https://msdn.microsoft.com/en-us/library/ms254494(v=vs.110).aspx.
Quote from the article:
To store connection strings in an external configuration file, create
a separate file that contains only the connectionStrings section. Do
not include any additional elements, sections, or attributes. This
example shows the syntax for an external configuration file.
<connectionStrings>
<add name="Name"
providerName="System.Data.ProviderName"
connectionString="Valid Connection String;" />
</connectionStrings>
Hope this helps people who run into this question later.
Here is my app.config
<configuration>
<configSections>
<section name="procedureList" type="System.Configuration.NameValueSectionHandler, System, Version=4.0.30319, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</configSections>
<procedureList>
<add key="NAS.spBusObjGetLineProd" value="#area='Melt Shop';#endDt=?date?;#dayonly=1;#obj='Melt Shop Business Objective" />
<add key="NAS.spBusObjGetLineProd" value="#area='Cold Mill';#endDt=?date?;#dayonly=1;#obj='Cold Mill Business Objective" />
</procedureList>
<appSettings>
<add key="Connstr" value=""/>
<add key="Userid" value=""/>
<add key="Timeout" value=""/>
</appSettings>
</configuration>
But when I call it in code, I'm getting a null back
public void samplemethod()
{
NameValueCollection nvc = ConfigurationManager.GetSection("procedureList") as NameValueCollection;
string[] keys = nvc.AllKeys;
}
I would appreciate any help pointing out what I've done wrong
Using section handlers to group settings in the configuration file
For example you can follow something like the following
private void ReadSettings()
{
NameValueCollection loc =
(NameValueCollection )ConfigurationSettings.GetConfig("procedureList");
}
MSDN ConfigurationManager.GetConfig Method
If you are testing your class you must copy the configuration to the app.config in your Test project.
using immediate window check which config file it is pointing to. in my case i had app.config which i am expecting it to read, but on using the command. ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) it is pointing to something else like nuintrunner.exe.config as that info is loaded into bin. this help in loading the right configuration file