Web.config values passed through levels
I have a .NET 2008 solution with a project that acts as a host for a WCF service. This project has a web.config file with settings that will be replaced by the installer when the project is finished. These parameters are the components that make up the connection string and several others.
This WCF project references a Business Logic Project (a class library that implements utility code), which in turn references a DAL project that uses the Entity Framework.
What I would like to know is how can I get the values in the web.config project in the WCF for DAL project? Without using any of the relative paths I've seen with OpenMappedExeConfiguration. I need to create a connection string in DAL based on a parameter in the web.config file.
Thank you for your responses.
a source to share
I keep common things like connection strings in the same folder that isn't even in the folder where the source code lives. In the DAL layer, I just use the ConfigurationManager to bring it up.
In the project that runs the application (in your case it is a WCF project), I add the "ConnectionStrings.config" file from my external "config" folder AS LINK (in visual studio, click "add existing item" → select item → next to the button "Add" is the arrow where this variant lives). Then I just set it through the properties of that file (click on the file in the solution explorer -> press F4) as the content of the project and that it must be copied again if changed to expand the folder. Then I add a new app.config file for the project that includes "ConnectionString.config".
Connectionstrings.config source:
<connectionStrings>
<add name="MyConnectionString"
connectionString="Data source=tralala"/>
</connectionStrings>
App.config source in WCF project:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings configSource="ConnectionStrings.config"></connectionStrings>
</configuration>
I'm not sure if this is the best approach. But so far so good.
a source to share
Unfortunately, the answer to your question is "copy and paste". This has always been true.
The closest exception to this rule is the "new" .NET 2.0 configuration files. Since the structure and default values for them are part of the assembly that defines the component, the component, when launched, can cause the default values to be written to the application configuration. I suppose one could link this with a piece of code to work with installutil to force it to write out the defaults before the containing application is launched, leaving the defaults in the config file to be changed before the application is used for the first time.
a source to share