I have a console application in which website project is added as dll. Inside dll ConfigurationManager.ConnectionStrings["XXX"] is called which is always returning null.
But web.config in dll has connectionstring named 'XXX'. Can any one suggest what is going wrong?
I don't think .config file of the .dll is added to your console project together with the dll. You need to have the .config in your executing project.
Try copying the config file to your console project. Or at least the connection strings section. Probably in an App.config.
I had a slightly different problem that I haven't seen an answer to. I was using.
ConfigurationManager.ConnectionStrings["XXXX"].ConnectionString;
When I opened up web.config I saw that the connection string I had created was prefixed with "WebApplicationAPI.Properties.Settings." - putting the entire string in worked for me:
ConfigurationManager.ConnectionStrings["WebApplicationAPI.Properties.Settings.XXXX"].ConnectionString;
Your config file is not in the .dll. So you're referencing nothing I would suspect! Unless you've manually copied the web.config file to the correct location or references it via an absolute path?
web.config is for web application.
For console application you have to use app.config.
Well, actually that's the default configuration for C#, I won't be surprised some advanced user could make it go the other way arround.
Related
I was trying to use the solution found in this post:
How to access the folder path in web config using c#
but I get a null reference exception (System.Collections.Specialized.NameValueCollection.this[string].get returned null)?
I have this in my web config file (it's inside the configuration like shown in the post linked above):
<appSettings>
<add key="SessionTest" value="C:\\Settings\\XmlDir\\Session\\20180824.xml"/>
</appSettings>
and retrieve it like so:
string path = System.Web.Configuration.WebConfigurationManager.AppSettings["SessionTest"].ToString();
Is there something simple that I'm missing here? The file exists in the folder (and I copied the path from the file explorer, so I'm having trouble understanding what is causing the null exception >.<")
Thanks in advance :)
As far as I'm aware this is the standard way to access web.config <appSettings>
System.Configuration.ConfigurationManager.AppSettings["SessionTest"]
There was a second web config file that I wasn't aware of (I'm working on an old project so I didn't realize the first team that wrote this had added one to the folder I was working in, which in hindsight is probably a little strange)
When I added it to the web config file that is associated with the entire solution and not the one inside the folder I was working in, I was able to retrieve the value successfully!
I want to also note I included SLaks suggestion from the comment he made (which is not to say it doesn't work with the double slashes, but I didn't include them when I found success so I can't confirm that).
I have a web project that references a dll project that uses an app.config value. Is there a way to put the app.config values in the web.config so when the web project is run it pulls the values from the web.config? I've heard of this happening but I'm not 100% sure of it.
You can use the configSource attribute/property of each config element - this allows you to put sections of your configuration in an external file.
Once in an external file, you can reference this file from both your web.config and app.config files.
I dont think there is a wasy - but to open manually the app.config file ( if your start entry point is the web...)
so youll have to open each section and analze it.
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.
I have a class library, which has an app.config and when built it put the app.config along with the dll in the output directory I have chosen.
I don't want it to be name app.config though, if I have another component that has it's own dll, I can see confusion happening.
I've been looking at another project, that does exactly that, but I can't see why it outputs dllname.dll.config and mine always app.config.
Any ideas?
Thx
You probably have set the Copy to output directory setting of the app.config.
BUT: In a class library, an app.config is useless.
Note, that you can only have one config file per application. The configuration is being read (at execution time) from <executing assembly file name> + ".config".