3 回答

TA貢獻1842條經(jīng)驗 獲得超13個贊
打開VisualStudio 2008,并選擇FilesNewNewProject菜單選項。 在“新項目”對話框中.。 在項目類型中選擇VisualC#000-Windows節(jié)點 選擇WindowsService模板 輸入項目的名稱和位置 按OK 此時,您已經(jīng)掌握了Windows服務(wù)的所有基礎(chǔ)知識。cs文件包含服務(wù)的main()方法,Service1.cs定義System.ServiceProcess.ServiceBase組件,這是新的Windows服務(wù)。 在Service1組件的屬性網(wǎng)格中,考慮至少設(shè)置以下屬性: (名稱)-給你的對象一個直觀的名稱,例如,Service示例 Autolog-設(shè)置為 false
為了防止事件默認地寫入應(yīng)用程序事件日志(注意:我不是說您不應(yīng)該記錄服務(wù)事件;我只是更喜歡編寫自己的事件日志而不是應(yīng)用程序日志-請參見下面) 倒計時-設(shè)置為 true
如果您想處理系統(tǒng)關(guān)閉 ServiceName-定義服務(wù)控制管理器(SCM)將知道您的服務(wù)的名稱 在ServiceExsquare的代碼中,將刪除OnStart()和OnStop()虛擬函數(shù)。顯然,您需要用任何您的服務(wù)需要做的事情來填充這些內(nèi)容。如果將“倒計時”屬性更改為 true
,您也需要重寫OnShu倒計時方法。我在下面創(chuàng)建了一個示例,說明這些函數(shù)的使用情況。 此時,ServiceExmen服務(wù)基本上已經(jīng)完成,但是您仍然需要一種方法來安裝它。為此,請在設(shè)計器中打開ServiceExmen組件。右鍵單擊“設(shè)計器”面板中的任何位置,并選擇“添加安裝程序”菜單選項。這將向項目添加一個ProjectInstaller組件,該組件包含兩個附加組件-serviceProcessInstaller1和serviceInstaller1。 在設(shè)計器中選擇serviceProcessInstaller1組件。在屬性網(wǎng)格中,考慮設(shè)置以下屬性: (Name)-給對象一個直觀的名稱,例如serviceProcessInstaller 帳戶-至少選擇LocalService帳戶,但如果服務(wù)需要更多特權(quán),則可能必須使用NetworkService或LocalSystem帳戶 在設(shè)計器中選擇serviceInstaller1組件。在屬性網(wǎng)格中,考慮設(shè)置以下屬性: (名稱)-給對象一個直觀的名稱,例如serviceInstaller 描述-該服務(wù)的描述將顯示在您的服務(wù)的SCM中 DisplayName-您的服務(wù)的友好名稱,它將顯示在您的服務(wù)的SCM中 ServiceName-確保這是您為ServiceExfacum組件的ServiceName屬性選擇的相同名稱(請參見步驟4) StartType-指示您希望服務(wù)是自動啟動還是手動啟動 請記住,我說過我更喜歡編寫事件而不是自己的事件日志,而不是應(yīng)用程序事件日志。為此,需要將ProjectInstaller中的默認EventLogInstaller替換為自定義的EventLogInstaller。使ProjectInstaller的代碼如下所示:
using System.Diagnostics; [RunInstaller(true)] public partial class ProjectInstaller : Installer { public ProjectInstaller() { InitializeComponent(); EventLogInstaller installer = FindInstaller(this.Installers); if (installer != null) { installer.Log = "ServiceExample"; // enter your event log name here } } 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; } }
using System.Diagnostics; public partial class ServiceExample : ServiceBase { public ServiceExample() { // Uncomment this line to debug the service. //Debugger.Break(); InitializeComponent(); // Ties the EventLog member of the ServiceBase base class to the // ServiceExample event log created when the service was installed. EventLog.Log = "ServiceExample"; } protected override void OnStart(string[] args) { EventLog.WriteEntry("The service was started successfully.", EventLogEntryType.Information); } protected override void OnStop() { EventLog.WriteEntry("The service was stopped successfully.", EventLogEntryType.Information); } protected override void OnShutdown() { EventLog.WriteEntry("The service was shutdown successfully", EventLogEntryType.Information); } }

TA貢獻2065條經(jīng)驗 獲得超14個贊

TA貢獻1906條經(jīng)驗 獲得超3個贊
- 3 回答
- 0 關(guān)注
- 648 瀏覽
添加回答
舉報