建立,安裝,除錯 Windows Service

2021-09-08 23:43:00 字數 3658 閱讀 6594

把步驟寫下來備忘。

目的: 要從windows service裡定時呼叫wcf服務, 做一些事情。

1.新建立乙個windows service專案, 然後新增乙個lzdcallwcfservice.cs的windows service類.

2.新增starthour,endhour和interval三個引數,這樣可以通過onstart(string args)方法,在windows service初始啟動時傳進來間隔時間等引數。

public int starthour = 1;

public int endhour = 23;

public int interval = 1800000; //預設為半小時啟動一次。

3.新增timer,讓windows service能定時激發,在tmrtimerstimer_elapsed中執行一些事情。

using system.timers;

private void tmrtimerstimer_elapsed(object sender, system.timers.elapsedeventargs e)

5.如果wcf部署在有安全證書保護的iis站點內,還需要ignorecertificateerrorhandler來規避證書問題。

using system.net;

using system.net.security;

using system.security.cryptography.x509certificates;

private void tmrtimerstimer_elapsed(object sender, system.timers.elapsedeventargs e)}}

public static bool ignorecertificateerrorhandler(object sender, x509certificate certificate, x509chain chain, sslpolicyerrors sslpolicyerrors)

6.完成的**如下

using system.text;

using system.timers;

using system.configuration;

using system.net;

using system.net.security;

using system.security.cryptography.x509certificates;

namespace yourcompanywinservice

protected override void onstart(string args)

catch

//服務從endhour開始不再執行

trycatch

//interval

trycatch

//start to work

timer1.enabled = true;

timer1.interval = interval;

timer1.elapsed += new elapsedeventhandler(tmrtimerstimer_elapsed);

timer1.start();           

}protected override void onstop()

private void tmrtimerstimer_elapsed(object sender, system.timers.elapsedeventargs e)}}

public static bool ignorecertificateerrorhandler(object sender, x509certificate certificate, x509chain chain, sslpolicyerrors sslpolicyerrors)}}

7.新增安裝嚮導installer

在windows service設計介面,右鍵快捷選單上選擇新增installer,如下圖:

在installer介面中,右鍵serviceprocessinstaller1屬性,設定account=localsystem,如下圖:

繼續在installer介面中,右鍵serviceinstaller1屬性,設定description,displayname,servicename,starttype等屬性,如下圖:

displayname最重要, 它用來顯示在service列表裡, 再寫一下description.

8.編譯專案.

9.安裝/解除安裝

安裝 installutil.exe d:\testservice\lzdcallwcfservice.exe

解除安裝 installutil.exe /u d:\testservice\lzdcallwcfservice.exe

筆者伺服器win2008 x64, installutil.exe的位置在c:\windows\microsoft.net\framework\v4.0.30319, 而不是 c:\windows\microsoft.net\framework64\v4.0.30319.

具體步驟:右鍵vs2010裡的命令列tools,選擇run as administrator, 在cmd視窗中輸入 installutil.exe d:\testservice\lzdcallwcfservice.exe

得到如下結果,表示windows service 這就安裝成功了。

c:\program files (x86)\microsoft visual studio 10.0\vc>installutil.exe d:\testservice3\lzdservice.exe

microsoft (r) .net framework installation utility version 4.0.30319.1

running a transacted installation.

beginning the install phase of the installation.

see the contents of the log file for the d:\testservice3\lzdservice.exe

...the commit phase completed successfully.

the transacted install has completed.

10.啟動

在管理工具》服務中找到該服務,右鍵屬性,在引數中輸入啟動引數。

10.除錯

windows service除錯起來不象普通的專案, 直接執行起來(或按f5除錯),  需要先install,然後attach a debugger到該service程序.  install-load-start

建立,安裝,除錯 Windows Service

把步驟寫下來備忘。目的 要從windows service裡定時呼叫wcf服務,做一些事情。1.新建立乙個windows service專案,然後新增乙個lzdcallwcfservice.cs的windows service類.2.新增starthour,endhour和interval三個引數,...

為服務建立安裝程式

1 返回到 service1 的 設計 檢視。2 單擊設計器的背景以選擇服務本身,而不是它的任何內容。3 在 屬性 視窗中,單擊屬性列表下面灰色區域中的 新增安裝程式 鏈結。預設情況下,向您的專案新增包含兩個安裝程式的元件類。將該元件命名為 projectinstaller,它包含的安裝程式分別是服...

建立安全組

操作場景 您可以建立安全組並定義安全組中的規則,將vpc中的彈性雲伺服器劃分成不同的安全域,以提公升彈性雲伺服器訪問的安全性。建議您將不同公網訪問策略的彈性雲伺服器劃分到不同的安全組。操作步驟 登入管理控制台。在管理控制台左上角單擊 在系統首頁,選擇 網路 虛擬私有雲 在左側導航樹選擇 訪問控制 安...