Fabrice's weblog

Tools and Source

News

My .NET Toolbox
An error occured. See the script errors signaled by your web browser.
No tools selected yet
.NET tools by SharpToolbox.com

Read sample chapters or buy LINQ in Action now!
Our LINQ book is also available on AMAZON

.NET jobs

Emplois .NET

Tuneo

ASP.NET Hosting transatlantys

Contact

Me

Others

Selected content

Archives

Using configSource to split configuration files

As Nikhil Kothari writes in his blog, he's started to use the configSource attribute to split configuration files into smaller pieces.

For example, here is how he exports the profile configuration into a dedicated configuration file.
This is web.config:

...
  <system.web>
    ...
    <profile configSource="profile.config" />
    ...
  </system.web>
...

This is profile.config:

<profile>
  <properties>
    <add name="Name" type="String" />
    <add name="Age" type="Int32" />
  </properties>
</profile>
 

Make sure you use .config as the extension of your files so they cannot be served to the browser. Avoid .xml for example or your files can be available to prying eyes.

I've been using this feature for my web sites for a while. This allows having a unique web.config file, and separate sub-files for things that are different between the development machine and the hosting environment. For example, I have different connectionStrings.config and smtp.config files for the two environments. The benefits: the web.config file is smaller and hence easier to read, and you don't need a complete web.config file for each environment.

Comments

lkempe said:

Fabrice you might also use Visual Studio 2005 Web Deployment Projects to manage build configurations that are stage dependant. I am using it on Tech Head Brothers website for example.

# April 26, 2007 4:14 AM

Chris D said:

Now if only you could point configSource at a parent directory it might be useful.  Quite ridiculous that it doesn't support absolute paths or at least ..\ notation...

# April 26, 2007 9:40 AM

Matt said:

So how do I reference this in my code-behinds?

I am creating a web app and using AppSettings which may be modified by the administrator of the application, but I really want to avoid allowing them to have access to the entire web.config file.

It doesn't look like I can subscribe to this post.  Could you maybe send me an email to matt [dot] schutz [at] gmail [dot] com?

Thanks!

# December 19, 2008 10:23 AM

Fabrice Marguerie said:

Matt, you reference the configuration settings in code in the same way you'd reference them if they were all in a single file (web.config or yourapp.exe.config). This approach only gives you the option of splitting configuration files, but other than that it's transparent from the code's point of view.

# December 19, 2008 2:48 PM

Matt said:

I have discovered something.  I'm not sure if it's a different .NET framework version or just different for appSettings or what, but "file" is the correct attribute for appSettings for me.

[Web.config]

<configuration>

...  

<appSettings file="setup.config" />

...

</configuration>

[setup.config]

<appSettings>

...

</appSettings>

Note that setup.config root is "appSettings", not "configuration".

Hope this helps someone else!  Thanks for the help.

# December 22, 2008 9:24 AM

smhaig said:

Could you show a sample for connectionstrings in app.config for.net 2008.  I am having a beastly problem getting this simple thing to work.  I keep getting "connection string not initialized.  The extension, the path of the connSstrings.config iscorrect.  Does one need to add anything other than this to the app.config?  Or do something other than build the project.

Will the program be able to access the string in the same fashion as when it is physically in the app.config?

<connectionStrings  configSource="connStrings.config"/>

# December 25, 2008 12:57 PM

Fabrice Marguerie said:

I don't do anything more than what you did.

Does your connStrings.config file correctly starts with a <connectionStrings> tag?

Are you sure that this file is correctly deployed in the output directory, where your executable is?

# December 25, 2008 3:59 PM

smhaig said:

I discovered finally the answer to the problem.  The property "Copy to Out put Directory" of the configsource file must be set to "copy always"

I found this at BlueSam Blog.

Now another question I have not yet found an answer to: will this work with User and Application settings.  I tried it and I receive xml errors.  Either this is impossible or I do not have the correct section head.

# December 25, 2008 11:12 PM

Rajul Saxena said:

I am doing the following code

Changes in web.config file

<connectionStrings configSource="ConnectionString.config">

</connectionStrings>

Creating a new file "ConnectionString.config"

put the following code in the new file

<connectionStrings>

   <add name="Test" connectionString="Data Source=[Server IP]; Initial Catalog=TestingDB;User ID=sa;Password=test123;" providerName="System.Data.SqlClient" />

 </connectionStrings>

on my code behind page i am writing the following code

Configuration config = Configuration.WebConfigurationManager.OpenWebConfiguration("~/ConnectionString.config");

       string connString = config.ConnectionStrings.ConnectionStrings[0].ConnectionString;

       DataSet ds = new DataSet();

       try

       {            

           SqlConnection sql = new SqlConnection();

           SqlDataAdapter adp = new SqlDataAdapter("select * from central_audit", sql);

           adp.Fill(ds);

       }

       catch (Exception ex)

       {            

           throw ex;

       }

       return ds;

and facing the error

"The format of a configSource file must be an element containing the name of the section. " :(

Please help me whr i am going wrong

Rajul

# April 20, 2009 8:34 AM

Fabrice Marguerie said:

Rajul,

You don't have to use OpenWebConfiguration. You should be able to use System.Configuration.ConfigurationManager.ConnectionStrings directly.

You'd use OpenWebConfiguration when you want to load a configuration file other than the default one (Web.config), but it has to be in the same format as Web.config. It cannot be used to load partial configuration files such as your ConnectionString.config file.

Also, don't forget to use your connString variable as a parameter of the SqlConnection constructor.

At the same time, please get rid of the useless try..catch block. I hate it when I see code crippled with this kind of thing. You can use Visual Studio's debugger window or evaluate $exception to see the detail of the current exception.

# April 20, 2009 6:05 PM

Dave R. said:

Hi,

I just wanted to say thanks for this tip. It just saved me a lot of duplication between my DLL and App config files :)

# October 9, 2009 10:45 AM

Fabrice Marguerie said:

You're welcome.

# October 9, 2009 11:10 AM

@nt said:

Hi,

Thanks but I'm getting the same error as Rajul.

I am using:

ConnectionStringSettings connectionString = ConfigurationManager.ConnectionStrings["Northwind"];

to retrieve the value so it should work, right?

So why do i receive the error?

Thanks for a great article. Love to get this going.

# February 10, 2010 5:50 AM

jpuckett said:

@nt:

I'm using:

string sqlCn = System.Configuration.ConfigurationManager.ConnectionStrings["SqlConnString"].ConnectionString;

Where my web.config looks like:

<connectionStrings configSource="_resources\configFiles\db.config"/>

Where my db.config looks like:

<connectionStrings>

 <add name="SqlConnString" connectionString="data source=[redacted]" providerName="System.Data.SqlClient" />

</connectionStrings>

# March 23, 2010 2:50 PM