Debugging a Windows Service

A

Andrew Falanga

Hi,

I'm writing a windows service, that uses a WCF service (the server
side). The WCF service simply opens a TCP port to wait for clients to
connect, etc. I put the Open( ) call in the OnStart( ) function and
likewise the Close( ) call in the OnStop( ) function. This seemed the
proper course when making the windows service (I used the C# template
for this). I used the example on MSDN for making a WCF service, which
is a command line app, and then simply used those calls assuming that
they start and stop the server.

Now, I used MSDN to setup an installer deal and then used
"installutil" to install my service. Lo and behold, my service is
listed in the system services. So, I start it up. This works and I'm
able to run my test driver program against it and everything works. I
tried to stop it and I get an error. Trying to eliminate variables, I
restarted the box and restarted my service (it's manual start). Then,
I stopped it without making any connections. I still got the same
error. The error is:

Could not stop the MyService service on Local Computer. The service
did not return an error.

Now, I'm confused because the OnStop( ) function is defined as
returning type void, i.e. it has no return. So, why is this error
happening? How should I fix it?

Andy
 
S

sloan

Hint #1


Windows Service In General Hint:

In your "Start" method:

//start code
AppDomain currentDomain = AppDomain.CurrentDomain;

currentDomain.UnhandledException += new
UnhandledExceptionEventHandler(UnhandledExceptionHandler.HandleUnhandledException);

//end code



Register for that event.


namespace MyNamespace

{

public class UnhandledExceptionHandler

{

public static void HandleUnhandledException(object sender,
UnhandledExceptionEventArgs args)
{

Exception e = args.ExceptionObject as Exception;

if(null!=e)
{
Console.WriteLine("Host HandleUnhandledException Event Fired
: " + e.Message);
LogException(e);
}

}





private static void LogException(Exception error)
{
try
{

StringBuilder errorMsg = new StringBuilder();

while (null != error)
{
Exception e = error;
errorMsg.Append(error.Message + " \n\r\n\r ");
errorMsg.Append(error.StackTrace + " \n\r\n\r ");
error = e.InnerException;
}




try
{
string exceptionLogDirectory = string.Empty;

exceptionLogDirectory = @"C:\WUTEMP\"; // Give permissions to the
"SYSTEM" Account!!!!!

//You can read the above directory from an app.config file as well

if (exceptionLogDirectory.Length > 0)
{
string fileName = exceptionLogDirectory +
"HOST_UNHANDLED_" + Guid.NewGuid().ToString("N") + ".txt";
WriteToTempFile(errorMsg.ToString(), fileName);
}

}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}



try
{

string applicationName;
string logType;
string eventType;

applicationName = "MySuperCoolApp";
logType = "Application";


if (!EventLog.SourceExists(applicationName))
EventLog.CreateEventSource(applicationName,
logType);

EventLog.WriteEntry(applicationName,
errorMsg.ToString(), EventLogEntryType.Error);

}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}


}




}


private static string WriteToTempFile(string toWrite, string
fileName)
{
System.IO.StreamWriter writer = null;
System.IO.FileStream fs = null;

try
{


fs = new System.IO.FileStream(fileName,
System.IO.FileMode.Append, System.IO.FileAccess.Write);
// Opens stream and begins writing
writer = new System.IO.StreamWriter(fs);
writer.BaseStream.Seek(0, System.IO.SeekOrigin.End);
writer.WriteLine(toWrite);
writer.Flush();

return fileName;

}
finally
{
if (null != writer)
{
writer.Close();
}
if (null != fs)
{
fs.Close();
}
}
}









}
}




HINT #2





Wire up this in your config settings for WCF





<system.diagnostics>

<trace autoflush="true" />

<sources>

<source name="System.ServiceModel"

switchValue="Information, ActivityTracing"

<listeners>

<add name="sdt"

type="System.Diagnostics.XmlWriterTraceListener"

initializeData= "c:\wutemp\Host1.svclog" />

</listeners>

</source>

</sources>

</system.diagnostics>




Note c:\wutemp\ has to exist before the app runs.

THEN use
SvcTraceViewer.exe
and view the c:\wutemp\Host1.svclog file:

My directory to SvcTraceViewer.exe is:
"C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin\SvcTraceViewer.exe"
 
M

Mr. Arnold

Andrew Falanga said:
Hi,

I'm writing a windows service, that uses a WCF service (the server
side). The WCF service simply opens a TCP port to wait for clients to
connect, etc. I put the Open( ) call in the OnStart( ) function and
likewise the Close( ) call in the OnStop( ) function. This seemed the
proper course when making the windows service (I used the C# template
for this). I used the example on MSDN for making a WCF service, which
is a command line app, and then simply used those calls assuming that
they start and stop the server.

Now, I used MSDN to setup an installer deal and then used
"installutil" to install my service. Lo and behold, my service is
listed in the system services. So, I start it up. This works and I'm
able to run my test driver program against it and everything works. I
tried to stop it and I get an error. Trying to eliminate variables, I
restarted the box and restarted my service (it's manual start). Then,
I stopped it without making any connections. I still got the same
error. The error is:

Could not stop the MyService service on Local Computer. The service
did not return an error.

Now, I'm confused because the OnStop( ) function is defined as
returning type void, i.e. it has no return. So, why is this error
happening? How should I fix it?

If it didn't stop, then it blew-up.

I suggest you put a try/catch around the code on the onstop().

If this is a NT based O/S like Win XP or Win 2k3 server the service is
running on, then enable the Messenger service.

In the Catch part of the try/catch, use the NetSend command in the Catch and
send the error message to the machine the service is running on. That way, a
message pops on the monitor with an OK button so you can see the error.

You can also use the Netsend command in the Onstart or Onstop code in other
areas, like 'I am here in the code' message so you can see what areas in the
code that are executing before it dies.




__________ Information from ESET NOD32 Antivirus, version of virus signature database 4286 (20090728) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 

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