最近由於工作需要,寫了一個windows服務程式,有許多經驗,我會陸續寫出來。
請原諒我從安裝談起,因為我一直有一個誤區:只要從System.ServiceProcess.ServiceBase繼承一個類並編譯好就可以用.net提供的命令列程式InstallUtil.exe安裝了。經過嘗試,發現如果僅僅做了這樣的一個類是無法用InstallUtil.exe安裝的,在安裝時會輸出下面的資訊:
D:\>installutil windowsservice2.exe<br />Microsoft (R) .NET Framework Installation utility Version 1.1.4322.573<br />Copyright (C) Microsoft Corporation 1998-2002. All rights reserved.<br />Running a transacted installation.</p><p>Beginning the Install phase of the installation.<br />See the contents of the log file for the d:\windowsservice2.exe assembly's progr<br />ess.<br />The file is located at d:\windowsservice2.InstallLog.<br />Installing assembly 'd:\windowsservice2.exe'.<br />Affected parameters are:<br /> assemblypath = d:\windowsservice2.exe<br /> logfile = d:\windowsservice2.InstallLog<br />No public installers with the RunInstallerAttribute.Yes attribute could be found<br /> in the d:\windowsservice2.exe assembly.</p><p>The Install phase completed successfully, and the Commit phase is beginning.<br />See the contents of the log file for the d:\windowsservice2.exe assembly's progr<br />ess.<br />The file is located at d:\windowsservice2.InstallLog.<br />Committing assembly 'd:\windowsservice2.exe'.<br />Affected parameters are:<br /> assemblypath = d:\windowsservice2.exe<br /> logfile = d:\windowsservice2.InstallLog<br />No public installers with the RunInstallerAttribute.Yes attribute could be found<br /> in the d:\windowsservice2.exe assembly.<br />Remove InstallState file because there are no installers.</p><p>The Commit phase completed successfully.</p><p>The transacted install has completed.
這是因為installUtil.exe會用反射的方法去尋找所有把RunInstallerAttribute設定為true的System.Configuration.Install.Installer類型,並執行上面的Install方法,當找不到的時候,安裝就失敗了。
所以我們必須要寫一個從Installer類繼承的類來滿足installUtil.exe的要求。
windows服務開發(二、調試)