Здравствуйте! Передо мной стоит задача запустить приложение в качестве службы Windows. В наличии только Express версия среды. Соответственно, установщик искали отдельно. Ниже приводится код всех частей приложения Основная часть | Код | using System; using System.Text; using System.IO; using System.Collections.Generic; using System.Security; using System.ServiceProcess; using System.Diagnostics;
using OPT.DriversService.Drivers;
namespace OPT.DriversService { public class DriversService : System.ServiceProcess.ServiceBase { private System.Diagnostics.EventLog eventLog;
private System.ComponentModel.Container components = null;
private SignalsCollector signalsCollector;
private static WindowsPC windowsPCcollector = new WindowsPC(); private bool connectionEstablished = false;
public DriversService() { this.ServiceName = "DriversService"; this.CanStop = true; this.CanPauseAndContinue = false; this.AutoLog = true;
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("DriversServiceSource")) { System.Diagnostics.EventLog.CreateEventSource("DriversServiceSource", "DriversServiceLog"); } eventLog.Source = "DriversServiceSource"; eventLog.Log ="DriversServiceLog";
do { try { signalsCollector = new SignalsCollector(); // делает по таймеру основную работу connectionEstablished = true; } catch (Exception e) { Thread.Sleep(120000); connectionEstablished = false; } } while (!connectionEstablished); } static void Main(string[] args) { Console.WriteLine("Drivers service started. Press Enter for Exit"); System.ServiceProcess.ServiceBase[] ServicesToRun; ServicesToRun = new System.ServiceProcess.ServiceBase[] { new DriversService() }; System.ServiceProcess.ServiceBase.Run(ServicesToRun); Console.ReadLine(); }
private void InitializeComponent() { this.eventLog = new System.Diagnostics.EventLog(); ((System.ComponentModel.ISupportInitialize)(this.eventLog)).BeginInit(); this.eventLog.Log = "DriversServiceLog"; this.eventLog.Source = "DriversServiceSource"; this.ServiceName = "DriversService"; ((System.ComponentModel.ISupportInitialize)(this.eventLog)).EndInit(); }
protected override void Dispose(bool disposing) { if (disposing && components != null) { components.Dispose(); } base.Dispose(disposing); }
protected override void OnStart(string[] args) { eventLog.WriteEntry("my service started");
signalsCollector.Start(); }
protected override void OnStop() { eventLog.WriteEntry("my service stoped");
signalsCollector.Stop(); }
protected override void OnContinue() { eventLog.WriteEntry("my service is continuing working"); } } }
|
Установщик | Код | [RunInstaller(true)] class DriversServiceInstaller : Installer { private ServiceProcessInstaller processInstaller; private ServiceInstaller serviceInstaller;
public DriversServiceInstaller() { processInstaller = new ServiceProcessInstaller(); serviceInstaller = new ServiceInstaller();
processInstaller.Account = ServiceAccount.LocalSystem; serviceInstaller.StartType = ServiceStartMode.Manual; serviceInstaller.ServiceName = "DriversService";
Installers.Add(serviceInstaller); Installers.Add(processInstaller); } }
|
Перед запуском делаю в консоли Visual Studio installutil D:\...\bin\Debug\DriversService.exe Пишет, что установщика найти не удалось (нет атрибута RunInstall). Пожалуйста, помогите разобраться, почему установщик невиден? Спасибо Вся беда была в отсутствии public перед class DriversServiceInstaller Это сообщение отредактировал(а) Zerofill - 26.10.2013, 11:10
|