deshaw
asked on
Exception occurred while reading xml configuration file from windows service
We have below xml configuration file.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<SQLServers>
<add name="PERTRACQADB"/>
<add name="DBNYC1"/>
<add name="IRDB"/>
<add name="IRTRACKERDB"/>
<add name="LPCAPDB"/>
</SQLServers>
<PerTracServices>
<add name="CMS-Email-Auto-Log"/ >
<add name="CMSImporter3SVC"/>
<add name="DESVCDIR 2x5"/>
<add name="SSUPDATES 2.0"/>
</PerTracServices>
</configuration>
We are using C# code to read the configuration file as in code snippet.
We can successfully run the code to load the configuration file as an individual application but we are getting the following exception while we are running it as windows service and the service has been stopped abnormally.
EventType clr20r3, P1 pertracmonitor.service.exe , P2 1.0.0.0, P3 49d9e873, P4 system.configuration, P5 2.0.0.0, P6 461ef187, P7 1a6, P8 136, P9 ioibmurhynrxkw0zxkyrvfn0bo yyufow, P10 NIL.
Service cannot be started. System.Configuration.Confi gurationEr rorsExcept ion: Configuration system failed to initialize ---> System.Configuration.Confi gurationEr rorsExcept ion: Unrecognized configuration section SQLServers. (C:\Program Files\PerTracMonitor\Setup \CustomAct ions\PerTr acMonitor. Service.ex e.config line 3)
at System.Configuration.Confi gurationSc hemaErrors .ThrowIfEr rors(Boole an ignoreLocal)
at System.Configuration.BaseC onfigurati onRecord.T hrowIfPars eErrors(Co nfiguratio nSchemaErr ors schemaErrors)
at System.Configuration.BaseC onfigurati onRecord.T hrowIfInit Errors()
at System.Configuration.Clien tConfigura tionSystem .EnsureIni t(String configKey)
--- End of inner exception stack trace ---
at System.Configuration.Clien tConfigura tionSystem .EnsureIni t(String configKey)
at System.Configuration.Clien tConfigura tionSystem .System.Co nfiguratio n.Internal .IInternal ConfigSyst em.GetSect ion(String sectionName)
at System.Configuration.Confi gurationMa nager.GetS ection(Str ing sectionName)
at System.Configuration....
Can anyone please help me to resolve this.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<SQLServers>
<add name="PERTRACQADB"/>
<add name="DBNYC1"/>
<add name="IRDB"/>
<add name="IRTRACKERDB"/>
<add name="LPCAPDB"/>
</SQLServers>
<PerTracServices>
<add name="CMS-Email-Auto-Log"/
<add name="CMSImporter3SVC"/>
<add name="DESVCDIR 2x5"/>
<add name="SSUPDATES 2.0"/>
</PerTracServices>
</configuration>
We are using C# code to read the configuration file as in code snippet.
We can successfully run the code to load the configuration file as an individual application but we are getting the following exception while we are running it as windows service and the service has been stopped abnormally.
EventType clr20r3, P1 pertracmonitor.service.exe
Service cannot be started. System.Configuration.Confi
at System.Configuration.Confi
at System.Configuration.BaseC
at System.Configuration.BaseC
at System.Configuration.Clien
--- End of inner exception stack trace ---
at System.Configuration.Clien
at System.Configuration.Clien
at System.Configuration.Confi
at System.Configuration....
Can anyone please help me to resolve this.
private static void ConfigReader()
{
try
{
string[] splitedPath = System.Reflection.Assembly.GetEntryAssembly().Location.Split(new char[] { '\\' });
string parentDir = string.Join("\\", splitedPath, 0, splitedPath.Length - 1);
string configFile = parentDir + "\\PerTracMonitor.xml";
if (!File.Exists(configFile))
{
msgBody = "Configuration file not found.";
sendEmail();
return;
}
XmlDocument xml_doc = new XmlDocument();
XmlNodeList childNodes = xml_doc.GetElementsByTagName("SQLServers");
foreach (XmlNode cNode in childNodes)
SQLServers.Add(cNode.Attributes["name"].Value);
childNodes = xml_doc.GetElementsByTagName("PerTracServices");
foreach (XmlNode cNode in childNodes)
PerTracServices.Add(cNode.Attributes["name"].Value);
}
catch (System.Exception ex)
{
msgBody += "Exception occured while reading config file.\n";
msgBody += ex.Message;
sendEmail();
}
}
ASKER
Thanks for your suggestion.
But I want to have two sections namely "SQLServers" and "PerTracServices" and I did not find the way to have sections in <appSettings>. Could you please tell me how I can define the sections using <appSettings>.
I would be great full if you can find the reason for the exceptions while I am running the given code as a windows service.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you.
I have tried the code. But it also gives the same problem. It works well if we are using it in an individual application but it is not getting values from the configuration file while running as windows service. It does not give any exceptions.
I have tried the code. But it also gives the same problem. It works well if we are using it in an individual application but it is not getting values from the configuration file while running as windows service. It does not give any exceptions.
I have similar code and it works, very odd. So for each service you have added the App.Config to the solution? Services wouldn't really throw exceptions, they are more silent annoyances.
Try declaring this at the top of your service:
private static readonly string _sqlServers =
ConfigurationManager.AppSe ttings["SQ LServers"] ; etc.
Try declaring this at the top of your service:
private static readonly string _sqlServers =
ConfigurationManager.AppSe
ASKER
Service is not throwing any exception but it is getting stopped immediately and I could see the reason for that in Application event log. It says the given exception in my main thread.
Thanks.
Thanks.
Yes, the exception causes your service to stop running. Is your Config file formatted correctly?
ASKER
thanks
<add key="ConnectionString" value="YourConnectionStrin
Then just call it:
string connection = ConfigurationManager.AppSe
Then you have your string easy.