C#Windows服務程序開發實例淺析

C#Windows服務程序開發實例:編寫一個C#Windows服務程序,定時從數據庫中拿出記錄發送郵件。 node

測試環境:Visual Studio 2005 SP一、Windows Server 2003 SP2 數據庫

C#Windows服務程序開發實例1、新建項目 設計模式

打開VS2005,新建一個「Windows 服務」項目。 編輯器

C#Windows服務程序開發實例2、添加Timer ide

展開「工具箱」,在「組件」標籤下找到「Timer」雙擊,這時就添加了一個Timer組件,修改「Name」屬性爲「timEmail」、「Enabled」爲「false」、「Interval」爲「60000」。 函數

接下來要作一些修補工做,不知是VS2005的BUG仍是我沒找着地方,在VS2003下是不存在該問題的:剛從「組件」下添加的「Timer」按 理說應該來自「System.Timers命名空間」,也只有「System.Timers.Timer」才能在Windows服務程序中正常工做,可是 如今這個Timer倒是屬於「System.Windows.Forms.Timer」的。因此得稍做修改,打開「.Designer.cs」文件,修改 以下: 工具

 
  1. #region 組件設計器生成的代碼  
  2. //........以上略  
  3. /// <summary>   
  4. /// 設計器支持所需的方法 - 不要  
  5. /// 使用代碼編輯器修改此方法的內容。  
  6. /// </summary>  
  7. private void InitializeComponent()  
  8. {  
  9.     this.components = new System.ComponentModel.Container();  
  10.     //this.timEmail = new System.Windows.Forms.Timer(this.components);原  
  11.     this.timEmail = new System.Timers.Timer();//改  
  12.     this.timEmail.Interval = 60000;  
  13.     this.ServiceName = "Service1";  
  14. }  
  15. #endregion  
  16. //private System.Windows.Forms.Timer timEmail;原  
  17. private System.Timers.Timer timEmail;//改 

C#Windows服務程序開發實例3、添加配置文件 學習

服務每次調用配置文件,獲取一些基本參數,這樣一些變動就可直接修改配置文件而沒必要修改代碼。新建ServiceConfig.xml存放於項目「Bin\Debug\」下: 測試

 
  1. ﹤?xml version="1.0" encoding="utf-8" ?﹥   
  2. ﹤serviceConfig﹥  
  3.     ﹤serviceItem   
  4. name="sendEmail"   
  5. enable="true"   
  6. elapsed="60000"   
  7. connectionString="your database connection..."   
  8. smtp="smtp address"   
  9. account="your email account..."   
  10. password="your password..." ﹥  
  11.     ﹤/serviceItem﹥  
  12. ﹤/serviceConfig﹥ 

C#Windows服務程序開發實例4、如下是實現代碼 this

 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Diagnostics;  
  6. using System.ServiceProcess;  
  7. using System.Text;  
  8. using System.Xml;//操做配置文件  
  9. using System.IO;//寫日誌  
  10. using System.Threading;//使用線程  
  11.  
  12. namespace ClientWindowsService  
  13. {  
  14.     public partial class ClientService : ServiceBase  
  15.     {  
  16. public ClientService()  
  17. {  
  18.     InitializeComponent();  
  19. }  
  20.  
  21. protected override void OnStart(string[] args)  
  22. {  
  23.     //服務啓動  
  24.       this.timEmail.Enabled = true;  
  25.     this.tSendEmail();  
  26. }  
  27.  
  28. protected override void OnStop()  
  29. {  
  30.     //服務中止  
  31.       this.timEmail.Enabled = false;  
  32. }  
  33.  
  34. private void timEmail_Elapsed(object sender,  
  35.  System.Timers.ElapsedEventArgs e)  
  36. {  
  37.     //定時器  
  38.       this.tSendEmail();  
  39. }  
  40.  
  41. //開啓新進程發送郵件  
  42.     private void tSendEmail()  
  43. {  
  44.     Thread t = new Thread(new ThreadStart(sendEmail));  
  45.     t.Start();  
  46. }  
  47.  
  48. //發送郵件函數  
  49.     private void sendEmail()  
  50. {  
  51.     XmlDocument doc = new XmlDocument();  
  52.     //添加System.Windows.Forms引用,獲取執行目錄  
  53.       string configFile = System.Windows.Forms.Application.  
  54. StartupPath.ToString() + "\ServiceConfig.xml";  
  55.     doc.Load(@configFile);  
  56.     XmlElement root = doc.DocumentElement;  
  57.     foreach (XmlNode node in root)  
  58.     {  
  59. //若是配置文件中開啓服務  
  60. if (node.Attributes["name"].Value == "sendEmail" &&  
  61.  node.Attributes["enable"].Value == "true")  
  62. {  
  63.     try 
  64.     {  
  65. //讀取數據庫,發送郵件操做,略  
  66.     }  
  67.     catch (Exception error)  
  68.     {  
  69. //寫錯誤日誌  
  70.     using (StreamWriter sw = new 
  71. StreamWriter(System.Windows.Forms.
  72. Application.StartupPath.ToString() + 
  73. @"" + DateTime.Now.ToString("yyyy-MM-dd") + 
  74. ".txt"true, System.Text.Encoding.UTF8))  
  75. {  
  76.     sw.WriteLine(DateTime.Now.ToString() + ":");  
  77.     sw.WriteLine(error.ToString());  
  78.     sw.WriteLine("----------------
  79. -----------------------------");  
  80.     sw.Close();  
  81. }  
  82.     }  
  83. }  
  84.     }//end foreach  
  85. }  
  86.  
  87.     }//end class  
  88. }//end namespace 

C#Windows服務程序開發實例5、佈署服務

在設計模式下右鍵-->添加安裝程序-->設置serviceProcessInstaller1的Account爲LocalSystem

設置serviceInstaller1的StartType爲Automatic

編譯

在命令模式下執行:%systemroot%\microsoft.net\framework\v2.0.50727\installUtil.exe D:\項目目錄\bin\Debug\可執行文件名.exe

在每次須要修改Windows服務時,這就會要求你卸載和從新安裝這個服務。不過要注意在卸載這個服務前,最好確保服務管理控制檯已經關閉,這會是 一個很好的習慣。若是沒有這樣操做的話,你可能在卸載和重安裝Windows服務時會遇到麻煩。僅卸載服務的話,能夠執行相的InstallUtil命令 用於註銷服務,不過要在後面加一個/u命令開關。

調試Windows服務

從另外的角度度看,調試Windows服務毫不同於一個普通的應用程序。調試Windows服務要求的步驟更多。服務不能象你對普通應用程序作的那 樣,只要簡單地在開發環境下執行就能夠調試了。服務必須首先被安裝和啓動,這一點在前面部分咱們已經作到了。爲了便於跟蹤調試代碼,一旦服務被啓動,你就 要用Visual Studio把運行的進程附加進來(attach)。記住,對你的Windows服務作的任何修改都要對這個服務進行卸載和重安裝。

附加正在運行的Windows服務

爲了調試程序,有些附加Windows服務的操做說明。這些操做假定你已經安裝了這個Windows服務而且它正在運行。

1. 用Visual Studio裝載這個項目

2. 點擊「調試」菜單

3. 點擊「進程」菜單

4. 確保 顯示系統進程 被選

5. 在 可用進程 列表中,把進程定位於你的可執行文件名稱上點擊選中它

6. 點擊 附加 按鈕

7. 點擊 肯定

8. 點擊 關閉

9. 在timer1_Elapsed方法裏設置一個斷點,而後等它執行

C#Windows服務程序開發實例6、代碼下載

http://files.cnblogs.com/linckle/log.rar

C#Windows服務程序開發實例的基本內容就向你介紹到這裏,但願對你學習和了解C#Windows服務程序開發實例有所幫助。

相關文章
相關標籤/搜索