日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

如何用.NET創(chuàng)建Windows服務(wù)

 道小然 2014-04-25
 我們將研究如何創(chuàng)建一個(gè)作為Windows服務(wù)的應(yīng)用程序。內(nèi)容包含什么是Windows服務(wù),如何創(chuàng)建、安裝和調(diào)試它們。會(huì)用到System.ServiceProcess.ServiceBase命名空間的類。
什么是Windows服務(wù)?


  Windows服務(wù)應(yīng)用程序是一種需要長期運(yùn)行的應(yīng)用程序,它對(duì)于服務(wù)器環(huán)境特別適合。它沒有用戶界面,并且也不會(huì)產(chǎn)生任何可視輸出。任何用戶消息都會(huì)被寫進(jìn)Windows事件日志。計(jì)算機(jī)啟動(dòng)時(shí),服務(wù)會(huì)自動(dòng)開始運(yùn)行。它們不要用戶一定登錄才運(yùn)行,它們能在包括這個(gè)系統(tǒng)內(nèi)的任何用戶環(huán)境下運(yùn)行。通過服務(wù)控制管理器,Windows服務(wù)是可控的,可以終止、暫停及當(dāng)需要時(shí)啟動(dòng)。

Windows 服務(wù),以前的NT服務(wù),都是被作為Windows NT操作系統(tǒng)的一部分引進(jìn)來的。它們在Windows 9x及Windows Me下沒有。你需要使用NT級(jí)別的操作系統(tǒng)來運(yùn)行Windows服務(wù),諸如:Windows NT、Windows 2000 Professional或Windows 2000 Server。舉例而言,以Windows服務(wù)形式的產(chǎn)品有:Microsoft Exchange、SQL Server,還有別的如設(shè)置計(jì)算機(jī)時(shí)鐘的Windows Time服務(wù)。


創(chuàng)建一個(gè)Windows服務(wù)

  我們即將創(chuàng)建的這個(gè)服務(wù)除了演示什么也不做。服務(wù)被啟動(dòng)時(shí)會(huì)把一個(gè)條目信息登記到一個(gè)數(shù)據(jù)庫當(dāng)中來指明這個(gè)服務(wù)已經(jīng)啟動(dòng)了。在服務(wù)運(yùn)行期間,它會(huì)在指定的時(shí)間間隔內(nèi)定期創(chuàng)建一個(gè)數(shù)據(jù)庫項(xiàng)目記錄。服務(wù)停止時(shí)會(huì)創(chuàng)建最后一條數(shù)據(jù)庫記錄。這個(gè)服務(wù)會(huì)自動(dòng)向Windows應(yīng)用程序日志當(dāng)中登記下它成功啟動(dòng)或停止時(shí)的記錄。

  Visual Studio .NET能夠使創(chuàng)建一個(gè)Windows服務(wù)變成相當(dāng)簡單的一件事情。啟動(dòng)我們的演示服務(wù)程序的說明概述如下。

1. 新建一個(gè)項(xiàng)目
2. 從一個(gè)可用的項(xiàng)目模板列表當(dāng)中選擇Windows服務(wù)
3. 設(shè)計(jì)器會(huì)以設(shè)計(jì)模式打開
4. 從工具箱的組件表當(dāng)中拖動(dòng)一個(gè)Timer對(duì)象到這個(gè)設(shè)計(jì)表面上 (注意: 要確保是從組件列表而不是從Windows窗體列表當(dāng)中使用Timer)
5. 設(shè)置Timer屬性,Enabled屬性為False,Interval屬性30000毫秒
6. 切換到代碼視圖頁(按F7或在視圖菜單當(dāng)中選擇代碼),然后為這個(gè)服務(wù)填加功能


Windows服務(wù)的構(gòu)成

  在你類后面所包含的代碼里,你會(huì)注意到你所創(chuàng)建的Windows服務(wù)擴(kuò)充了System.ServiceProcess.Service類。所有以.NET方式建立的Windows服務(wù)必須擴(kuò)充這個(gè)類。它會(huì)要求你的服務(wù)重載下面的方法,Visual Studio默認(rèn)時(shí)包括了這些方法。

· Dispose – 清除任何受控和不受控資源(managed and unmanaged resources)
· OnStart – 控制服務(wù)啟動(dòng)
· OnStop – 控制服務(wù)停止

數(shù)據(jù)庫表腳本樣例

  在這個(gè)例子中使用的數(shù)據(jù)庫表是使用下面的T-SQL腳本創(chuàng)建的。我選擇SQL Server數(shù)據(jù)庫。你可以很容易修改這個(gè)例子讓它在Access或任何你所選擇的別的數(shù)據(jù)庫下運(yùn)行。

CREATE TABLE [dbo].[MyServiceLog] (
   [in_LogId] [int] IDENTITY (1, 1) NOT NULL,
   [vc_Status] [nvarchar] (40)
           COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
   [dt_Created] [datetime] NOT NULL
) ON [PRIMARY]

Windows服務(wù)樣例

  下面就是我命名為MyService的Windows服務(wù)的所有源代碼。大多數(shù)源代碼是由Visual Studio自動(dòng)生成的。

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.ServiceProcess;

namespace CodeGuru.MyWindowsService
{
public class MyService : System.ServiceProcess.ServiceBase
{
   private System.Timers.Timer timer1;
   /// <remarks>
   /// Required designer variable.
   /// </remarks>
   private System.ComponentModel.Container components = null;

   public MyService()
   {
       // This call is required by the Windows.Forms
       // Component Designer.
     InitializeComponent();
   }

   // The main entry point for the process
   static void Main()
   {
     System.ServiceProcess.ServiceBase[] ServicesToRun;

   
     ServicesToRun = new System.ServiceProcess.ServiceBase[]
{ new MyService() };

     System.ServiceProcess.ServiceBase.Run(ServicesToRun);
   }

   /// <summary>
   /// Required method for Designer support - do not modify
   /// the contents of this method with the code editor.
   /// </summary>
   private void InitializeComponent()
   {
     this.timer1 = new System.Timers.Timer();
     ((System.ComponentModel.ISupportInitialize)
(this.timer1)).BeginInit();
     //
     // timer1
     //
     this.timer1.Interval = 30000;
     this.timer1.Elapsed +=
   new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
     //
     // MyService
     //
     this.ServiceName = "My Sample Service";
     ((System.ComponentModel.ISupportInitialize)
(this.timer1)).EndInit();

   }

   /// <summary>
   /// Clean up any resources being used.
   /// </summary>
   protected override void Dispose( bool disposing )
   {
     if( disposing )
     {
      if (components != null)
      {
         components.Dispose();
      }
     }
     base.Dispose( disposing );
   }

   /// <summary>
   /// Set things in motion so your service can do its work.
   /// </summary>
   protected override void OnStart(string[] args)
   {
     this.timer1.Enabled = true;
     this.LogMessage("Service Started");
   }

   /// <summary>
   /// Stop this service.
   /// </summary>
   protected override void OnStop()
   {
     this.timer1.Enabled = false;
     this.LogMessage("Service Stopped");
   }

   /*
    * Respond to the Elapsed event of the timer control
    */
   private void timer1_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
   {
     this.LogMessage("Service Running");
   }

   /*
    * Log specified message to database
    */
   private void LogMessage(string Message)
   {
     SqlConnection connection = null;
     SqlCommand command = null;
     try
     {
      connection = new SqlConnection(
"Server=localhost;Database=SampleDatabase;Integrated
Security=false;User Id=sa;Password=;");
command = new SqlCommand(
"INSERT INTO MyServiceLog (vc_Status, dt_Created)
VALUES ('" + Message + "',getdate())", connection);
      connection.Open();
      int numrows = command.ExecuteNonQuery();
     }
     catch( Exception ex )
     {
      System.Diagnostics.Debug.WriteLine(ex.Message);
     }
     finally
     {
      command.Dispose();
      connection.Dispose();
     }
   }
}
}

安裝Windows服務(wù)

  Windows服務(wù)不同于普通Windows應(yīng)用程序。不可能簡簡單單地通過運(yùn)行一個(gè)EXE就啟動(dòng)Windows服務(wù)了。安裝一個(gè)Windows服務(wù)應(yīng)該通過使用.NET Framework提供的InstallUtil.exe來完成,或者通過諸如一個(gè)Microsoft Installer (MSI)這樣的文件部署項(xiàng)目完成。


添加服務(wù)安裝程序

  創(chuàng)建一個(gè)Windows服務(wù),僅用InstallUtil程序去安裝這個(gè)服務(wù)是不夠的。你必須還要把一個(gè)服務(wù)安裝程序添加到你的Windows服務(wù)當(dāng)中,這樣便于InstallUtil或是任何別的安裝程序知道應(yīng)用你服務(wù)的是怎樣的配置設(shè)置。

1. 將這個(gè)服務(wù)程序切換到設(shè)計(jì)視圖
2. 右擊設(shè)計(jì)視圖選擇“添加安裝程序”
3. 切換到剛被添加的ProjectInstaller的設(shè)計(jì)視圖
4. 設(shè)置serviceInstaller1組件的屬性:
    1) ServiceName = My Sample Service
    2) StartType = Automatic
5. 設(shè)置serviceProcessInstaller1組件的屬性
    1) Account = LocalSystem
6. 生成解決方案

  在完成上面的幾個(gè)步驟之后,會(huì)自動(dòng)由Visual Studio產(chǎn)生下面的源代碼,它包含于ProjectInstaller.cs這個(gè)源文件內(nèi)。

using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;

namespace CodeGuru.MyWindowsService
{
/// <summary>
/// Summary description for ProjectInstaller.
/// </summary>
[RunInstaller(true)]
public class ProjectInstaller :
System.Configuration.Install.Installer
{
   private System.ServiceProcess.ServiceProcessInstaller
serviceProcessInstaller1;
   private System.ServiceProcess.ServiceInstaller serviceInstaller1;
   /// <summary>
   /// Required designer variable.
   /// </summary>
   private System.ComponentModel.Container components = null;

   public ProjectInstaller()
   {
     // This call is required by the Designer.
     InitializeComponent();

     // TODO: Add any initialization after the InitComponent call
   }

   #region Component Designer generated code
   /// <summary>
   /// Required method for Designer support - do not modify
   /// the contents of this method with the code editor.
   /// </summary>
   private void InitializeComponent()
   {
     this.serviceProcessInstaller1 = new
System.ServiceProcess.ServiceProcessInstaller();
     this.serviceInstaller1 = new
System.ServiceProcess.ServiceInstaller();
     //
     // serviceProcessInstaller1
     //
     this.serviceProcessInstaller1.Account =
System.ServiceProcess.ServiceAccount.LocalSystem;
     this.serviceProcessInstaller1.Password = null;
     this.serviceProcessInstaller1.Username = null;
     //
     // serviceInstaller1
     //
     this.serviceInstaller1.ServiceName = "My Sample Service";
     this.serviceInstaller1.StartType =
System.ServiceProcess.ServiceStartMode.Automatic;
     //
     // ProjectInstaller
     //
     this.Installers.AddRange(new
System.Configuration.Install.Installer[]
{this.serviceProcessInstaller1, this.serviceInstaller1});
}
   #endregion
}
}

用InstallUtil安裝Windows服務(wù)

  現(xiàn)在這個(gè)服務(wù)已經(jīng)生成,你需要把它安裝好才能使用。下面操作會(huì)指導(dǎo)你安裝你的新服務(wù)。

1. 打開Visual Studio .NET命令提示
2. 改變路徑到你項(xiàng)目所在的bin\Debug文件夾位置(如果你以Release模式編譯則在bin\Release文件夾)
3. 執(zhí)行命令“InstallUtil.exe MyWindowsService.exe”注冊這個(gè)服務(wù),使它建立一個(gè)合適的注冊項(xiàng)。
4. 右擊桌面上“我的電腦”,選擇“管理”就可以打計(jì)算機(jī)管理控制臺(tái)
5. 在“服務(wù)和應(yīng)用程序”里面的“服務(wù)”部分里,你可以發(fā)現(xiàn)你的Windows服務(wù)已經(jīng)包含在服務(wù)列表當(dāng)中了
6. 右擊你的服務(wù)選擇啟動(dòng)就可以啟動(dòng)你的服務(wù)了

  在每次需要修改Windows服務(wù)時(shí),這就會(huì)要求你卸載和重新安裝這個(gè)服務(wù)。不過要注意在卸載這個(gè)服務(wù)前,最好確保服務(wù)管理控制臺(tái)已經(jīng)關(guān)閉,這會(huì)是一個(gè)很好的習(xí)慣。如果沒有這樣操作的話,你可能在卸載和重安裝Windows服務(wù)時(shí)會(huì)遇到麻煩。僅卸載服務(wù)的話,可以執(zhí)行相的InstallUtil命令用于注銷服務(wù),不過要在后面加一個(gè)/u命令開關(guān)。


調(diào)試Windows服務(wù)

  從另外的角度度看,調(diào)試Windows服務(wù)絕不同于一個(gè)普通的應(yīng)用程序。調(diào)試Windows服務(wù)要求的步驟更多。服務(wù)不能象你對(duì)普通應(yīng)用程序做的那樣,只要簡單地在開發(fā)環(huán)境下執(zhí)行就可以調(diào)試了。服務(wù)必須首先被安裝和啟動(dòng),這一點(diǎn)在前面部分我們已經(jīng)做到了。為了便于跟蹤調(diào)試代碼,一旦服務(wù)被啟動(dòng),你就要用Visual Studio把運(yùn)行的進(jìn)程附加進(jìn)來(attach)。記住,對(duì)你的Windows服務(wù)做的任何修改都要對(duì)這個(gè)服務(wù)進(jìn)行卸載和重安裝。


附加正在運(yùn)行的Windows服務(wù)

  為了調(diào)試程序,有些附加Windows服務(wù)的操作說明。這些操作假定你已經(jīng)安裝了這個(gè)Windows服務(wù)并且它正在運(yùn)行。

1. 用Visual Studio裝載這個(gè)項(xiàng)目
2. 點(diǎn)擊“調(diào)試”菜單
3. 點(diǎn)擊“進(jìn)程”菜單
4. 確保 顯示系統(tǒng)進(jìn)程 被選
5. 在 可用進(jìn)程 列表中,把進(jìn)程定位于你的可執(zhí)行文件名稱上點(diǎn)擊選中它
6. 點(diǎn)擊 附加 按鈕
7. 點(diǎn)擊 確定
8. 點(diǎn)擊 關(guān)閉
9. 在timer1_Elapsed方法里設(shè)置一個(gè)斷點(diǎn),然后等它執(zhí)行


總結(jié)

  現(xiàn)在你應(yīng)該對(duì)Windows服務(wù)是什么,以及如何創(chuàng)建、安裝和調(diào)試它們有一個(gè)粗略的認(rèn)識(shí)了。Windows服務(wù)的額處的功能你可以自行研究。這些功能包括暫停(OnPause)和恢復(fù)(OnContinue)的能力。暫停和恢復(fù)的能力在默認(rèn)情況下沒有被啟用,要通過Windows服務(wù)屬性來設(shè)置。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請遵守用戶 評(píng)論公約

    類似文章 更多