I’ve just written a test service in C# 2010. In the OnStart method i’m opening an XML document in the root of C: drive, parsing it and writing out to another XML document also in the root of C: drive.
When I install the service and start it, im told it stopped again automatically. The output XML file is created but is empty. I’m running the service as Local System account.
Can anyone tell me why no content is being written?
Thanks,
EDIT (to include code for OnStart)…
protected override void OnStart(string[] args)
{
String win32ClassName = "";
String nodeSubkeyName = "";
List<String> propertyList = new List<String>();
List<String> propertyListQuery = new List<String>();
XmlTextReader reader = new XmlTextReader("C:\\hwin.xml");
XmlTextWriter writer = new XmlTextWriter("C:\\hwout.xml", null);
writer.WriteStartDocument();
writer.WriteComment("Asset hardware Inventory for " + System.Environment.MachineName);
writer.WriteStartElement("hardware");
The above code doesn’t even write the starting element for the output XML file, but it does create the empty file so suspect it has rights to do that. Perhaps reading the input file IS the issue? I’ve little to no idea how the Local Service account works!
Probably an unhandled exception….
Use Try/Catch, write exception to system.diagnostics.trace.writeline and use DebugView(http://technet.microsoft.com/en-us/sysinternals/bb896647) to read the output.