Служба на локальном компьютере запущена, затем остановлена ошибка
У меня есть служба Windows. Он работал нормально, пока я не добавил код для начала регистрации. Теперь, когда я пытаюсь запустить службу, я получаю следующую ошибку:
Служба GBBService на локальном компьютере была запущена, а затем остановлена. Некоторые службы автоматически останавливались, если у них не было работы, например журналы производительности и служба оповещений
Вот код для моего сервиса: - Изнутри установщик проекта
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
string eventSource = "GBBServiceLog";
public ProjectInstaller()
{
InitializeComponent();
EventLogInstaller installer = FindInstaller(this.Installers);
if (installer != null)
{
installer.Source = eventSource;
installer.Log = "My GBBServiceLog";
}
}
private EventLogInstaller FindInstaller(InstallerCollection installers)
{
foreach (Installer installer in installers)
{
if (installer is EventLogInstaller)
{
return (EventLogInstaller)installer;
}
EventLogInstaller eventLogInstaller = FindInstaller(installer.Installers);
if (eventLogInstaller != null)
return eventLogInstaller;
}
return null;
}
protected override void OnCommitted(IDictionary savedState)
{
base.OnCommitted(savedState);
// Start the service after installation
using (ServiceController sc = new ServiceController(this.serviceInstaller1.ServiceName))
{
sc.Start();
}
}
}
Из моего сервиса:
public GBBService()
{
InitializeComponent();
EventLog.Source = eventSource;
EventLog.Log = "My GBB Service Log";
}
protected override void OnStart(string[] args)
{
EventLog.WriteEntry("GBBService Service Started");
}
Я сделал что-то не так в своем коде?
1 ответ
Решение
Я думаю, что проблема в том, что вы пытаетесь написать EventLog
этого не существует.
в ServiceInstaller
Вы создаете EventLog
My GBBServiceLog
if (installer != null)
{
installer.Source = eventSource;
installer.Log = "My GBBServiceLog";
}
Но в Service
ты пытаешься написать My GBB Service Log
public GBBService()
{
InitializeComponent();
EventLog.Source = eventSource;
EventLog.Log = "My GBB Service Log"; <--------------
}
Я думаю, что это должно быть:
public GBBService()
{
InitializeComponent();
EventLog.Source = eventSource;
EventLog.Log = "My GBBServiceLog";
}