Question

I created a C# windows service and installed it successfully on my local developer machine (it works well).

Now I'm trying to install the service on a different machine. I copied the "Release" folder to the new machine and installed the service.

When I start the service on the new machine I get the following error: "service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other services or programs."

I don't get any message to the Application event log, I even added debug message as the first line of the program, but i see nothing in the Event Viewer (as if the code doesn't start at all). :/

What have i done wrong?

Was it helpful?

Solution 2

Well, I've found the problem:

I used password decryption with DataProctionScope.LocalMachine. So when I changed the machine- the decryption failed.

I had to re-encrypt the passwords on the local machine and then the decryption worked fine.

Thank you for your responds!

*The eventlog debugging didn't work because of my fault.

OTHER TIPS

"started and then stopped" message usually appears when your server throws an exception during start up. Which could be for many reasons, including invalid paths and inability to write to the Application Event Log due to missing source or insufficient privileges.

I usually include an option to run my service as a console app. Which allows me to display any exceptions using Console.WriteLine.

Following assumes your service extends from System.ServiceProcess.ServiceBase.

partial class MyService : ServiceBase
{
    private static void Main(string[] args)
    {
        MyService svc = new MyService();

        if (Environment.UserInteractive)
            RunConsole(args, svc);
        else
            Run(svc);
    }

    public MyService()
    {
        InitializeComponent();
    }

    protected static bool KeepRunning { get; set; }

    protected override void OnStart(string[] args)
    {
        StartServiceHost();
    }

    protected override void OnStop()
    {
        StopServiceHost();
    }

    protected override void OnShutdown()
    {
        StopServiceHost();
        base.OnShutdown();
    }

    private static void RunConsole(string[] args, ConverterService svc)
    {
        // need to hold on to Ctrl+C, otherwise StopServiceHost() never gets called
        Console.CancelKeyPress += (sender, e) => ShutDown(svc);

        KeepRunning = true;

        svc.OnStart(args);
        Console.WriteLine("Press <Ctrl+C> to exit.");
        while (KeepRunning)
        {
            Console.ReadLine();
        }
    }

    private void StartServiceHost()
    {
        // start your service
    }

    private void StopServiceHost()
    {
        // stop your service
    }

    private static void ShutDown(MyService svc)
    {
        Console.WriteLine("exiting...");
        svc.OnStop();
        KeepRunning = false;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top