I've got a solution with 4 projects (Windows Service, Windows forms, Web and Shared code).
I would like all 4 apps to be able to write and read the database connection settings (server, db name, credentials) to an area where all apps can read it.
Currently it is being saved in the config file, but the Shared code app does not save it it's own config file, but in the app.config and web.config files respectively.
Is there a way that I can save it in a general space to be accessible by all?
Create another configuration file in the parent of all these apps in which you'll store the connection string.
Then customize the loading of the config file to take this file into account.
The appSettings element can contain a file attribute that points to an external file. I will change my web.config file to look like the following:
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="false" strict="false" explicit="true" />
</system.web>
<appSettings file="../externalSettings.config"/>
</configuration>
Next, we can create the external file "externalSettings.config" and add an appSettings section with our connection information and any other settings that we want to use.
Reading connection string from external config file
You would also need to save the settings you want in that particular file
Related
I have developed a small windows service which performs few database operations. I have to give an option to the user to change server name on post-deploy. if user changes in app.config it doesn't affect the service, it is still reading connection strings from AppName.exe.config.
Here what I have tried.
<connectionStrings>
<add name="testString" connectionString="Data Source=ServerAddresss;Initial Catalog=DatabaseName;Integrated Security=True;" />
</connectionStrings>
C# code,
ConfigurationManager.ConnectionStrings["ProjectName.Properties.Settings.testString"].ConnectionString);
This returns a server connection string from AppName.exe.config file but I want to access it from App.config file.
can someone help me out in this?
App.config and AppName.exe.config aren't exactly separate - AppName.exe.config is the build output of app.config.
In other words, once the service is deployed, there shouldn't even be an app.config to change, and if there is, changing it won't do anything. AppName.exe.config is where your application gets "app.config" values.
If you need different configuration values for different environments, take a look at config transformations. They allow you to change or replace sections or individual values for different configurations such as debug, release, or other custom configurations.
This is useful because it means that all of the connection strings are part of the project and are in source control. You can do without this and just edit the config file in place on the server, but then there will be nothing in source control indicating where the connection string came from. And then if you redeploy the service the change will be overwritten unless someone remembers to make the same change every time. Having it in source control is much better.
For some reason that I do not understand, you can right-click a web.config and select "Add Config Transforms", but you can't do that with an app.config. Maybe there's a good reason.
You can install this extension which enables the same behavior for app.config. Then you can right-click on app.config and add a transformation for another configuration, like Release.
In that file (app.Release.config) add this:
<connectionStrings xdt:Transform="Replace">
<add name="testString" connectionString="...your Release connection string..." />
</connectionStrings>
When you build and deploy using the Release configuration it will replace the connectionStrings section with this one, leaving everything else just as it is. You can also right-click app.Release.config and select "Preview Config Transforms" to see the transformed file side-by-side with the original.
You should be able to just pull in an arbitrary file with a ConfigurationFileMap
System.Configuration.ConfigurationFileMap fileMap = new ConfigurationFileMap(strConfigPath); //Path to your config file
System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
This question is a possible duplicate of: Using ConfigurationManager to load config from an arbitrary location
Also, you'll have to be sure that the service is restarted after updating the config file (either config file).
I have an asp.net project that is using Entity Framework that is used on several different client servers. This means a different web.config for each for connectionstrings and app settings.
Hasn't been an issue but I changed something that altered the web.config file recently and I manually had to adjust this for each client, I also have to exclude the web.config file in updates to ensure their own one is not overwritten.
What I would like to achieve is store these settings in maybe another config file that the project can pick up and use. Maybe that on Globals Application_Start gets these and imports them/overwrites the current web.config file perhaps.
Essentially I don't want to affect the current code that uses the connection string and ConfigurationManager.AppSettings used throughout the project but I do want to be able to let the web.config file update for each client and use a seperate file for some settings.
NOTE: I do not have access to publish directly to each server so I can't simply write a different deploy web.config for each one. I have to publish the files locally, store as zip and automated routine on servers downloads and extracts accordingly.
EDIT:
Please do say if this is considered a bad idea but an idea I had was to put something similar in Global.asax Application_Start method:
Does config file exist?
If no, create file and append all current settings in web.config
If yes, open and import those settings to the current web.config overwriting the original values
Hopefully then in a few weeks time, after I have asked all clients to perform a manual update they will have this code and I can begin to include the web.config in updates.
In VS, inside the Build menu, the last item is Configuration Manager.
In here you can specify various different release environments which can each have their own web.config transforms.
This is normally used for production/staging/test environments. However, I can see no reason why you could not use this and have a configuration file for each of your servers/environments.
You will then need to create the transformations for each environment, by rightclicking the web.config, then selecting Add Config Transform.
each of the environments you had setup can then override the settings in the main web.config. (That now acts as a template/default settings)
e.g.
In Web.EnvironmentA.Config
<add key="ConnectionString" value="ConStringA" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
In Web.EnvironmentB.Config
<add key="ConnectionString" value="ConStringB" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
Then when you do a publish, you simply setup which config file to use. If you use Azure or the VS publish method, it will save each of these settings, so then you can simply push to the relevant environment easily.
Just make sure you test this works as you intent first, before you start publishing everywhere ;)
I currently have three projects in my solution that all have their own App.config file with the same exact connection string.
Is there a way to consolidate the connections strings / app.config files so that I only need to make changes to one location?
You can share the connection strings among multiple projects in a solution as follows:
Create a ConnectionStrings.config file with your connection strings under a solution folder, this file should contain only the section connectionStrings
In your projects, add this config file As a Link (add existing item, add as link)
Select the added file and set its property Copy to Output Directory to Copy always or Copy if newer
In the App.config of your projects, point to the linked ConnectionStrings.config file using the configSource attribute:
<connectionStrings configSource="ConnectionStrings.config" />
ConnectionStrings.config
<connectionStrings>
<add name="myConnStr" connectionString="Data Source=(local); Initial Catalog=MyDB;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
</connectionStrings>
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
...
<connectionStrings configSource="ConnectionStrings.config" />
...
</configuration>
Read more details....
There's a few ways you could do it:
Put common configuration settings in machine.config as shown here
Put common configuration settings in a central file and link to that in each projects's app.config as shown here
Store the configuration settings in the registry
For me, i always work with the last solution :)
Good luck!
First, take a look at this post. It describes how you can share the same app.config between multiple projects.
How to Share App.config?
Second, take a look at one other post, which describes how you let different app.config-files have a reference to one single shared xml-file which contains the connection strings.
Use XML includes or config references in app.config to include other config files' settings
I have my Entity Connection String set in my Web.Config file. In the same solution, I have a Windows Application, and I want to access the Connection String of my Entity. How can I access the connection string that exist in my Web.Config file? Or I have to create a specific one in my Windows Application ?
If you're unfamiliar with adding a config file to a windows app, you must select an "Application Configuration File" from add new items, it will create a file named App.Config which will at compile time become YourProgramsName.exe.config but internally it works mostly just like a web.config with an appsettings section and connectionstrings sections and all the other normal sections.
The best way to share this data between Web and Win config files would be:
Create a separate file that looks like:
<connectionStrings>
<add name="Name"
providerName="System.Data.ProviderName"
connectionString="Valid Connection String;" />
</connectionStrings>
and reference it from both config files with:
<?xml version='1.0' encoding='utf-8'?>
<configuration>
<connectionStrings configSource="connections.config"/>
</configuration>
Then just add the connections.config (add existing item by link) to each project.
More reading: http://msdn.microsoft.com/en-us/library/ms254494.aspx
Put the connection string your windows application's App.config.
use this code:
string connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
I'm using a config file in my Library project in order to associate the interfaces with their own classes; I'm having troubles since my Application can't load anything from the config. Here is a sample from the config file,which is called app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Sic2Lib.it.carrefour.sic.profiler.datasource.def.AuthenticateDS" value="Sic2Lib.it.carrefour.sic.profiler.datasource.impl.AuthenticateDSImpl"/>
<add key="Sic2Lib.it.carrefour.sic.profiler.datasource.def.CheckUserDS" value="Sic2Lib.it.carrefour.sic.profiler.datasource.impl.CheckUserDSImpl"/>
<add key="Sic2Lib.it.carrefour.sic.profiler.datasource.def.ReadApplicationConfigDS" value="Sic2Lib.it.carrefour.sic.profiler.datasource.impl.ReadApplicationConfigDSImpl"/>
Which is held in the same directory of the DataSourceFactory Class. This class is supposed to take the setting through the command
NameValueCollection keys = ConfigurationManager.AppSettings;
So, I build the project with no errors and I get a file called myProject.dll.confing in the bin/Debug folder. But after all this I always get the keys variable empty...How come? What's wrong with what I've done so far?
Library projects do not have their own configuration - they use the configuration from the application that uses the library.
Put the configuration settings in the configuration file of the application project and you should be fine.