3

I am using the service controller executecommand function, like so:

            ServiceController serviceController = new ServiceController("a Service",
                Environment.MachineName);

            serviceController.ExecuteCommand(129);

And in the service controller:

    protected override void OnCustomCommand(int command)
    {
        base.OnCustomCommand(command);

        // Depending on the integer passed in, the appropriate method is called.
        switch (command)
        {
            case 129:
                RestartSpooler();
                break;
            case 131:
                InstallPrinter();
                break;
            case 132:
                DeletePrinter();
                break;
        }
    }

However, despite calling any of the commands from the calling code (the code hits the line, then steps over, no exceptions), nothing happens. Why? This is all on the local machine and I have full admin rights.

Thanks

blade33
  • 131
  • 2
  • 7
  • It should work the way you describe it. Check that you installed service correctly and your customCommand actions do what you want – username Apr 27 '11 at 14:16
  • I know this is very late but it could be a permission issue. Did you try putting a try/catch around the execute command call and see what the exact error shows? – John Odom Apr 15 '15 at 20:43

2 Answers2

1

You must be trying to execute command against a stopped service. Add something like the following:

    if (serviceController1.Status == ServiceControllerStatus.Stopped)
    {
        serviceController1.Start();
    }
    serviceController1.ExecuteCommand(192);
r.sumesh
  • 313
  • 1
  • 3
  • 13
0

I haven't found any reason why it shouldn't work. Here is the working example of windows service with custom command

public partial class TestService : ServiceBase
{
    public TestService()
    {
        InitializeComponent();
    }

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

    protected override void OnStop() { }

    protected override void OnCustomCommand(int command)
    {
        base.OnCustomCommand(command);

        switch (command)
        {
            case 129:
                //
                break;
            case 131:
                //
                break;
            case 132:
                //
                break;
        }
    }
}

Service installer

[RunInstaller(true)]
public partial class Installer : System.Configuration.Install.Installer
{
    public Installer()
    {
        InitializeComponent();

        _processInstaller = new ServiceProcessInstaller();
        _processInstaller.Account = ServiceAccount.LocalSystem;

        _serviceInstaller = new ServiceInstaller();
        _serviceInstaller.StartType = ServiceStartMode.Manual;
        _serviceInstaller.ServiceName = "TestService";

        Installers.Add(_serviceInstaller);
        Installers.Add(_processInstaller);
    }

    private readonly ServiceInstaller _serviceInstaller;
    private readonly ServiceProcessInstaller _processInstaller;
}

Service usage

var serviceController = new ServiceController("TestService", Environment.MachineName);
serviceController.ExecuteCommand(129);
username
  • 3,378
  • 5
  • 44
  • 75