C# windows服務的建立與調試

Windows Service這一塊並不複雜,可是注意事項太多了,網上資料也很凌亂,偶爾本身寫也會丟三落四的。因此本文也就產生了,本文不會寫複雜的東西,徹底以基礎應用的需求來寫,因此不會對Windows Service寫很深刻。html

本文介紹瞭如何用C#建立、安裝、啓動、監控、卸載簡單的Windows Service 的內容步驟和注意事項。windows

1、建立一個Windows Serviceide

1)建立Windows Service項目url

C# <wbr>windows服務的建立與調試

2)對Service重命名spa

將Service1重命名爲你服務名稱,這裏咱們命名爲ServiceTest。3d

2、建立服務安裝程序調試

1)添加安裝程序日誌

C# <wbr>windows服務的建立與調試

C# <wbr>windows服務的建立與調試

 

以後咱們能夠看到上圖,自動爲咱們建立了ProjectInstaller.cs以及2個安裝的組件。htm

2)修改安裝服務名blog

右鍵serviceInsraller1,選擇屬性,將ServiceName的值改成ServiceTest。

 C# <wbr>windows服務的建立與調試

3)修改安裝權限

右鍵serviceProcessInsraller1,選擇屬性,將Account的值改成LocalSystem。

 C# <wbr>windows服務的建立與調試

3、寫入服務代碼

1)打開ServiceTest代碼

右鍵ServiceTest,選擇查看代碼。

2)寫入Service邏輯

添加以下代碼: 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Diagnostics;

using System.Linq;

using System.ServiceProcess;

using System.Text;

namespace WindowsServiceTest

{

public partial class ServiceTest : ServiceBase

{

public ServiceTest()

{

InitializeComponent();

}

protected override void OnStart(string[] args)

{

using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true))

{

sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Start.");

}

}

protected override void OnStop()

{

using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true))

{

sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Stop.");

}

}

}

}

這裏咱們的邏輯很簡單,啓動服務的時候寫個日誌,關閉的時候再寫個日誌。

4、建立安裝腳本

在項目中添加2個文件以下(必須是ANSI或者UTF-8無BOM格式):

1)安裝腳本Install.bat

?

 

1

2

3

%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe WindowsServiceTest.exe

Net Start ServiceTest

sc config ServiceTest start= auto

2)卸載腳本Uninstall.bat 

1

%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u WindowsServiceTest.exe

3)安裝腳本說明

第二行爲啓動服務。

第三行爲設置服務爲自動運行。

這2行視服務形式自行選擇。

4)腳本調試

若是須要查看腳本運行情況,在腳本最後一行加入pause

5、在C#中對服務進行控制

0)配置目錄結構

簡歷一個新WPF項目,叫WindowsServiceTestUI,添加對System.ServiceProcess的引用。

在WindowsServiceTestUI的bin\Debug目錄下創建Service目錄。

將WindowsServiceTest的生成目錄設置爲上面建立的Service目錄。

生成後目錄結構以下圖

 C# <wbr>windows服務的建立與調試

1)安裝

安裝時會產生目錄問題,因此安裝代碼以下: 

1

2

3

4

5

6

7

8

string CurrentDirectory = System.Environment.CurrentDirectory;

System.Environment.CurrentDirectory = CurrentDirectory + "\\Service";

Process process = new Process();

process.StartInfo.UseShellExecute = false;

process.StartInfo.FileName = "Install.bat";

process.StartInfo.CreateNoWindow = true;

process.Start();

System.Environment.CurrentDirectory = CurrentDirectory;

2)卸載

卸載時也會產生目錄問題,因此卸載代碼以下:

1

2

3

4

5

6

7

8

string CurrentDirectory = System.Environment.CurrentDirectory;

System.Environment.CurrentDirectory = CurrentDirectory + "\\Service";

Process process = new Process();

process.StartInfo.UseShellExecute = false;

process.StartInfo.FileName = "Uninstall.bat";

process.StartInfo.CreateNoWindow = true;

process.Start();

System.Environment.CurrentDirectory = CurrentDirectory;

3)啓動

代碼以下:

1

2

3

4

5

using System.ServiceProcess;

ServiceController serviceController = new ServiceController("ServiceTest");

serviceController.Start();

4)中止

1

2

3

ServiceController serviceController = new ServiceController("ServiceTest");

if (serviceController.CanStop)

serviceController.Stop();

5)暫停/繼續

1

2

3

4

5

6

7

8

ServiceController serviceController = new ServiceController("ServiceTest");

if (serviceController.CanPauseAndContinue)

{

if (serviceController.Status == ServiceControllerStatus.Running)

serviceController.Pause();

else if (serviceController.Status == ServiceControllerStatus.Paused)

serviceController.Continue();

}

6)檢查狀態

1

2

ServiceController serviceController = new ServiceController("ServiceTest");

string Status = serviceController.Status.ToString();

6、調試Windows Service

1)安裝並運行服務

2)附加進程

C# <wbr>windows服務的建立與調試

C# <wbr>windows服務的建立與調試

 

3)在代碼中加入斷點進行調試

C# <wbr>windows服務的建立與調試

相關文章
相關標籤/搜索