把步驟寫下來備忘。
目的: 要從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)
{
}
4.添加wcf服務引用,通過windows service調用wcf中的商務邏輯代碼,這樣,windows service端不必部署什麼邏輯,只起個定時器的作用。
using (Reference1.LocalWCFClient obj = new Reference1.LocalWCFClient())
{
obj.DoWork();
}
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)
{
if (DateTime.Now.Hour > StartHour && DateTime.Now.Hour < EndHour)
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(IgnoreCertificateErrorHandler);
using (Reference1.LocalWCFClient obj = new Reference1.LocalWCFClient())
{
obj.DoWork();
}
}
}
public static bool IgnoreCertificateErrorHandler(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
6.完成的代碼如下
using System.Text;
using System.Timers;
using System.Configuration;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
namespace YourCompanyWinService
{
public partial class YourCompanyWinService : ServiceBase
{
public int StartHour = 1;
public int EndHour = 23;
public int Interval = 1800000; //預設為半小時啟動一次。
private System.Timers.Timer timer1 = null;
public YourCompanyWinService()
{
InitializeComponent();
timer1 = new Timer();
}
protected override void OnStart(string[] args)
{
//服務從StartHour開始可以運行
try
{
StartHour = Convert.ToInt32(args[0]);
}
catch { }
//服務從EndHour開始不再運行
try
{
EndHour = Convert.ToInt32(args[1]);
}
catch { }
//Interval
try
{
Interval = Convert.ToInt32(args[2]);
}
catch { }
//start to work
timer1.Enabled = true;
timer1.Interval = Interval;
timer1.Elapsed += new ElapsedEventHandler(TmrTimersTimer_Elapsed);
timer1.Start();
}
protected override void OnStop()
{
timer1.Enabled = false;
timer1.Stop();
}
private void TmrTimersTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (DateTime.Now.Hour > StartHour && DateTime.Now.Hour < EndHour)
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(IgnoreCertificateErrorHandler);
using (Reference1.LocalWCFClient obj = new Reference1.LocalWCFClient())
{
obj.DoWork();
}
}
}
public static bool IgnoreCertificateErrorHandler(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
}
}
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
Copyright (c) Microsoft Corporation. All rights reserved.
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