In my solution I have two projects, one which is set to the startup project, the other not. I have added an App.config file to the start-up project.
The App.config file works in the project which is the startup project, but I cannot read the App.config from the other project, the data is coming back as null.
If I want my non start-up project to access the App.config file in the start-up project, what settings do I need to change?
I am using the following code to access the file. String testrail is coming back as null when running from the non start-up project.
public string testrail;
testrail = ConfigurationManager.AppSettings["testrail"];
My App.config file looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="testrail" value="True"/>
</appSettings>
</configuration>
Related
My project use Entity Framework 6 Database First for the DAL, and it need to have a connectionString in every project's config file.
As I have multi project in solution, I found that it can be a problem if I need to modify connection string, which I need to check every config file.
So I come up an idea, create a Share config file under the solution directory.
The structure:
+ Solution
- CommonConfig
- Share.Debug.config
- Share.Release.config
- Project1
- web.config
- web.Debug.config
- web.Release.config
- Share.Debug.config (Add with Link)
- Share.Release.config (Add with Link)
- Project2
...
And I have move the connectionString section to Share.xxx.config
<connectionStrings>
<add name="db" connectionString="..." />
</connectionStrings>
First test with configSource, it work without problem
<?xml version="1.0" encoding="utf-8"?>
<configuration>
...
<connectionStrings configSource="bin\Share.Debug.config"/>
...
</configuration>
Then I have remove whole section for connectionString
And modify the web.Debug.config
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings configSource="bin\Share.Debug.config" xdt:Transform="InsertIfMissing" />
<system.web>
</system.web>
</configuration>
But it doesn't work, the EF cannot find the connectionString with this setting.
Will it a problem if I want to add a whole section instead of a node?
EDIT
After read several post, some said the transform only work when I publish the website, so I try to publish it to my local IIS.
When I create and preview the publish profile, it return error
A section using 'configSource' may contain no other attributes or elements.
I have an app.config file in Winforms application that holds a connection string. This is to go out to multiple tenant (clients) as a separate file. These clients have different database sources. This config file also holds other version information such as EF, Telerik reporting etc...
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
and
<section name="Telerik.Reporting"
type="Telerik.Reporting.Configuration.ReportingConfigurationSection, Telerik.Reporting, Version=8.1.14.804, Culture=neutral, PublicKeyToken=a9d7983dfcc261be"
allowLocation="true" allowDefinition="Everywhere" />
The problem I have is when we have an updated version of EF or Telerik reporting with our application and we deploy (auto-deploy) this we need to overwrite the app.config file in the client directory to update the versions in the client config file. They then lose their connection setting and I do not want the client to have to go and re-enter it.
My question:
Is there a best practice to overcome this issue? Should I hold the connection string somewhere else?
Yep, the best thing to do is to move your connection strings section to an another config file and reference that file within your app.config.
For example create a new file called connectionStrings.config:
<connectionStrings>
<add name="Default" connectionString="[client_connection_string] "/>
</connectionStrings>
And in your app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings configSource="connectionStrings.config" />
</configuration>
A full example can be found here.
Use an external configuration file that is referenced from the application config file. E.g. include this section in your config file.
<configuration>
<connectionStrings configSource="connections.config"/>
</configuration>
The external config file is described http://msdn.microsoft.com/en-us/library/ms254494(v=vs.110).aspx
Note that storing connection settings in plaintext on a workstation is still a bad idea.
Using Windows registry for stuff like this is a definite no-no these days.
you can try to hold all connection data that you need in separate xml file so it dont get overwrite when you preform a deploy of updated version.
I'm trying to reference some common config settings between a Windows Service and an ASP.NET MVC website. I am doing this by using the file attribute on appSettings in either the App.config or Web.config (respectively). The file (named common.config) that is being referenced is a linked file in a separate project in the same solution. That common.config is set to Content with Copy Always in both projects.
This stack answer to a similiar question seems to suggest at least for configSource this solution would work. I don't want configSource though as I only want a handful of the properties to be common amongst the two projects. Update: I just tried this, and the configSource also doesn't work. It can't find the config file. This leads me to believe the common.config is not treated as content with copy always.
Example App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings file="common.config">
<add key="NotCommonKey" value="1"/>
</appSettings>
</configuration>
Example Web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings file="common.config">
<add key="NotCommonKey2" value="2" />
</appSettings>
</configuration>
Example common.config (Content -> Copy Always)
<appSettings>
<add key="CommonKey" value="1" />
</appSettings>
I am using ConfigurationManager / WebConfigurationManager reading from the AppSettings property.
Any ideas why when the common.config is a linked file, it's AppSettings values are not used and when it is not linked it works as normal?
Thanks!
In the Web.Config you must add "bin/" (se example below).
By default the web.config is NOT copied into the bin folder but the file common.config is, therefore you must add the path from web.config. In a non-web project the default behavior is that the App.config is copied to the bin folder with name MyProgram.exe.config and is in the same directory as common.config.
<appSettings file="bin/common.config">
The idea of using "bin/..." is good but leads to an error saying that "/" is an invalid character in the resulting virtual path.
The proper solution is tu use "bin...".
Cheers
I use this to access another .exe's config file, not sure whether it will work with a MVC project, but this might get you closer:
string proj2Exe = #"C:\projects\proj2\bin\Debug\proj2.exe";
Configuration proj2Config = ConfigurationManager.OpenExeConfiguration(proj2Exe);
string mysetting = proj2Config .AppSettings.Settings["ThatSetting"].Value;
I have a console app, let's call it Test.Console. This app uses a project called Test.Code.
Test.Code is a wrapper to access a database and should have an app.config file containing the connection string.
Test.Console should then access the classes in Test.Code to access what's in the database.
However, when I put the connection string in the app.config in the Code project, it doesn't seem to be able to access it, but when I put it in app.config in the Console project, the code in the Code project seems to be able to access it.
Is there any way that I can put the connection string in the Code project rather than the Console project?
Edit:
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<clear />
<add name="ACE" connectionString="data source=servername;database=ACE;userid=username;password=password"/>
</connectionStrings>
</configuration>
In my class:
string connection = ConfigurationManager.ConnectionStrings["ACE"].ConnectionString;
That's it at the moment.
The app.config is exactly that, configuration for the running application.
The running application should be in charge of what databases etc that it connects to, hence the reason the config goes there.
I have a solution containing 1 console application and 2 libraries.
In the libraries I have two different app.configs for an example my data.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="OutputFileFolder" value="c:\\log" />
<add key="OutputIndexFile" value="c:\\log\index.xml" />
</appSettings>
</configuration>
And in this library class I have in the constructor
_indexPath = ConfigurationManager.AppSettings["OutputIndexFile"];
But how should I load the Data.config file from my main console application (this should be the main config file)?
Config files in your dll projects are not relevant at runtime. The config file (if any) in your console application project is the one that will get used.
If you want to use a configuration file in two separate projects, you can add it as a link to your second project, or you could use a post-build event to copy it over. However both of these seem a little hacky.
Libraries don't really have associated configuration files as such - they operate under an executable (a console application, in your case).
You should put all the configuration in the app.config file of the application for the code in the libraries to have access to it.
You can load multiple config files by have multiple Configuration instances from multiple calls to ConfigurationManager.OpenMappedExeConfiguration (add your config file to the file map, the global file will be added automatically and specify a ConfigurationUserLevel.None.
Something like:
var fileMap = new ExeConfigurationFileMap {
ExeConfigFilename = Path of dll's config file
};
var cfg = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
var result = cfg.AppSettings["OutputIndexFile"];