一个Windows Service项目的完整开发过程

(一)建立项目文件

先建立一个解决方案文件,然后添加三个项目。

{ID%TAI8GBABV[N970I(EN7

分别是:

(1)Windows服务项目  -----ActiveMQSenderService项目,服务主要是定时轮询某表,将更新发送到Active MQ队列。

(2)测试项目  ----- ActiveMQServiceTest项目,由于Windows服务不便于测试,所以专门建立一个控制台项目用来测试业务逻辑正确性。

(3)业务逻辑项目 ----- ActiveMQProducer项目,将整体业务逻辑从Windows服务分离出去,降低耦合,便于维护。

(二)编写ActiveMQSenderService项目

1。首先添加对ActiveMQProducer的引用

2。建立项目文件如下结构

5R0HO}7U[7KMZQ2FP}4H4WB

其中:

(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

转载于:https://www.cnblogs.com/youchun/p/3459257.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值