The Service on local computer started and then stopped.

Recently i have created a Windows service, then i have installed on my win7 machine.

Whenever i tried to start the service I got the error:

The Service on local computer started and then stopped ,Some services stop automatically if there are not in use by other services or programs.”



This is generally the result of one of three things - either 
(a) your OnStart() method is throwing an exception 
(b) the OnStart() method is not kicking off a thread to do work.
(c) assign a  Local System account instead of Local Service account to run this service.
If the problem is (a), then the obvious solution is to debug the service to identify what is going wrong. At a minimum, put a try-catch block around the contents of the OnStart() method and log an error to the system event log when an exception occurs. Then you can see the details in the Windows Event Viewer.

protected override void OnStart(string[] args)
{
        try
        {
              // Your Code Here
        }

        catch (Exception ex)
        {

            EventLog.WriteEntry(ex.Message, EventLogEntryType.Error);

        }
}

I have received the issue due to problem (a) and rectified the issue after catching the cause in event log.


If the problem is (b), then you need to create a thread that actually does something. The thread needs to be a foreground thread (as opposed to a background thread) to prevent the service from shutting down. 
If the problem is (c), then you need to 
1)      Click Run Command from start buttin
2)      Enter Services.msc then click OK,you will get all the services in your computer.
3)      Select your service and right click on the service and select Properties
4)      Goto Logon Properties à select  Local System Account then click OK.



Comments

Post a Comment

Popular posts from this blog

Data Bound Controls in ASP.Net - Part 4 (FormView and DetailsView controls)

ASP.net: HttpHandlers

The Clickjacking attack and X-Frame-Options