I have a C# class library A which has some configuration settings in its App.config I access them with
Method1()
{
string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
}
But when I call Method 1() from my ASP Web project B, it cannot find the configurations settings in the Class library A
Any idea what is happening here?
The entire configuration management structure created by .Net runtime, is process-specific. not assembly specific. This means that each running executable gets an app.config. A Web project gets a web,config (actually a web project can have multiple web.configs), but assemblies cannot have their own app.configs, they can have code to read the configuration settings in the config file for whatever process they are being referenced in (which use the assembly as a reference in a winforms app, then it can see config settings in the MyWinformsApplication.exe.config; Use the assembly in an ASP.Net web app, then it can see confiog settings in the web applications' web.config...
The config settings have to be copied to your web.config. Essentially there is only one default config file per project which the ConfigurationManager reads.
A library doesn't have its own configuration file. Configuration settings should be defined in the exe that uses that library
I believe you can use OpenExeConfiguration to do this:
string exePath = "<full path and name of the app .exe file>";
System.Configuration.Configuration otherConfig =
ConfigurationManager.OpenExeConfiguration(exePath);
You could put the path of the other .exe in the web app's web.config (for instance, in the appSettings section), and read it from there, which would be better than hard-coding it here.
to view the appSettings in that config file:
AppSettingsSection otherAppSettings = otherConfig.AppSettings;
This MSDN page might help.
It's looking for the configuration setting in your web project.
Related
when building a desktop app in wpf can you read documentation of problems and safely subsititute 'app.config' when people's answer's refer to 'web.config'?
if so are there any glaring GOTCHAS you have to look out for?
tnx
Read the Documentation:
Web.config and App.config
The choice of the
configuration file name is determined by the hosting environment you
choose for the service. If you are using IIS to host your service, use
a Web.config file. If you are using any other hosting environment, use
an App.config file.
In Visual Studio, the file named App.config is used to create the
final configuration file. The final name actually used for the
configuration depends on the assembly name. For example, an assembly
named "Cohowinery.exe" has a final configuration file name of
"Cohowinery.exe.config". However, you only need to modify the
App.config file. Changes made to that file are automatically made to
the final application configuration file at compile time.
In using an App.config, file the configuration system merges the
App.config file with content of the Machine.config file when the
application starts and the configuration is applied. This mechanism
allows machine-wide settings to be defined in the Machine.config file.
The App.config file can be used to override the settings of the
Machine.config file; you can also lock in the settings in
Machine.config file so that they get used. In the Web.config case, the
configuration system merges the Web.config files in all directories
leading up to the application directory into the configuration that
gets applied.
Web.Config is used for asp.net web projects / web services.
App.Config is used for Windows Forms, Windows Services, Console Apps and WPF applications
Your question isn't providing all the information as to where the gotcha's may lie for you.
Can you give us more info on what you are trying to do in terms of these config files?
Here's a link...
Problems with Web.config and App.config
Well, this question is a bit different from others with the slightly same title.
I add a config file to my DLL which will be used from a website and a console application.
I'm testing the DLL from my web application.
When I build the DLL I can see my MyApp.dll.config in the bin\debug folder.
Nevertheless, when I try to read the settings from the DLL this way:
var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
The file is not found.
I know it is something to do with the location where the application is being executed as Assembly.GetExecutingAssembly().Location return a path in Framework\v4.0.30319\Temporary ASP.NET Files\root\... while AppDomain.CurrentDomain.BaseDirectory return another completely different path.
So, what I'm doing wrong? There is some configuration missing to get the config file to be copied in the real location where the application is being ran?
Thank you in advance from your help!
Usually when you build a .dll and it has a config, that file lives with the dll in the same folder.
Alas, when you use your dll in another project, that project usually has it's own config, which takes precedence over the dll's one.
You could either add the dll's config to the parent's config, or try configuring what you need in code, instead of in the config.
In .NET DLLs can not have configuration files. They will simply not be used. They are created if you use the settings tab in the project properties, but they will not be read. What you need to do is merge the settings from that config file into the application's configuration (in your case, the web.config file).
I have several C# console applications, which need to have the same set of settings. I want to avoid duplicity and avoid separate app.config for each application.
Is there any way to read a common app.config file (say common.config) for applications (app1.exe, app2.exe).
Create one file called app.config. Put it in some place outside of your projects' directories, like up in the solution directory. Add it to your projects as a linked item with a relative path to the file. Set the right build action for this item (application configuration) in each project.
Now when each project builds, the file will be copied to the project's output dir with the right name.
You can load an external app.config using the code below:
config = ConfigurationManager.OpenExeConfiguration(Path.Combine("C:\test\root", "Master.exe"));
string logpath = config.AppSettings.Settings["Log.Path"].Value;
And save settings as so:
config = ConfigurationManager.OpenExeConfiguration(Path.Combine("C:\test\root", "Master.exe"));
config.AppSettings.Settings["Log.Path"].Value = "C:\newpath";
config.Save();
You might have to have a master config within one of the applications and point the rest to this. Typically this method is considered bad practice though. There might be issues with different applications locking the file.
#Ran's answer is an option, but each application will still have its own config file after you build. At compile time they will be the same, but at deploy time they are copies.
You can also open one application's config file from another application using:
ConfigurationManager.OpenExeConfiguration(string)
You can have an external config file that all applications reference using:
ConfigurationManager.OpenMappedExeConfiguration
And there's the option to using the Machine config file using:
ConfigurationManager.OpenMachineConfiguration()
I have an asp.net mvc website which of course has a web.config file. I also have an external project which is a class library that uses a .config file for its own app settings. The problem is when I run my web application those external app settings values are not included in the appSettings.
How can I get the external class library projects appSettings values?
You can get the external app setting like that :
var config = ConfigurationManager.OpenExeConfiguration("some.config");
var someKeyValue = config.AppSettings.Settings["someKey"].Value;
You need to either:
1. add those settings to your web.config file.
2. point to the external settings, and use a post build event handler to copy the output into your web project.
<configuration>
<appSettings configSource="my.config" />
</configuration>
Standard convention is that you add the settings to your web.config file. assemblies that are only dlls do not load their own config files. This allows people who use them to specify the settings in their own application.
i am having a classlibrary with app1.config and also a windows application with app2.config;i am adding the reference of classlibrary in windows application as well as app1.config.is it possible if i call the method class lib it will go to app1.config otherwise it will use app2.config;
The best you can achieve is to have two separate configuration files, then have the code of one method read the "main" config file (using the ordinary ConfigurationManager.AppSetting[""] code) and other method read the configuration file of the class library using such code:
Configuration config = ConfigurationManager.OpenExeConfiguration(dllFilePath);
KeyValueConfigurationElement element = config.AppSettings.Settings[appSettingKey];
string value = element.Value;
This will read application setting from the config file of DLL sitting in the location of dllFilePath.
You can also have separate section for the class library in the "main" configuration file if relevant I can give sample as well.
Using the default ConfigurationManager process to access a configuration file it goes for the file that is configured for the application, there is no way to differentiate between a class library and an application's config.
For example if you have a windows/WPF app that is called MyWonderfulApp.exe, the only config file that will be used is MyWonderfulApp.exe.config. Therefore all settings are in that file. Web applications ONLY use the web.config file.