서비스 프로젝트를 작성 후 배포하기 위해 서비스를 설치하고 설치 후 바로 서비스가 구동되도록 하는 방법에 대해 설명한다.
서비스프로젝트에 설치관련 클래스 추가
- 작성한 서비스.cs파일 더블클릭
- 도구상자에 ProjectInstaller를 선택
- ServiceProcessInstaller, ServiceInstaller가 생성됨
설치 프로젝트 추가
- 파일 -> 프로젝트 추가 > Setup Project
- 프로젝트 아이콘에 마우스우측 버튼 클릭
- Add -> 프로젝트 출력
- 추가할 프로젝트를 콤보박스에서 선택하고 기본출력을 선택
- View -> 사용자지정작업에서 Install, Commit, Rollback, Uninstall에 각각 마우스우측버튼 클릭 후 '사용자지정작업'으로 작성한 서비스프로젝트 추가
- 빌드 수행하면 msi와 setup.exe가 생성됨
설치 시 서비스 실행되도록
- ProjectInstaller.Designer.cs코드보기
- initializeComponent()에서 서비스설치 관련 파라메터 수정
- ServiceName, DisplayName, Description
- StartType : Automatic으로 설정
- Committed 핸들러 오버라이드 등록
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
this.serviceProcessInstaller1.Account = ServiceAccount.LocalSystem;
this.serviceInstaller1.ServiceName = "My Service";
this.serviceInstaller1.DisplayName = "My service";
this.serviceInstaller1.Description = "This is My service";
this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
this.Committed += new System.Configuration.Install.InstallEventHandler(MyServiceWindowsServiceInstaller_Committed);
3. ProjectInstaller.cs 코드보기
4. Committed 핸들러 오버라이드 수행
- 서비스 컨트롤러 생성하여 서비스를 시작함.
void MyServiceWindowsServiceInstaller_Committed(object sender, InstallEventArgs e)
{
// Auto Start the Service Once Installation is Finished.
var controller = new ServiceController("MyService");
controller.Start();
}