(一)建立项目文件

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

分别是:

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

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

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

(二)编写ActiveMQSenderService项目

1。首先添加对ActiveMQProducer的引用

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

其中:

(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:

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

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));
}
}
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

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>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }需要将文件属性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

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Install.bat

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

   1:  @echo off
   2:  :install
   3:  %SystemRoot%/Microsoft.NET/Framework/v4.0.30319/installutil ActiveMQSenderService.exe
   4:  net start ActiveMQSenderService
   5:  pause
   6:  :end

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

6.设置服务安装文件

(1)打开ProjectInstall右键属性界面,添加一个serviceInstaller1控件,设置控件的右键属性:ServiceName为指定名称。

(2)设置serviceProcessInstaller1控件,将Account设置为LocalSystem.

(三)建立业务逻辑项目:主要包括:操作数据库,日志操作,ActiveMQ操作几个逻辑。

(四)建立业务逻辑测试项目:引用业务逻辑项目,测试想要测试的各种业务逻辑即可。

(五)测试服务方法:

(1)安装服务,运行Install.bat

(2)附加进程

 

引用:

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

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

一个Windows Service项目的完整开发过程的更多相关文章

  1. C#创建一个Windows Service

    Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...

  2. vs2010 windows service 项目不能引用类库项目

    在一个windows 服务项目A中,引用了另外一个项目B,可以使用自动完成,引用其他项目中的类,按理,可以自动提示了,应该就是没问题了,但编译时却提示"未能找到类型或命名空间名称" ...

  3. Windows Service 项目中 Entity Framework 无法加载的问题

    Windows Service 项目引用了别的类库项目,别的项目用到了 Entity Framework(通过Nuget引入),但是我的 Windows Service 无法开启,于是我修改了 App ...

  4. 让自己的C++程序(非服务程序)运行为一个windows service

    因为项目的一些变化和原因,需要把数据处理的一个后台程序创建为一个windows服务,运行以下命令能创建成功: sc create "MyApp Service Name" binP ...

  5. 使用Windows Service Wrapper快速创建一个Windows Service

    前言 今天介绍一个小工具的使用.我们都知道Windows Service是一种特殊的应用程序,它的好处是可以一直在后台运行,相对来说,比较适合一些需要一直运行同时不需要过多用户干预的应用程序,这一类我 ...

  6. 使用Windows Service Wrapper快速创建一个Windows Service 如nginx

    前言 今天介绍一个小工具的使用.我们都知道Windows Service是一种特殊的应用程序,它的好处是可以一直在后台运行,相对来说,比较适合一些需要一直运行同时不需要过多用户干预的应用程序,这一类我 ...

  7. 创建一个Windows Service 程序

    1.新建Windows项目,选择"Windows服务"类型的项目. 2.在生成的Service1.cs中代码中写你需要的代码,如下: using System; using Sys ...

  8. 新建一个Windows Service的方法

    http://www.cnblogs.com/YanPSun/archive/2010/05/22/1741381.html http://blog.csdn.net/m15188153014/art ...

  9. Windows Service 服务搭配FluentScheduler实现定时任务调度

    Windows Service 服务 创建Windows Service 项目 创建一个Windows Service项目,并将项目名称改为 TaskWindowService 在解决方案资源管理器内 ...

随机推荐

  1. HDU5758 Explorer Bo 树形dp

    我是参考这一篇写的:http://blog.csdn.net/fsss_7/article/details/52049474 一点感想:dp[i][0]代表以这个点为根的且总叶子数为偶数个叶子的答案 ...

  2. 《Python核心编程》 第四章 Python对象- 课后习题

    练习 4-1. Python对象.与所有Python对象有关的三个属性是什么?请简单的描述一下. 答:身份.类型和值: 身份:每一个对象都有一个唯一的身份标识自己,可以用id()得到.  类型:对象的 ...

  3. CMDB反思2

    当云灭掉CMDB http://blog.vsharing.com/xqscool/A1193910.html 虽然之前也思考过当运维底层都被替换为云时,现有的传统运维可能就消失了,其所依赖的ITIL ...

  4. javascript AES加密 C#AES解密实现

    首先需要引入js类库 crypto-js(开源),地址:http://code.google.com/p/crypto-js 现在很多人无法打开这个地址不要紧,下面我们会将全部代码贴出来 需要引入 a ...

  5. BBED的安装

    BBED是Block Browser EDitor的缩写,只有linux/unix版本,没有windows版本. 11g中默认是不带bbed的,如果要使用,可以在10g中拷贝过来,然后再进行编译使用. ...

  6. linux 用 SSH2协议远程连接并控制 linux

    [参考链接](http://php.net/manual/zh/ssh2.installation.php) ssh2_exec 并不能打印所有的命令的提示信息 如果有返回的字符串信息,可以打印,或重 ...

  7. json字符串转换为JSONObject和JSONArray

    一.下载json 具体到http://www.json.org/上找java-json下载,并把其放到项目源代码中,这样就可以引用其类对象了 二.具体转化过程 //JSONObject String ...

  8. 机器学习----分布问题(二元,多元变量分布,Beta,Dir)

    这涉及到数学的概率问题. 二元变量分布:          伯努利分布,就是0-1分布(比如一次抛硬币,正面朝上概率) 那么一次抛硬币的概率分布如下: 假设训练数据如下: 那么根据最大似然估计(MLE ...

  9. linux 下载软件

    wget 软件地址 eg: 下载tomcat : wget http://apache.fayea.com/apache-mirror/tomcat/tomcat-7/v7.0.52/bin/apac ...

  10. JavaFx2.0中CSS的应用

    http://user.qzone.qq.com/773534839#!app=2&via=QZ.HashRefresh&pos=1326994508 ———————————————— ...