简体   繁体   中英

ServiceBase OnStart Not receiving args from serviceController.Start

As I understand it, the serviceBase.Onstart(args) should recieve arguments that are present in serviceController.start(args).

Here is my service controller implementation

    if (serviceController.Status == ServiceControllerStatus.Stopped)
            {
                try
                {
                    string[] args = {"execute-service"};
                    serviceController.Start(args);
                    ServiceManager.WaitForStatusChange(......);
                }
                catch (InvalidOperationException ex)
                {
                    EventLog.WriteEntry(.....);
                    return 1;
                }
            }

Here is my service.cs

     protected override void OnStart(string[] args)
    {
        this.OnStart(args);
        this.scheduler.StartScheduler();
    }

When I attempt to start my service, the argument "execute-service" is not passed to the main program.cs. I have a logFile that is being created and can see the args are not there.

Looking for some ideas on how to pass the args, as I read online, I am doing it correctly using the servicebase.onstart().

Thoughts on how to debug or fix?

I don't think there is enough information shown here to debug this issue. For comparison, this is what I do in a console application designed to run as a Windows service:

public sealed partial class ServiceMain : ServiceBase
{
    // Service startup modes.
    private const string DEBUG = @"/d";         
    private const string INSTALL = @"/i";       
    private const string UNINSTALL = @"/u";     

Then ServiceMain.Main() is set is set as the startup method:

// This is the entry point for this service. 
// This method runs on an SCM thread.
public static void Main(string[] args)
{
    if (Environment.UserInteractive && args.Length > 0)
    {
        switch (args[0])
        {
            // Debug the service as a normal app, presumably within Visual Studio.
            case DEBUG:
                ServiceMain DebugService = new ServiceMain();
                DebugService.OnStart(null);
                break;

Which calls the ServiceMain.OnStart() method:

// SCM requests service start using its own thread.
// This method must complete within 10 seconds of it
// starting. Otherwise the SCM diagnoses a hang.
protected override void OnStart(string[] args)
{
    this.AppLog = new Log();
    AppLog.Information("SCM requested service start.", "ServiceMain.OnStart");

You can see the whole service in context here .

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