Using config files in MSTest (testConfig) - c#

I´m trying to use MyProject.config in MSTest. I added this file in "Deployment" (testConfig).
What is the best practice to access those fields? Can i build something like Java Properties?
Example:
<host>localhost</host>
<port>0</port>
<user>me</user>
Edit:
I have two Unit Test suite . I want to use diferents config files in each test.

Is the ConfigurationManager.AppSettings property of any use? Give that a try and see if it works, like this:
string port = System.Configuration.ConfigurationManager.AppSettings("port");
You'd need to refactor your configuration file to be a standard "App.config" file, and you add key/value pairs like this:
<configuration>
<appSettings>
<add key="port" value="80" />
</appSettings>
</configuration>

Related

App.config - Values in <applicationSettings> fail, Values in <appSettings> work

In an effort to try to get ConfigurationManager.AppSettings[..] working again, I've created one applicationSettings element and one appSettings element in a single-project console application.
<applicationSettings>
<el_testo.Properties.Settings>
<setting name="ApplicationSetting" serializeAs="String">
<value>Application Value</value>
</setting>
</el_testo.Properties.Settings>
</applicationSettings>
<appSettings>
<add key="AppSetting" value="App Value"/>
</appSettings>
Here's my effort to retrieve the two values.
Debug.WriteLine("---> ApplicationSettings Test");
Debug.WriteLine(ConfigurationManager.AppSettings["ApplicationSetting"] + "");
var applicationSettingsTest = ConfigurationManager.AppSettings["ApplicationSetting"];
Debug.WriteLine(applicationSettingsTest);
Debug.WriteLine("---> AppSettings Test");
Debug.WriteLine(ConfigurationManager.AppSettings["AppSetting"]);
Debug.WriteLine("---> Complete");
And here are my results.
---> ApplicationSettings Test
---> AppSettings Test
App Value
---> Complete
I could just "settle" and use the appSettings construct. It would be kind of nice, however, to use the Settings tab on Project properties on this project and future projects. Here's what I've tried this far to make work.
There's only one project in this application. Therefore, I can't be setting the value in one project and trying to read it in another. (Additionally, appSettings works.)
I verified that the project includes the assembly reference System.Configuration and the class includes the proper namespace reference (using System.Configuration;)
I've double-checked spelling and case of the setting name in ConfigurationManager.AppSettings["ApplicationSetting"].
I've tried linking App.config to different XML Schema files, EnterpriseLibrary.Configuration.xsd and an XML Schema file I generated using the XML menu in Visual Studio.
I've wiped the contents of the Debug and Release folders and rebuilt the project.
You are incorrectly trying to read your ApplicationSetting element as a key under appSettings with the following statement:
ConfigurationManager.AppSettings["ApplicationSetting"]
This means your are trying to get the value of this:
<appSettings>
<add key="ApplicationSetting" value="xxxx" />
</appSetting>
Instead; when you add an application setting using the project properties, it creates a settings class extending the ApplicationSettingsBase class, create properties with the name of your settings and will also add necessary configSection and a default config element in your app.config file. Why not just create and use that class which will give you type strict access to your application settings?
Just go to Project/e_testo Properties/Settings and add ApplicationSetting with scope Application
You can then access the ApplicationSetting by
var applicationSettingsTest = Properties.Settings.Default.ApplicationSetting

How to define web.config local variables

I have a web application in C# with .NET Framework 4.0 and I'm trying to find a way to define a variable in Web.Config that can be referenced elsewhere within the Web.Config.
I want something like this.
<?xml version="1.0" encoding="utf-8"?>
<LocalWebConfigVars>
<add key="Var1" value="ServerName1"/>
<add key="Var2" value="DatabaseName1"/>
</LocalWebConfigVars>
<configuration>
<connectionStrings>
<add name="AppConnectionString" connectionString="DATA SOURCE=#Var2 ServerName=#Var1"/>
<add name="OtherStuff" value="#Var1"/>
etc...
Currently I have to keep 3 or 4 hard-coded values (some embedded others just the value) updated to the same thing and would like to make it easier to keep in sync.
Is this possible?
Thanks.
Edit:
Just some background. The reason this is becoming problematic is that we define the apps database instance (among other instance specific setting) in the web.config. We have multiple database instances in our test and production environments and if I need to switch to a different one them while testing something and miss one of the hand full of references I get some strange results. I'm trying to avoid this by defining it once and referencing it everywhere else.
Would not doing a simply .config transformation for each of your environments work, by setting up a project configuration & transform, you would be able to swap from environment to environment by the use of solutions configuration dropdown.
See this link
for more info on transforms on .config files

How can I make a C# project's settings be user specific?

I am writing a Web API 2.0 project and a test project using Visual Studio 2013.
In the test project, I saved some information in the Settings.settings file (under TestProject->Properties in the Solution Explorer). One of the things saved there is the connection string to a database that is stored locally.
Unfortunately, the connection string will be slightly different on each person's computer when they download the repo. When people push their code to the master repo it overwrites the connection string, affecting everyone else.
What is the best way to make this configurable for each user such that everyone can have their own database path, but pushing to master repo won't affect anyone?
Edit
I don't think this is exactly a duplicate of that other question. Although, yes, my configuration settings are stored in app.config (since they happen to be application settings rather than user settings), following the solution in the other answer will lead me with the same problem. The app.config will contain configSource="otherconfig.config", and when people push that file to the master repo, it will still clobber other people's values. I need something that allows the custom configurations to be source-controlled without affecting the other users of the project.
Visual Studio handles this automatically for WEB projects through Web.config transformations
You'll need to install a separate plugin for use with App.config and non-web projects. http://visualstudiogallery.msdn.microsoft.com/579d3a78-3bdd-497c-bc21-aa6e6abbc859
The plugin basically adds the same functionality to app.config files, and works with the same syntax in the transform files.
Your best approach to this is to use Build Profiles. Have a developer-specific Web.developer.config and with that you get each user to choose their name in Configuration Manager. Then just make the new config, which is technically an XSLT make the changes needed for each team member.
Think of it as Debug vs Release configs, except in your case you'll have many Debug (one for each user). The Build profile you set doesn't get checked into TFS, so you're fine.
This is what a subconfig looks like:
<?xml version="1.0" encoding="utf-8"?>
<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="RavenDB" connectionString="Url=http://xxx/databases/xxx" xdt:Transform="Replace" xdt:Locator="Match(name)"/>
</connectionStrings>
<appSettings>
<add key="BaseUrl" value="http://xxx" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
</appSettings>
<system.net>
<defaultProxy enabled="true" />
<mailSettings>
<smtp xdt:TrandeliveryMethod="Network" transform="Replace">
<network xdt:Transform="Replace" host="xxx" defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
</configuration>
More info on web.config transforms
http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx
The way I handle this problem is by adding a folder into my app that has only assets that don't get included in the build/publish. One of the things I include in that folder is DeveloperName.App.config files for each of my developers. Then I leave the the actual App.config file out of source control. When they check out the project, they copy their personalized DeveloperName.App.config file to the project folder and rename it to App.config.
This isn't perfect, but it gives you at least most of the goals you're looking for: The developers each get their own App.config file they can maintain and keep in source control. And the changes they make to App.config don't clobber each other every check-in.

Best way to set up the connection setting

I am new to windows forms programming and I was wondering what would be the best way to create like a connectionstring.config (i dont know how to do this) I have seens it , its like an xml giving all the information to connect to the database, in my case I am connecting to a MySql database. I would like to have so i could do something like this (I KNOW THIS IS WRONG BUT YOU GET MY IDEA):
MySqlConnection conn = new MySqlConnection
(Someconfiguration.thatconnects.toMyXMLOrSomething["MyXMLFile]);
something like that, i know i am probably too far from what it is.. but I have seen this somewhere and i think its clean, instead of putting the data connection information everywhere i need it.
so a few questions:
How do i create that xml file in VS2010?
Where do I place that file?
How should I call it in the functions where i am using it?.
I would really appreciate all the possible help as I am learning and would like to keep everything separate and clean like this.
Thank you for your help and valuable time to help me.
You can put them in a specific node inside your app.config file.
Here's the MSDN documentation for adding the app.config file to your project and it includes adding connection strings: http://msdn.microsoft.com/en-us/library/ms243192(v=vs.100).aspx
<configuration>
<connectionStrings>
<add name="myConnectionString" connectionString="Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;" />
</connectionStrings>
<configuration>
You can then use the built in .NET ConfigurationManager class to pull it out:
ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
Right click your project - Add -> New Item... - General - Application Configuration File (app.config). You can add a connectionStrings section there.
See "Connection Strings and Configuration Files" for details.
If you really want to put you connection strings in a separate file you can set this up as well within your app.config file:
<?xml version='1.0' encoding='utf-8'?>
<configuration>
<connectionStrings configSource="connections.config"/>
</configuration>
How do i create that xml file in VS2010?
Add a new app.config file in the root of your project.
Where do I place that file?
Normally the root of your application.
How should I call it in the functions where i am using it?
string value = System.Configuration.ConfigurationManager.AppSettings[key];
string connection = System.Configuration.ConfigurationManager.ConnectionStrings[key];
Put in in app.config. It should already be in your project.
Here is some Microsoft documentation on the topic.
http://msdn.microsoft.com/en-us/library/ms254494(v=vs.80).aspx
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="ApplicationConnectionString" connectionString="YOUR_CONNECTION_STRING" providerName="System.Data.PROVIDER_NAME"/>
</connectionStrings>
</configuration>

How to make the Setting works for Class Library project in C# 2.0?

I am putting the setting under the property of one of my C# Class Library project for app setting:
EUCAccountService_ConnectionString
EUCTelcoDB_ConnectionString
In the development, it works nicely. Until I deported to production, I realise that the component that use those thing .. it just hang. I found that under \BIN when it compiled dewaCorp.EUC.TelcoDB.Data.dll.config and open up that file and turn out nothing.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
</configuration>
How to make this works? I thought by compiling it, it turned to some sort config file or something. But it didn't.
I am appreciated your comment.
The properties are not stored in the .config file they are stored in the windows user profiles.
To store setting in the .config file add a config file to the executing assembly (take note is important to use the executing assembly) and store add the settings there for connection strings there is a special note for them.
<ConnectionStrings>
<ConnectionString />
</ConnectionStrings>
You'd better take a look at similar projects, such as log4net, and Enterprise Library.
http://logging.apache.org/log4net/index.html
http://www.codeplex.com/entlib

Categories

Resources