Tobia
Tobia

Reputation: 9524

Why Main() method is called on service start?

I have an application with one main class It overrides ServiceBase methods and has the Main() static method. I want to use Main() method when it is called by command line, and OnStart()/OnStop() when it is called from windows service management.

I successfully installed this app as service it with installutils but when I start it the Main() method is called instead of OnStart() as expected.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Xml.Serialization;
using System.Runtime.Serialization.Json;
using System.Threading;
using System.ServiceProcess;
using System.Configuration.Install;
using System.Reflection;

namespace Test
{

    class Program : ServiceBase
    {

        static void Main(string[] args)
        {  
            log.Error("Run as App");
        }


        protected override void OnStart(string[] args)
        {
            log.Info("Starting service");
        }

        protected override void OnStop()
        {
            log.Info("Stopping service");
        }
    }


}

Upvotes: 3

Views: 5143

Answers (3)

Tobia
Tobia

Reputation: 9524

This is the answer:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Xml.Serialization;
using System.Runtime.Serialization.Json;
using System.Threading;
using System.ServiceProcess;
using System.Configuration.Install;
using System.Reflection;

namespace Test
{

    class Program : ServiceBase
    {

        static void Main(string[] args)
        {  
                if (System.Environment.UserInteractive)
                {
                    log.Debug("App");
                }
                else
                {
                    ServiceBase.Run(new ServiceBase[] { new Program() });
                }
        }


        protected override void OnStart(string[] args)
        {
            log.Info("Starting service");
        }

        protected override void OnStop()
        {
            log.Info("Stopping service");
        }
    }


}

Upvotes: 2

flobjective
flobjective

Reputation: 66

The Main method also gets called within a service like in any other programm. (see https://msdn.microsoft.com/en-us/library/windows/desktop/ms685477(v=vs.85).aspx).

You can use the Property Environment.UserInteractive (https://msdn.microsoft.com/en-us/library/system.environment.userinteractive.aspx) to determine if the app was called by the user or as a service.

Upvotes: 1

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239754

Windows services have a guilty secret. They start their lives as plain old console applications. It's only after they've started running and register with the service control manager that they're transformed into services.

As such, yes, it's correct that Main is the first entrypoint that is called because at that point, it's just a console app.

The way that the service registers with the Service Control Manager and becomes a service is handled by calling ServiceBase.Run() from your Main method (or a method that it invokes)

Upvotes: 11

Related Questions