简体   繁体   中英

Windows Service not running after installing

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?

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).

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM