(1) 在建立windowsservice程式時,Debug/Release的類型選擇為AnyCPU(特別是64位的機器)
(2) 添加安裝程式時,會產生兩個組件,比如ServiceProcessInstaller1,ServiceInstaller1
(3) 在為service添加安裝程式時,安裝程式(比如ServiceInstaller1)的Name屬性和Service的Name屬性要一致
(4) ServiceProcessInstaller1的Account屬性必須設定為LocalSystem
(5) 安裝服務時,以管理員身份運行Viusial Studio命令列工具進行安裝。
安裝服務的命令:
Installutil “程式地址”
(比如:Installutil "D:/Users/SPang/Documents/VisualStudio2010/Projects/WindowsService1/WindowsService1/bin/Debug/WindowsService1.exe")
卸載服務的命令:
Installutil “程式地址” /u
(比如:Installutil "D:/Users/SPang/Documents/VisualStudio2010/Projects/WindowsService1/WindowsService1/bin/Debug/WindowsService1.exe"/u )
(6) 安裝完成後啟動服務,可以通過我的MyComputer -> Manage -> System Tools -> EventViewer -> Windows Logs-> Application(我的電腦->管理->事件檢視器->windows日誌->應用程式)查看服務的日誌。
(7) 在使用命令時可能出現的錯誤:
i. an attempt was made to load a program withan incorrect format。解決方案:步驟1
ii. the install failed, and the roll back hasbeen performed。解決方案:步驟4
最簡單的一個Windows Service程式碼範例(VB.NET):
C#代碼和這個差不多
Public Class Service1
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
EventLog.WriteEntry("Start Service")
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
End Sub
End Class