Home All Groups Group Topic Archive Search About

How do I use ConfigurationSettings.AppSettings collection safely from a thread?

Author
15 Dec 2005 5:56 PM
Brad Wood
I've isolated a call to ConfigurationSettings.AppSettings.Get as the
source of an error in my application.

The 1.1 documentation for both the ConfigurationSettings class and the
NameValueCollection base class underlying the AppSettings collection say
that all static members (which I am calling) are thread safe.

What am I missing about accessing ConfigurationSettings from threads?

Author
15 Dec 2005 7:16 PM
Brian Gideon
Brad Wood wrote:
> I've isolated a call to ConfigurationSettings.AppSettings.Get as the
> source of an error in my application.
>

What error are you getting?

> The 1.1 documentation for both the ConfigurationSettings class and the
> NameValueCollection base class underlying the AppSettings collection say
> that all static members (which I am calling) are thread safe.
>

Yes, but AppSettings returns an instance of a NameValueCollection.  Get
is an instance method of the NameValueCollection.

The documentation specifically says instance members on the
NameValueCollection are not thread-safe.  I didn't see anything that
said the collection can support multple readers either.  So technically
that means you need to synchronize access to the AppSettings object
everytime you need to read a configuration setting.  Now, I suspect
it's very likely that the NameValueCollection does indeed support
multiple readers because the documentation also states that the
underlying structure used is the Hashtable which does support multiple
readers.  The documentation should have been more clear though.

> What am I missing about accessing ConfigurationSettings from threads?

Like I said, ConfigurationSettings.AppSettings likely supports multiple
readers so you shouldn't have a problem.  What makes you think the
source of the error is with ConfigurationSettings?

Brian
Author
16 Dec 2005 12:05 AM
Brad Wood
Brian Gideon wrote:
> What error are you getting?

It's in a web service; I get an error accessing the machine.config,
"Attempted to access an unloaded AppDomain" or "Resource lookup failed -
infinite recursion detected".  This I assume due to a deadlock condition.

>
>
>>The 1.1 documentation for both the ConfigurationSettings class and the
>>NameValueCollection base class underlying the AppSettings collection say
>>that all static members (which I am calling) are thread safe.
>>
> Yes, but AppSettings returns an instance of a NameValueCollection.  Get
> is an instance method of the NameValueCollection.

When I call ConfigurationSettings.AppSettings I get a static class.
Then when I call Get, an instance of NameValueCollection returns?

> Like I said, ConfigurationSettings.AppSettings likely supports multiple
> readers so you shouldn't have a problem.  What makes you think the
> source of the error is with ConfigurationSettings?

Because the problem is consistent; when I replace all calls to
ConfigurationSettings.AppSettings.Get with hard coded strings, no
problems...
Author
16 Dec 2005 1:17 AM
Brian Gideon
Brad Wood wrote:
> Brian Gideon wrote:
> It's in a web service; I get an error accessing the machine.config,
> "Attempted to access an unloaded AppDomain" or "Resource lookup failed -
> infinite recursion detected".  This I assume due to a deadlock condition.
>

Hmm...I don't know.  It doesn't sound like a deadlock condition to me.
Can you post the stack trace of each exception?

> > Yes, but AppSettings returns an instance of a NameValueCollection.  Get
> > is an instance method of the NameValueCollection.
>
> When I call ConfigurationSettings.AppSettings I get a static class.
> Then when I call Get, an instance of NameValueCollection returns?
>

AppSettings is a static property on the ConfigurationSettings class.
It returns an instance of the NameValueCollection class.  Get is an
instance method of that class.

> > Like I said, ConfigurationSettings.AppSettings likely supports multiple
> > readers so you shouldn't have a problem.  What makes you think the
> > source of the error is with ConfigurationSettings?
>
> Because the problem is consistent; when I replace all calls to
> ConfigurationSettings.AppSettings.Get with hard coded strings, no
> problems...

Interesting.  I'll be honest.  I really don't know what the problem is,
but it doesn't sound like a threading problem to me.
Author
16 Dec 2005 1:59 AM
Brad Wood
Brian Gideon wrote:
> Hmm...I don't know.  It doesn't sound like a deadlock condition to me.
> Can you post the stack trace of each exception?

Both traces are the same except that the exception messege (same
exception) of the other one is:
[Resource lookup failed - infinite recursion detected.  Resource name:
Arg_AppDomainUnloadedException]

<Trace>
[AppDomainUnloadedException]: Attempted to access an unloaded AppDomain.
    at System.Threading.Thread.SetCompressedStackInternal(IntPtr
unmanagedCompressedStack)
    at System.Threading.Thread.SetCompressedStack(CompressedStack stack)
    at System.Xml.XmlTextReader.CreateScanner()
    at System.Xml.XmlTextReader.Init()
    at System.Xml.XmlTextReader.Read()
    at System.Xml.XmlReader.MoveToContent()
    at System.Web.Configuration.XmlUtil.OpenXmlTextReader()
    at System.Web.Configuration.HttpConfigurationRecord..ctor(String
filename, HttpConfigurationRecord parent, Boolean inheritable, String
path, String mappedPhysicalPath)
[ConfigurationException]: The XML file
c:\windows\microsoft.net\framework\v1.1.4322\Config\machine.config could
not be loaded.  Attempted to access an unloaded AppDomain.
(c:\windows\microsoft.net\framework\v1.1.4322\Config\machine.config)
    at
System.Web.Configuration.HttpConfigurationSystem.CacheLookup(String vpath)
    at
System.Web.Configuration.HttpConfigurationSystem.ComposeConfig(String
reqPath, IHttpMapPath configmap)
    at System.Web.HttpContext.GetCompleteConfigRecord(String reqpath,
IHttpMapPath configmap)
    at System.Web.HttpContext.GetCompleteConfig(String path)
    at System.Web.HttpContext.GetConfig(String name, String path)
    at System.Web.HttpContext.GetMachineConfig(String name)
    at System.Web.HttpContext.GetAppLKGConfig(String name)
</Trace>

AddThis Social Bookmark Button