Reputation: 15
I have created a windows service to send mail when service is started. The service works fine like it sends mail when i debug the service and run it via code. But the service is not working after i install it. It is not sending any mail after i install the service.
Can anyone please suggest me the solution?
Upvotes: 0
Views: 1364
Reputation: 1469
In your Main()
method, just add the following lines before ServiceBase.Run(ServicesToRun);
:
#if DEBUG
while(!Debugger.IsAttached)
{
Thread.Sleep(1000);
}
#endif
Then install your service and launch it. While it's launching, attach your debugger to your service's process (Debug Menu => Attach to process) and you should be able to debug it.
Don't forget to set your breakpoints before launching your service.
Upvotes: 0
Reputation: 4733
Debugging service is a bit difficult.
use try..catch
block with writing messages to file in every method; for example
try
{
..
}
catch(Exception ex)
{
SaveMessage(ex.ToString());
}
Save message method would be:
static void SaveMessage(string s)
{
StreamWriter sw = new StreamWriter(@"C:\service_exceptions_file.txt", true);
sw.WriteLine(s);
sw.Close();
}
Then you will see where is the problem.
Also you can add some messages in your code via abovementioned method to see what parts of code are working without problems
Upvotes: 0
Reputation: 150238
There is an excellent chance that the service lacks permission to perform one or more actions when run as a service account.
Check the Windows Event Log for any related error messages. As a test, you can configure your service to run as the same user you log on with (just to make sure the issue is permission based... do NOT leave that configuration active as it is a major security hole).
Upvotes: 3