OR when you are facing with the following exception:
[LockRecursionException: A read lock may not be acquired with the write lock held in this mode.] | |
System.Threading.ReaderWriterLockSlim.TryEnterReadLockCore(TimeoutTracker timeout) +1347 | |
System.Threading.ReaderWriterLockSlim.TryEnterReadLock(TimeoutTracker timeout) +44 | |
Sitecore.DependencyInjection.ServiceLocator.get_ServiceProvider() +176 | |
Sitecore.Configuration.<>c.<.cctor>b__360_0() +9 | |
System.Lazy`1.CreateValue() +734 | |
System.Lazy`1.LazyInitValue() +189 | |
Sitecore.Configuration.Settings.GetSetting(String name) +48 |
I got the exception above when I tried to access to the Sitecore configuration using Factory.GetConfigNode
or Settings.GetSetting
. As the exception shows, the problem is that Sitecore is locking the service provider while calling the service configurators and this two methods mentioned above are using the service provider to read the Sitecore configuration. Reading Sitecore configurations in service configuration could be also a use-case for you, e.g. getting config values for a constructor in case of you are using a factory. So the way is to read configurations in a service configurator is the following:
public static class SettingsUtil | |
{ | |
// 1. Instead of Factory.GetConfigNode(customConfigNode) | |
public XmlNode GetConfigNode(string customConfigNode) | |
{ | |
return ConfigReader.GetConfiguration().SelectSingleNode($"sitecore/{customConfigNode}"); | |
} | |
// 2. Instead of Settings.GetSetting(name) | |
public string GetSetting(string name) | |
{ | |
var node = ConfigReader.GetConfiguration().SelectSingleNode($"sitecore/settings/setting[@name='{name}']"); | |
if (node == null) | |
{ | |
return null; | |
} | |
return node.Attributes["value"].Value; | |
} | |
} |
The return the value of the ConfigReader.GetConfiguration()
is the full Sitecore config which you can see on the /sitecore/admin/showconfig.aspx as an XmlDocument
. On this object you are able to run XPath queries to read different parts of the XML configuration. More info about XPath query language you can find here.