Referencing web.config from another assembly

T

Torrance S.

Hi,

I have a class in an assembly that references a value in web.config
like this:
<appSettings>
<add key="mykey" value="myvalue"/>
</appSettings>

string temp = System.Configuration.ConfigurationSettings.AppSettings.Get("mykey");

This call works properly as is. However, if I call this class from
another assembly (by placing the assembly containing this class in the
calling assembly's references directory), the call fails with a
NullReferenceException. I can reference everything else in the
imported assembly, without any trouble, as you might expect.
It is almost as if an imported assembly can not read its own
web.config file.

I can abstract the values from out of the web.config and place them in
an external xml file, but I would rather not do this. I would prefer
to use the web.config. Any suggestions?

Thanks in advance,
Torrance S.

Env:
..NET IDE 2002 (v 7.0.9466)
..NET Framework 1.0 (v. 1.0.3705)
C# Web Project on W2k Pro
 
P

Pete Davis

You have a separate assembly in the same directory, but the separate
assembly is a separate application. The web.config is specific to the
application. The web.config file is however, simply an XML file, so you
should be able to parse it easily enough, though there is probably a
permissions issue involved. Would it not be easier simply to keep your keys
in a separate XML file and have both assemblies access the XML file?

Pete
 
P

Philip Rieck

The "calling assembly"'s web.config will be used.

System.Configuration.ConfigurationSettings.AppSettings.Get("mykey") will
return "myke" from the current APPLICATION's config file. If this is a web
page, then it will use the current web app's config file (web.config).

The referenced assembly has no notion of its "own" configuration file. When
you reference it from an app, that's the application it belongs to.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top