(一)創建項目文件css
先創建一個解決方案文件,而後添加三個項目。html
分別是:數據庫
(1)Windows服務項目 -----ActiveMQSenderService項目,服務主要是定時輪詢某表,將更新發送到Active MQ隊列。app
(2)測試項目 ----- ActiveMQServiceTest項目,因爲Windows服務不便於測試,因此專門創建一個控制檯項目用來測試業務邏輯正確性。tcp
(3)業務邏輯項目 ----- ActiveMQProducer項目,將總體業務邏輯從Windows服務分離出去,下降耦合,便於維護。ide
(二)編寫ActiveMQSenderService項目測試
1。首先添加對ActiveMQProducer的引用ui
2。創建項目文件以下結構this
其中:spa
(1)ActiveMQSenderService.cs是Windows服務文件
(2)Install.bat和Unstall.bat用來自動安裝和卸載ActiveMQSenderService
(3)ActiveMQSendLog用來記錄服務運行日誌
(4)App.Config用來記錄項目配置文件
(5)ProjectInstall.cs是Windows服務安裝程序
3.編寫服務文件
ActiveMQSenderService.designer.cs:
/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { components = new System.ComponentModel.Container(); this.timer = new System.Timers.Timer(); ((System.ComponentModel.ISupportInitialize)(this.timer)).BeginInit(); this.timer.Enabled = true; this.timer.Interval = 10000; this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_Elapsed); this.ServiceName = "ActiveMQSenderService"; dsQuery = new DataSet(); ((System.ComponentModel.ISupportInitialize)(this.timer)).EndInit(); }
ActiveMQSenderService.cs:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; namespace ActiveMQSenderService { public partial class ActiveMQSenderService : ServiceBase { private System.Timers.Timer timer; private DataSet dsQuery; private DataSet dsLose; public ActiveMQSenderService() { InitializeComponent(); } private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { ActiveMQProducer producer = new ActiveMQProducer(); producer.SendMessageArray(); } protected override void OnStart(string[] args) { this.timer.Enabled = true; Log.LogMessage(string.Format("******Time:{0} Service Started******",DateTime.Now)); } protected override void OnStop() { this.timer.Enabled = false; Log.LogMessage(string.Format("******Time:{0} Service Stopped*******",DateTime.Now)); } } }
4.編寫配置文件
<?xml version="1.0"?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> <connectionStrings> <add name="conn" connectionString="userid='';password='';initialCatelog='XXXXX';server='127.0.0.1';connectTimeout=30;providerName='System.Data.SqlClient'"/> <add name="SqlConnectionString" connectionString="Database=XXXXX;Data Source=localhost;Integrated Security=SSPI;"></add> </connectionStrings> <appSettings> <add key="ActiveMQServerAddress" value="tcp://localhost:61616"/> <add key="QueueName" value="activemqtest"/> </appSettings> </configuration>
須要將文件屬性BuildAction設置爲None
5.編寫Install.bat和Uninstall.bat
Uninstall.bat
1: @echo off
2: :uninstall
3: %SystemRoot%/Microsoft.NET/Framework/v4.0.30319/installutil /uninstall ActiveMQSenderService.exe
4: pause
5: :end
1: @echo off
2: :install
3: %SystemRoot%/Microsoft.NET/Framework/v4.0.30319/installutil ActiveMQSenderService.exe
4: net start ActiveMQSenderService
5: pause
6: :end
6.設置服務安裝文件
(1)打開ProjectInstall右鍵屬性界面,添加一個serviceInstaller1控件,設置控件的右鍵屬性:ServiceName爲指定名稱。
(2)設置serviceProcessInstaller1控件,將Account設置爲LocalSystem.
(三)創建業務邏輯項目:主要包括:操做數據庫,日誌操做,ActiveMQ操做幾個邏輯。
(四)創建業務邏輯測試項目:引用業務邏輯項目,測試想要測試的各類業務邏輯便可。
(五)測試服務方法:
(1)安裝服務,運行Install.bat
(2)附加進程
引用: