I have a Windows application called TechReader. Its configuration file is TechReader.exe.config. Some parts of the configuration are kept in other configuration files. So I refer to that config file from the config section created in the exe.config.
<TechReader.ProviderConfiguration file="localProvider.config"/>
Now I want to load the whole configuration of my application using reflection. I use code like this.
Assembly techReaderAssembly = Assembly.GetAssembly(typeof(TechReaderStarter));
ConfigurationManager.OpenExeConfiguration(techReaderAssembly .Location);
TechReaderStarter class is defined in the project whose output is windows application and not the library.
When I use above code, I get TargetInvocationException and ConfigurationErrorsException
Is the approach correct?
Will OpenExeConfiguration load the final configuration generated by merging the exe.config and other referenced config file?
How can I achieve the things?
Note: I want to use this Windows application to install as a Windows Service. I am trying to read the configuration in the ServiceInstaller class (a class which inherits ServiceInstaller) so that the details like service name mentioned in the configuration of the Service will be available to the installutil. For this I have to use reflection to get the exact configuration of the service.
Related
I gone through lot of threads about this topic but did not get any solution.
First I created a windows application. In that I used app.config file to store some variables it worked properly. Then the application converted to class library. While extracting the varibles it gives only null values.
Any other option to get or set variables in config file in the class library
Thanks.
If you converted you code to some class library you have to remember that it will reuse web.config/app.config of an application, which references it. So I would suggest to add settings from "old" app.config to the configuration file of an application, which uses your library.
We can not use app.config or web.config in class library. Add app.config for Console/Windows application or web.config file for your web application which calls your class library then application will automatically takes and uses that configuration file.
I have a Windows Service that I'm creating and I'm wondering what options are available in order for me let developers configure the service.
The service is part of an over all larger open source project and hence the service is going to be installed on lots of different machines.
Normal I would use a web/app.config for this but I'm not sure if this is possible.
Hence I am looking to so how others handle this case.
you do as you expect. You use the app.config, which will be renamed to <exeName>.configwhen the project is built and then <exeName>.config will be read by the service called <exeName>.
Settings are applied in a layered way and may come from other configuration files on the machine, such as machine.config. You can read about how configuration is handled on MSDN
EDIT
In response to comment: A service will only read the config when it starts (for perf reasons). If you want to reload the config file later, you need to handle that yourself I think.
You could read the last modified date/time of the config file to determine if the file has been changed, or setup a file system watcher and then tell the configuration manager to reload that section again next time it is read, by calling ConfigurationManager.RefreshSection("appSettings") and that section will be reloaded from disk when you next access it. See the ConfigurationManager MSDN docs
You can just use a .config file with the same name as the exe that is the service.
If your service runs as MyService.exe, it's config file would be MyService.exe.config.
In Visual Studio, just add an Application Configuration file. This will add an app.config file to the project.
You can then access things like AppSettings and ConnectionStrings using the ConfigurationManager class, just like you do with ASP.Net applications.
I am creating one class library project.
Now by default I have one App.Config file so that I am putting all environment specific data in that Config file.
Now based on the Environment (whether Dev / Test / Production), I am planning to have three App.Config files in VS 2010 such as
App.Dev.Config
App.Test.Config
App.Prod.Config
Wondering how would the application know which config file to use.
Anyone implemented this scenario. Any Code samples / Articles would be helpful.
Thanks
The app will use the config file named YourExcecutable.exe.config which is by default the file App.config included in your (executable) project.
Note, that .NET only loads one config file for the whole application. You cannot use multiple configuration files (i.e. one per library project) without coding.
Option: You can use postbuild events and different solution configurations to copy one or another App.Config file to the output folder
Option: You can use the ConfigurationManager Class to load an alternate config file by code.
Loading a different application configuration file at run time can be done using the concept of mapped configuration file. To start with, you need to add reference to System.Configuration.dll in your project.
Set the value of Copy to Output Directory property to Copy if newer (Refer screenshot). This has to be done only for non-default configuration files e.g. App1.config, App2.config, etc. Leave the default configuration file namely App.config as it is . Due to this change, all the non-default application configuration files will be available in the project output directory (\bin\debug) when the project is built. Default value of this property is Do not copy.
Here is the code snippet on how to read configuration data from non-default configuration files:
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = "App1.config"; // app1.config should be present in root directory from where application exe is kicked off
// Get the mapped configuration file
var config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
//get the relevant section from the config object
AppSettingsSection section = (AppSettingsSection)config.GetSection("appSettings");
//get key value pair
var keyValueConfigElement = section.Settings["appSettingsKey"];
var appSettingsValue = keyValueConfigElement.Value;
If you have multiple application (aka app) configuration files then you can keep a setting in default App.config file with the help of which you can make a decision at run time about which non-default configuration file to load e.g. App1.config
Note: Now look at below code:
ConfigurationManager.AppSettings["DeployEnv"]
This code will still read the data from the default App.config file. This behavior can't be changed. There is no way to prohibit the Loading of default App.config file. You have to use alternate means as discussed in this post to read the data from non-default configuration files
Now there is an even better solution: SlowCheetah - XML Transforms
I got a solution that includes 3 different projects, a Class Library, a Web Service and a Windows Service.
MySolution.DataAccessLayer (Class Library)
Using Entity-Framework 3.5 to access the database.
MySolution.WebService (Web Service)
MySolution.WindowsService (Windows Service)
My problem is that a have to include the Connection String in my Web Service/Windows Service configuration files to get it to work.
I have checked and I have the correct Connection String in my DataAccessLayer App.Config.
Why does it not use that?
It will not run that, because the DLL is loaded in the process space. Meaning that the services load the DLL and it is the service config that is used.
In other words - the service starts, loads the config file (that belongs to it), then loads the dataaccess DLL. As it already has its own config, it will not look further for the DLL config.
The config system looks up the app/web.config for the app that's running, regardless of where in the code you look up the configuration.
The class library doesn't care that it's a class library/seperate .dll when it's running, and will just look up configuration in the app.config for the program that's running - which will be the config for your Windows service in one case and the config for your Web service in the other.
I have a config file in my C# class library called MyLibrary.config, in vs 2008.
I created another project, say a simple console app, add reference by "Browsing" the MyLibrary.dll in the bin directory of the class library project, and when I compile, the MyLibrary.config is not including in the bin directory of the output in the console app.
How can I set it so I can include it when I reference the dll?
Cheers
You can't. Your console application is expecting to find a config file with prefix the same as the name as the console application (MyConsoleApplication.exe -> MyConsoleApplication.exe.config.).
In some situations you can share a config file by using the file attribute on the appSettings element:
<appSettings
file="path">
</appSettings>
Note that path is relative to the executing assembly.
As a side note, DLLs do not even use the config file that you've defined in the project. Again, configuration information is read from the a config file with prefix the same as the executing assembly. Thus, even when MyLibrary.dll tries to yank configuration information out of a config file, it will be reading the config file for the executing assembly, not MyLibrary.dll.config.
For more on how config files work, see MSDN.
The standard way to use a config file is to have it the same as the executable, adding a reference to a dll will not include its config file and as far as I know dll's don't load config files on their own, rather they rely on the executable that reference them.
Beyond not being able to do this, I would also advise against this approach.
Rather than trying to tighly couple your settings to the DLL library, consider more of a "Dependency Injection" type approach - i.e. where you pass in the value dependencies (i.e. settings) to the code you are calling (i.e. the library).
The advantage in this is you are not tied to a single method of storing settings. You can then use a database, different config file formats... even hard-coding. It also makes unit testing easier by removing the external file dependency.
There are different ways to implement this, however one example is to pass the configuration settings into the constructor of the class(s) that uses them.