Hope you can help with this one, although so for here does seem to b a resolution.
I have a class library which connects to a SQL Server database using an EF context class with a connection string in the app.config file.
I then reference this dll in an ASP.NET app but have to add the connection sting again in the web.config file
I would have though by referencing the dll and thus the connection string, I would not require to add the connection string again.
Perhaps I am missing something or has anyone else found a resolution to this ?
The connection string is read from the executables app.config (i.e. windows forms/WPF/console applications). For web applications the connection string is taken from the web.config file of the startup project (i.e. where the global.aspx.cs) is located.
You cannot use the app.config from your model DLL directly. You have to put the connection strings in the "main" projects.
What might help is some kind of scripting, but that's up to you.
You could try to use the configSource attribute and configure and external file:
<ConnectionString configSource="shared.config" />
The content of the file replaces the content of the element though. Another thing is that you need to include the file in your web app project and toggle copy to output.
And another warning:
the external connection strings file must be in the same directory as
the root web.config file, so you'll have to take precautions to ensure
you don't check it into your source repository.
More about that and best practices about deploying sensitive data can be found here:
http://www.asp.net/identity/overview/features-api/best-practices-for-deploying-passwords-and-other-sensitive-data-to-aspnet-and-azure#con
Related
Recently created a new class library project and added a App.config file to it. App.config file contains DB connection string. But the current code
ConfigurationManager.ConnectionStrings["DBConnectionName"] gives me "null" .
But ConfigurationManager.ConnectionStrings[0] gives me result with name "LocalSqlServer" . But my App.config does not have any entry like this.
I don't see any other config files in my project . Then why this is happening ?
How could i ensure am pointing to correct App.Config?
App.Config and other config files are read from the entry point of the program. Thus, if it is an application, the app.config file stored in the windows application project is read.
LocalSqlServer is the default SQL server when no other connection string is defined. This is the reason why it exists inside ConnectionStrings property.
Please add the connection string to App.config of your main application, the executable. Alternatively, if you want to define your own config file then make use of ConfigurationManager.OpenExeConfiguration Method.
More details in following link:
http://msdn.microsoft.com/en-us/library/ms224437.aspx
I have done a project in C#.NET where my database file is an Excel workbook. Since the location of the connection string is hard coded in my coding, there is no problem for installing it in my system, but for other systems there is.
Is there a way to prompt the user to set a path once after the setup of the application is completed?
The answers I got was "Use App.Config"... can anyone tell what is this App.config and how to use it in my context here?
At its simplest, the app.config is an XML file with many predefined configuration sections available and support for custom configuration sections. A "configuration section" is a snippet of XML with a schema meant to store some type of information.
Overview (MSDN)
Connection String Configuration (MSDN)
Settings can be configured using built-in configuration sections such as connectionStrings or appSettings. You can add your own custom configuration sections; this is an advanced topic, but very powerful for building strongly-typed configuration files.
Web applications typically have a web.config, while Windows GUI/service applications have an app.config file.
Application-level config files inherit settings from global configuration files like machine.config. Web also applications inherit settings from applicationHost.config.
Reading from the App.Config
Connection strings have a predefined schema that you can use. Note that this small snippet is actually a valid app.config (or web.config) file:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="MyKey"
connectionString="Data Source=localhost;Initial Catalog=ABC;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
Once you have defined your app.config, you can read it in code using the ConfigurationManager class. Don't be intimidated by the verbose MSDN examples; it's actually quite simple.
string connectionString = ConfigurationManager.ConnectionStrings["MyKey"].ConnectionString;
Writing to the App.Config
Frequently changing the *.config files is usually not a good idea, but it sounds like you only want to perform one-time setup.
See: Change connection string & reload app.config at run time which describes how to update the connectionStrings section of the *.config file at runtime.
Note that ideally you would perform such configuration changes from a simple installer.
Location of the App.Config at Runtime
Q: Suppose I manually change some <value> in app.config, save it and then close it. Now when I go to my bin folder and launch the .exe file from here, why doesn't it reflect the applied changes?
A: When you compile an application, its app.config is copied to the bin directory1 with a name that matches your exe. For example, if your exe was named "test.exe", there should be a ("text.exe.config" in .net framework) or ("text.dll.config" in .net core) in your bin directory. You can change the configuration without a recompile, but you will need to edit the config file that was created at compile time, not the original app.config.
1: Note that web.config files are not moved, but instead stay in the same location at compile and deployment time. One exception to this is when a web.config is transformed.
.NET Core
New configuration options were introduced with .NET Core and continue with the unified .NET (version 5+). The way that *.config files works hasn't fundamentally changed, but developers are free to choose new, more flexible configuration paradigms.
As with .NET Framework configuration .NET Core can get quite complex, but implementation can be as simple as a few lines of configuration with a few lines of c# to read it.
Configuration in ASP.NET Core
Configuration in .NET Core
Simply, App.config is an XML based file format that holds the Application Level Configurations.
Example:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="key" value="test" />
</appSettings>
</configuration>
You can access the configurations by using ConfigurationManager as shown in the piece of code snippet below:
var value = System.Configuration.ConfigurationManager.AppSettings["key"];
// value is now "test"
Note: ConfigurationSettings is obsolete method to retrieve configuration information.
var value = System.Configuration.ConfigurationSettings.AppSettings["key"];
App.Config is an XML file that is used as a configuration file for your application. In other words, you store inside it any setting that you may want to change without having to change code (and recompiling). It is often used to store connection strings.
See this MSDN article on how to do that.
Just to add something I was missing from all the answers - even if it seems to be silly and obvious as soon as you know:
The file has to be named "App.config" or "app.config" and can be located in your project at the same level as e.g. Program.cs.
I do not know if other locations are possible, other names (like application.conf, as suggested in the ODP.net documentation) did not work for me.
PS. I started with Visual Studio Code and created a new project with "dotnet new". No configuration file is created in this case, I am sure there are other cases.
PPS. You may need to add a nuget package to be able to read the config file, in case of .NET CORE it would be "dotnet add package System.Configuration.ConfigurationManager --version 4.5.0"
You can access keys in the App.Config using:
ConfigurationSettings.AppSettings["KeyName"]
Take alook at this Thread
Just adding one more point
Using app.config some how you can control application access, you want apply particular change to entire application use app config file and you can access the settings like below
ConfigurationSettings.AppSettings["Key"]
Application settings enable you to store application information dynamically. Settings allow you to store information on the client computer that shouldn't be included in the application code (for example a connection string), user preferences, and other information you need at runtime.
To add an application configuration file to a C# project:
In Solution Explorer, right-click the project node, and then select Add > New Item.
The Add New Item dialog box appears.
Expand Installed > Visual C# Items.
In the middle pane, select the Application Configuration File template.
Select the Add button.
A file named App.config is added to your project.
take a look at this article
In my application I would like to persist a variable for configuration as part of the the project (I could do it with an accompanying file but that would be evil), so that a change can be made while the application is running and saved such that it can be closed and re-opened and keeping the same value.
What is the recommended or best practice approach to this in a Visual Studio 2010 C# project?
Use App.config or Web.config.
Example: here
Which is the recommended best approach.
You could also save your settings in the Database (if you're using one) and load them at runtime, if you don't want to use "an accompanying file" (although having said that it is still an accompanying file). However you'd have to hard code the connection string to your DB which might be troublesome if the connection string is changed.
Also note that connection strings are usually saved in the .Config file under the tag <connectionStrings></connectionStrings>
Make use of .Config file for saving setting. Developers can use configuration files to change settings without recompiling applications.
Configuration files contain elements, which are logical data structures that set configuration information. Within a configuration file, you use tags to mark the beginning and end of an element.
Configuration Files
and
Configuration Settings File for providing application configuration data
I am using more than one projects in a solution, on e of them are real projects, others are helper projects. One of those helper projects, I have settings.settings file for storing database connection string. Now When I build/debug it. I don't see any file where I can change the connection string. Was it gone integrated in the helperproject's dll file? How can i resolve this os that i can change the connection string please?
The Settings class generated by the designer has "internal" access specifier by default.
So the application settings you've added will not be visible in the assembly that is referring the helper assembly.
To make it visible across the referring assemblies, make the class public manually or via the designer. See the snapshot.
After you've made it public, you can access it in other projects where this assembly is referred.
String connectionString = TestAssembly.Settings.Default.ConnectionString;
But it would be better to put the settings in a application configuration file specific to that application. In your case add an app.config to the main project; and add connectionStrings section in the config.
I think that you miss understood something. You should add at any project you want the app.config file (or web.config in case of web app / site).
Then via ConfigurationManager class you should access the data in it.
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx
The app.config is automatically copied once you run/compile your project and it will have a name as youreapp.exe.config or yourelib.dll.config.
For more details read here http://www.codeproject.com/KB/files/SaveConnStringInAppConfig.aspx
Hope this helps
I'm trying to use application settings with a C#.NET project I am working on. But I can't seem to get it to return anything other then the default values. Through the project properties I've added a single setting, DBConnectionString, and set its value to a connection string I want to use. Its scope is set to "application".
Doing this created a number of files including Settings.settings, Settings.Designer.CS, and app.conifg. From what I understand the two Settings.* files define a custom Settings class that derives from ApplicationSettingsBase. The Settings class then has custom, type safe, properties that can be used to set and retrieve each setting. The app.config file is a XML file that stores the actual settings values.
When I build my project it looks like the app.config file is copied to the target directory as MyApplication.dll.config. My program runs fine and is able to use the default connection string.
Next I tried to edit the MyApplicaiton.dll.config file to change the connection string. I ran my program again, but it continued to use the default value. I noticed that in my Settings.Designer file there is a DefaultSettingValueAttribute with the original default string. I tried removing this attribute, but then when I tried to retrieve the connection string setting it just returned null.
This is the code I'm using to read the connection string.
string conn = Properties.Settings.Default.DbConnectionString
What am I doing wrong? How can I get it to return the value in the settings file and not the default value?
Update:
Sorry I should have mentioned this. I'm actually writing a plug-in for another application through a public API. So my project is a DLL not an EXE.
You cannot read settings from *.dll.config files. If you library needs any special settings you need to put them in your app.config or web.config files.
EDIT: You can include the external config files in the main application or web config file. Look here for details.
This question discusses how to manage configuration files for large projects.
Settings files and .config files are different things (I do not know why VS automatically added a .config when you created a Settings file). But, the settings file is compiled into a class and is referenced like you said. If you decompile the dll with .NET reflector the Settings class will be in there. It is used for holding constant values or external resources. For example: error message strings, icons, or images.
The config file is for settings which can change frequently or between environments (dev, test, prod). For a connection string you should use the <connectionStrings> section of the config file. And the property can be referenced using System.Configuration.ConfigurationManager[ "connectionStringName" ].
However, from your original post it looks like your .dll is going to be used in a larger project (either an .exe of web project). One thing to note is that all projects only use one .config file. And that is the config file for the main project. Websites the web.config file, and exe's use XXX.XXX.XXX.exe.config (as you saw, *.exe.config files are renamed copies of the app.config files). dll's do not have usable config files. All dll's will look at the main project's .config file to retrieve information.
If your connection string is never going to change then by all means use the Settings file. Otherwise, use a config file and let the developer of the main project determine what to populate the connection string with.