[C#] 설치시 자동으로 Windows 서비스 시작

InstallUtil.exe를 사용하여 설치 한 Windows 서비스가 있습니다. 시작 방법을 자동으로 설정했지만 설치시 서비스가 시작되지 않습니다. 수동으로 서비스를 열고 시작을 클릭해야합니다. 명령 줄이나 서비스 코드를 통해 시작할 수있는 방법이 있습니까?



답변

Installer 클래스에서 AfterInstall 이벤트에 대한 처리기를 추가합니다. 그런 다음 이벤트 처리기에서 ServiceController를 호출하여 서비스를 시작할 수 있습니다.

using System.ServiceProcess;
public ServiceInstaller()
{
    //... Installer code here
    this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);
}

void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
{
    ServiceInstaller serviceInstaller = (ServiceInstaller)sender;

    using (ServiceController sc = new ServiceController(serviceInstaller.ServiceName))
    {
             sc.Start();
    }
}

이제 설치 프로그램에서 InstallUtil을 실행하면 서비스가 자동으로 설치되고 시작됩니다.


답변

약간 리팩토링 한 후 자동 시작 기능이있는 완전한 Windows 서비스 설치 프로그램의 예입니다.

using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;

namespace Example.of.name.space
{
[RunInstaller(true)]
public partial class ServiceInstaller : Installer
{
    private readonly ServiceProcessInstaller processInstaller;
    private readonly System.ServiceProcess.ServiceInstaller serviceInstaller;

    public ServiceInstaller()
    {
        InitializeComponent();
        processInstaller = new ServiceProcessInstaller();
        serviceInstaller = new System.ServiceProcess.ServiceInstaller();

        // Service will run under system account
        processInstaller.Account = ServiceAccount.LocalSystem;

        // Service will have Start Type of Manual
        serviceInstaller.StartType = ServiceStartMode.Automatic;

        serviceInstaller.ServiceName = "Windows Automatic Start Service";

        Installers.Add(serviceInstaller);
        Installers.Add(processInstaller);
        serviceInstaller.AfterInstall += ServiceInstaller_AfterInstall;
    }
    private void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
    {
        ServiceController sc = new ServiceController("Windows Automatic Start Service");
        sc.Start();
    }
}
}


답변

다음 명령은 어떻습니까?

net start "<service name>"
net stop "<service name>"


답변

서비스 제어를위한 프로그래밍 옵션 :

  • 네이티브 코드는 “서비스 시작”을 사용할 수 있습니다 . 최소한의 종속성으로 최대한의 제어가 가능하지만 대부분의 작업이 수행됩니다.
  • WMI : Win32_Service 에는 StartService메서드가 있습니다. 이것은 다른 처리를 수행 할 수 있어야하는 경우에 유용합니다 (예 : 서비스 선택).
  • PowerShell은 : 실행 Start-Service을 통해 RunspaceInvoke나 자신을 만들어 Runspace그 사용하여 CreatePipeline실행하는 방법을. 이는 WMI보다 훨씬 더 쉬운 코딩 모델로 다른 처리 (예 : 서비스 선택)를 수행 할 수 있어야하지만 설치되는 PSH에 따라 달라지는 경우에 유용합니다.
  • .NET 응용 프로그램은 다음을 사용할 수 있습니다. ServiceController

답변

다음 명령 줄을 사용하여 서비스를 시작할 수 있습니다.

net start *servicename*


답변

ServiceController 를 사용 하여 코드에서 서비스를 시작하십시오.

업데이트 : 명령 줄에서 서비스를 시작하는보다 정확한 방법 은 “net”대신 “sc”( 서비스 컨트롤러 ) 명령을 사용하는 것입니다.


답변

정확히 허용 대답을 다음에도 불구하고, 나는 아직도 대신이 존재하지 않는 것처럼 방금 설치 한 서비스 사용에도 불구하고, 시작할 수 없다는 설치하는 동안 오류 메시지가 주어진 I를 start– 할 수있는 서비스를받을 수 없습니다 this.serviceInstaller.ServiceName오히려를 리터럴보다 …

결국 명령 줄을 사용하는 대체 솔루션을 찾았습니다.

private void serviceInstaller_AfterInstall(object sender, InstallEventArgs e) {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.FileName = "cmd.exe";
        startInfo.Arguments = "/C sc start " + this.serviceInstaller.ServiceName;

        Process process = new Process();
        process.StartInfo = startInfo;
        process.Start();
    }