Criei um Windows Service
de teste, andei pesquisando e vi que para iniciá-lo automaticamente é preciso mudar a propriedade StartType
do objeto serviceInstaller
para Automatic, que o serviço, depois de instalado, já inicia automaticamente. Entretanto, olhei no Gerenciamento do Computador na parte de Serviços e o Tipo de Inicialização do meu serviço está como Manual. Segue prints abaixo:
Serviço no gerenciamento do Computador
Configurações do serviceInstaller
Código do ProjectInstaller.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;
namespace servico_teste
{
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent();
}
private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
using (var sc = new ServiceController(serviceInstaller1.ServiceName))
{
sc.Start();
}
}
public void ServiceInstaller()
{
//... Installer code here
this.AfterInstall += new InstallEventHandler(serviceInstaller1_AfterInstall);
}
}
}
Código do Service1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.IO;
namespace servico_teste
{
public partial class Service1 : ServiceBase
{
Timer timer1;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer1 = new Timer(new TimerCallback(timer1_Tick), null, 15000, 60000);
}
protected override void OnStop()
{
StreamWriter vWriter = new StreamWriter(@"c:\testeServico.txt", true);
vWriter.WriteLine("Servico Parado: " + DateTime.Now.ToString());
vWriter.Flush();
vWriter.Close();
}
private void timer1_Tick(object sender)
{
StreamWriter vWriter = new StreamWriter(@"c:\testeServico.txt", true);
vWriter.WriteLine("Servico Rodando: " + DateTime.Now.ToString());
vWriter.Flush();
vWriter.Close();
}
}
}
A pergunta é: como faço para que depois de instalado o serviço inicie automaticamente?