how to include in web.config an external mydll.config file and read its values?

Posted by firepol on Stack Overflow See other posts from Stack Overflow or by firepol
Published on 2010-12-23T10:51:39Z Indexed on 2010/12/23 10:54 UTC
Read the original article Hit count: 301

Filed under:
|
|

Hi,

I found this answer about external configuration files. I'm trying to do a similar thing. I have a small webapplication called StatsGen I want to include in other projects, and for convenience I'd like to have the settings inside the bin folder, in a config file with an appropriate name, in my case: StatsGen.config. So I've put these line in the web.config (as explained in the answer I mentioned above):

<configSections>
  <section name="StatsGenSettings" restartOnExternalChanges="true" type="System.Configuration.NameValueFileSectionHandler" />
  <!--sectionGroups-->
</configSections>

<StatsGenSettings configSource="StatsGen.config"></StatsGenSettings>

<!--and here comes the rest... appSettings etc.-->

Inside the bin folder, I created a StatsGen.xml file, then renamed it to StatsGen.config. It looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<StatsGenSettings>
<add key="Password" value="myStatsPass" />
<add key="ConnectionString" value="Server=mydbsrv;Database=myDB;User ID=myUser;Password=myPass" />
</StatsGenSettings>

I created an Helper class, as suggested in the answer.

In the Page_Load of my default.aspx.cs file, I've put: goodPassword = StatsGenSettings.Instance["Password"];

When I load my page, I get this error:

The type initializer for 'StatsGen.Helpers.StatsGenSettings' threw an exception.

I've tried to exlude the helper and just to get access to the key, like this:

NameValueCollection _settings = ConfigurationManager.GetSection("StatsGenSettings") as NameValueCollection;

And I get this error:

Unable to open configSource file 'StatsGen.config'. (C:\Users\pbo\Documents\Visual Studio 2010\Projects\StatsGen\StatsGen\web.config line 21)

At line 21 I just have this, as explained above:

<StatsGenSettings configSource="StatsGen.config"></StatsGenSettings>

So now I'm wondering, what's wrong? Some detailed help would be cool... like: where exactly should I declare the StatsGenSettings element inside the web.config? It was not specified in the answer I've found... or what else am I doing wrong?

Thanks for letting me know...

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about configuration