문제

Visual Studio 설정 프로젝트에서 설치를 실행 한 후 자동으로 서비스를 시작하는 방법은 무엇입니까?

나는 방금 이것을 알아 내고 일반적인 선의 답을 나누겠다고 생각했다. 팔로우 할 답변. 나는 이것을하는 다른 방법으로 개방적입니다.

도움이 되었습니까?

해결책

프로젝트에 다음 수업을 추가하십시오.

using System.ServiceProcess;  

class ServInstaller : ServiceInstaller
{
    protected override void OnCommitted(System.Collections.IDictionary savedState)
    {
        ServiceController sc = new ServiceController("YourServiceNameGoesHere");
        sc.Start();
    }
}

설정 프로젝트는 설치자가 완료되면 클래스를 선택하고 서비스를 실행합니다.

다른 팁

허용 된 답변에 작은 추가 :

서비스 이름을 피할 수도 있습니다. 향후 서비스 이름이 변경된 경우 문제를 피하십시오.

protected override void OnCommitted(System.Collections.IDictionary savedState)
{
    new ServiceController(serviceInstaller1.ServiceName).Start();
}

(모든 설치 프로그램에는 ServiceProcessInstaller와 ServiceInstaller가 있습니다. 여기서 ServiceInstaller를 ServiceInstaller1이라고합니다.)

이 방법은 설치 프로그램 클래스와 최소량의 코드를 사용합니다.

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

namespace MyProject
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
            serviceInstaller1.AfterInstall += (sender, args) => new ServiceController(serviceInstaller1.ServiceName).Start();
        }
    }
}

정의하다 serviceInstaller1 (ServiceInstaller 타입) 설치 프로그램 클래스 디자이너의 ServiceName 디자이너의 속성.

고마워요 괜찮아요 ...

private System.ServiceProcess.ServiceInstaller serviceInstaller1;

private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
    ServiceController sc = new ServiceController("YourServiceName");
    sc.Start();
}

자신의 클래스를 만드는 대신 프로젝트 설치 프로그램에서 서비스 설치 프로그램을 선택하고 이벤트 핸들러를 Comitted 이벤트에 추가하십시오.

private void serviceInstallerService1_Committed(object sender, InstallEventArgs e)
{
    var serviceInstaller = sender as ServiceInstaller;
    // Start the service after it is installed.
    if (serviceInstaller != null && serviceInstaller.StartType == ServiceStartMode.Automatic)
    {
        var serviceController = new ServiceController(serviceInstaller.ServiceName);
        serviceController.Start();
    }
}

시작 유형이 자동으로 설정된 경우에만 서비스를 시작합니다.

위의 스 니펫을 기반으로, 내 ProjectInstaller.cs 파일은 FSWSERVICEMGR.EXE라는 서비스에 대해 이와 같이 상처를 입었습니다. 서비스는 설치 후 시작되었습니다. 참고로 솔루션 탐색기에서 설정 프로젝트를 선택할 때 회사 등을 설정할 때 속성 탭 (오른쪽 버튼을 클릭하지 않음)을 클릭하십시오.


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

namespace FSWManager {
    [RunInstaller(true)]
    public partial class ProjectInstaller : Installer {
        public ProjectInstaller() {
            InitializeComponent();
            this.FSWServiceMgr.AfterInstall += FSWServiceMgr_AfterInstall;
        }

        static void FSWServiceMgr_AfterInstall(object sender, InstallEventArgs e) {
            new ServiceController("FSWServiceMgr").Start();
        }
    }
}

코드가 포함되지 않는 다른 방법도 있습니다. 서비스 제어 테이블을 사용할 수 있습니다. 생성 된 MSI 파일을 orca.exe로 편집하고 항목을 추가합니다. ServiceControl 테이블.

ServiceControl, 이름, 이벤트 및 구성 요소 _ 열만 필수입니다. Component_ 열에는 파일 테이블의 구성 요소가 포함되어 있습니다. (파일 테이블에서 파일을 선택하고 component_value를 ServiceControl 테이블에 복사하십시오.)

마지막 단계는 TAST InstalLexecutesequence에서 STARTERVICES의 값을 6575로 업데이트하는 것입니다. 서비스를 시작하기에 충분합니다.

그건 그렇고, 서비스 설치 테이블을 사용하면 Windows 서비스를 설치하도록 설치 프로그램을 구성 할 수 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top