설치시 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) 송신자;
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>"
You can use the following command line to start the service:
net start *servicename*
Programmatic options for controlling services:
- Native code can used, "Starting a Service". Maximum control with minimum dependencies but the most work.
- WMI: Win32_Service has a
StartService
method. This is good for cases where you need to be able to perform other processing (e.g. to select which service). - PowerShell: execute
Start-Service
viaRunspaceInvoke
or by creating your ownRunspace
and using itsCreatePipeline
method to execute. This is good for cases where you need to be able to perform other processing (e.g. to select which service) with a much easier coding model than WMI, but depends on PSH being installed. - A .NET application can use
ServiceController
Use ServiceController to start your service from code.
Update: And more correct way to start service from the command line is to use "sc" (Service Controller) command instead of "net".
Despite following the accepted answer exactly, I was still unable to get the service to start-- I was instead given a failure message during installation stating that the service that was just installed could not be started, as it did not exist, despite using this.serviceInstaller.ServiceName
rather than a literal...
I eventually found an alternative solution that makes use of the command line:
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();
}
Automatic startup means that the service is automatically started when Windows starts. As others have mentioned, to start it from the console you should use the ServiceController.
You can use the GetServices
method of ServiceController class to get an array of all the services. Then, find your service by checking the ServiceName
property of each service. When you've found your service, call the Start
method to start it.
You should also check the Status
property to see what state it is already in before calling start (it may be running, paused, stopped, etc..).
You corrupted your designer. ReAdd your Installer Component. It should have a serviceInstaller and a serviceProcessInstaller. The serviceInstaller with property Startup Method set to Automatic will startup when installed and after each reboot.
Just a note: You might have set up your service differently using the forms interface to add a service installer and project installer. In that case replace where it says serviceInstaller.ServiceName with "name from designer".ServiceName.
You also don't need the private members in this case.
Thanks for the help.
Kind of late but you could make use of the sc tool in cmd.
sc start ServiceName
Note you must be in administrator mode.
참고URL : https://stackoverflow.com/questions/1036713/automatically-start-a-windows-service-on-install
'Programming' 카테고리의 다른 글
WPF ListBox에서 가로 스크롤을 비활성화하려면 어떻게해야합니까? (0) | 2020.07.19 |
---|---|
getMinimum ()이 O (1)이되도록 스택을 설계하십시오. (0) | 2020.07.19 |
Vuex 액션 vs 돌연변이 (0) | 2020.07.19 |
페이지에 세로 스크롤 막대가 있는지 감지 하시겠습니까?구조체에서 여러 이름 태그를 정의하는 방법 (0) | 2020.07.19 |
왜 C ++에서 부호없는 int 대신 DWORD를 사용합니까? (0) | 2020.07.19 |