一個Windows Service項目的完整開發過程

(一)創建項目文件css

先創建一個解決方案文件,而後添加三個項目。html

{ID%TAI8GBABV[N970I(EN7

分別是:數據庫

(1)Windows服務項目  -----ActiveMQSenderService項目,服務主要是定時輪詢某表,將更新發送到Active MQ隊列。app

(2)測試項目  ----- ActiveMQServiceTest項目,因爲Windows服務不便於測試,因此專門創建一個控制檯項目用來測試業務邏輯正確性。tcp

(3)業務邏輯項目 ----- ActiveMQProducer項目,將總體業務邏輯從Windows服務分離出去,下降耦合,便於維護。ide

(二)編寫ActiveMQSenderService項目測試

1。首先添加對ActiveMQProducer的引用ui

2。創建項目文件以下結構this

5R0HO}7U[7KMZQ2FP}4H4WB

其中: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

[[HR]XT_00~6$Q5]VQ_X$$H

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
Install.bat
   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.設置服務安裝文件

image

image

}D2W48%ORX$F(]$EDKSWQ41

(1)打開ProjectInstall右鍵屬性界面,添加一個serviceInstaller1控件,設置控件的右鍵屬性:ServiceName爲指定名稱。

Z9IYLO050Q4IWB)~N$ESS`G

(2)設置serviceProcessInstaller1控件,將Account設置爲LocalSystem.

OV56S2I}0MJ@5MGRW[YXPJU

(三)創建業務邏輯項目:主要包括:操做數據庫,日誌操做,ActiveMQ操做幾個邏輯。

(四)創建業務邏輯測試項目:引用業務邏輯項目,測試想要測試的各類業務邏輯便可。

(五)測試服務方法:

(1)安裝服務,運行Install.bat

}ZB{T4_[WS93AD{4P9_{44G

(2)附加進程

[8FJII(4CA@8EW$A5@L}G(I

 

引用:

http://www.cnblogs.com/xpvincent/p/3305997.html

http://www.cnblogs.com/alala666888/p/3421492.html

相關文章
相關標籤/搜索