I have an app.config file for a windows service which includes database connection strings and appsettings. when I install the windows service, it gets installed in "C:\program files\" folder and the settings are copied to a file called ".exe.config" in the same folder, which makes it difficult to change the settings after it is deployed to test environment. can I have all settings stored in an external file somewhere on shared network drive, instead of the same folder where the service is installed? is that possible? only alternative I can think of is to create an xml file and read it using the .NET XML API, another issue is I need to watch for the changes in the file and reload the settings in the service.
Linq to XML makes it fairly easy to read an external settings file. You can also use a FileSystemWatcher to watch for changes in the settings file.
You can push some of your config settings out of your core config file, e.g.:
<connectionStrings configSource="connectionStrings.config" />
And:
<?xml version="1.0" encoding="UTF-8"?>
<connectionStrings>
</connectionString>
That way, as long as you don't deploy the connectionStrings.config file, you can safely deploy the app and maintain those settings separately. The good thing about this approach, is you don't need to create a whole new method of getting config data, this is built into the configuration system, just read these settings how you do currently.
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
I am working on an existing client application. When this application is deployed by the deployment team it has a generic exe.config file.
I have created an application which gives user choices and based on their choices it creates the correct exe.config file. Now I just need to replace the generic exe.config file with my specific exe.config file and then launch the application.
My application works fine if I have write access to the folder where I have all the dlls and exes of the client application. I was replacing the generic config file with the newly created config file and then launching the application and it all worked fine.
Now the deployments team is planning create the folder inside Program Files folder to which my program wont have write permission to.
In this case I am looking for ideas how to point the generic config file to the specific config file which my program has created.
Anu ideas will be appreciated.
Thanks in advance.
Perhaps you can code the generic config file to reference the specific config file with the file or configSource attributes?
For Example:
generic config file
<configuration>
<appSettings file="..\..\specific.config"></appSettings>
<configuration>
specific config file
<appSettings>
...
</appSettings>
More info on file and configSource here: http://msdn.microsoft.com/en-us/library/vstudio/ms228154(v=vs.100).aspx
Suppose a project has both App.config and Web.config files with some conflicting information. Are there any rules which of the files has higher priority? Or an application has to disregard both of them and throw an exception?
From MSDN for .NET 4.5:
When configuring a service in Visual Studio, use either a Web.config file or an App.config file to specify the settings. 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. For more information about configuration and the setting priorities, see topics in the System.Configuration namespace.
Here's also a great post for those using MS Azure, explaining the differences b/w ApplicationSettings, appSettings (app.config/web.config) and ConfigurationSettings (.csdef / .cscfg):
http://haishibai.blogspot.com/2012/09/windows-azure-cloud-service.html
I have several projects some of which are web applications and some are windows applications.
I wanted to globalize my settings, something like Connection Strings because all the projects use the same connection string.
So I followed this example How to share custom application configuration settings across projects in .NET
my XML file is in the class library and that is referenced in each project. The windows project are working fine with the file path which is something like this in the client project:
<configuration>
<appSettings file="F:\Classes\ConnectionStringFile.xml">
</appSettings>
</configuration>
I also want to be able to use this xml file from a web project. however, the web project would not accept the path to this file as shown earlier.
What is the best approach or say, best solution to have this connection string file shared amongst web projects as well as windows projects. What do i pass in
something like <appSettings file ="~/classLibrary/ConnectionStringFile.xml" ???`
The solution we use is to store shared settings in a custom folder beneath the CommonApplicationData folder (e.g. C:\ProgramData on Windows7):
var commonAppData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
var settingsFilePath = Path.Combine(commonAppData, "MyProgram\\Settings.xml");
file= accepts relative paths, and *.config files are excluded from ASP.NET and IIS static file rules, so it is safe to drop any *.config file into an ASP.NET site. Just put it next to the web.config file (root of the web app), mylibrary.config, and file="mylibrary.config".
I am creating a windows service in .Net 4.0 and testing some functions of said service with a windows forms client by referencing the service project.
The service project has an App.config file and that file looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<clear />
<add name="myLocalMySQLDBUsername" connectionString="username"/>
</connectionStrings>
</configuration>
When a function belonging to the service calls:
ConfigurationManager.ConnectionStrings("myLocalMySQLDBUsername").ConnectionString
a null reference error is thrown because my connection string is not loaded. The only connectionStrings that are loaded are from the machine.config file located in c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Config\machine.config
If I create an application scope setting for the service, I can get that setting by using the My.Settings.setting -> so it's not like the App.config file is not being read.
My question is: why are my connectionStrings not being loaded from the App.config file?
Thank you for your help.
UPDATE:
Also, at this point, even a work around would be appreciated; the only reason for using app.config is to be able to encrypt the contents using the DpapiProtectedConfigurationProvider (the contents will have some username/password values for service and database connections).
I tried creating an AppSettings section manually in the app.config but those settings were also not read by the configurationManager (count = 0).
UPDATE 2:
Per a suggestion, I tried to manually open the app.config file like so:
Dim exePath As String = System.IO.Path.Combine(Environment.CurrentDirectory, "ServiceName.exe")
Dim myConfig As Configuration = ConfigurationManager.OpenExeConfiguration(exePath)
So here is the weird part, when I look inside, path is correct (points to my app.config) but the connectionStrings are still being loaded from the machine.config file (my connectionStrings are not loaded)!! ARGH
UPDATE 3:
Okay, so, I figured it out. When referencing a project(parent) from another project(child), the child's app.config is used even if the parent's classes are being used. Thus, I can get the connectionStrings to show up if I copy them over to the child's app.config. When trying to open it manually, my currentDirectory was of the child, not the parent (strange how it did not throw an exception - it wouldn't have been able to find the config file ... it just silently used the machine.config ... oh well).
Thanks all for the help!
The first thing you'll want to do is make sure that the service account has access to the file (if not running as SYSTEM). It sounds like it should be ok though since you mention My.Settings.Setting works.
The other thing to look out for is to make sure that the app.config has the name of the service executable in it - so if the service exe is MyService.exe the app.config must be named MyService.exe.config.
The last thing to make note of: libraries will read from the executable's app.config that loads the library, not the app.config that is with the library. So if you have a project for the service executable MyService and a project for the library MyServiceLibrary the code in the library will read the app.config from MyService not MyServiceLibrary.
I saw some people say this problem might be fixed by manually re-adding a reference to System.Configuration.dll
SIDE NOTE: If that really is you whole app.config file and not just a snippet then that is your problem... you're app.config file should be MUCH more complicated or .NET will not be able to load it.
WORK AROUND: Use the configuration manager to open this config file (there is an API for that.) You can't get it to load auto-magically just tell the config manager to open it.
I still think the problem is your config file is invalid -- could you please post the FULL file?
Make sure the config file is deployed to the same folder as the executable file, and that it's called your.assembly.exe.config.
I had a similar problem, but in my case it was because I had changed the project's namespace. This is also used as the application settings section element name in the config file, so the code was not finding the new section name. Fiddling with one of the custom setting's values in the project properties and rebuilding caused the new section to written into the app.config alongside the old ones which was what indicated the issue to me.