一个Windows Service项目的完整开发过程
(一)建立项目文件
先建立一个解决方案文件,然后添加三个项目。

分别是:
(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; }
.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项目的完整开发过程的更多相关文章
- C#创建一个Windows Service
Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...
- vs2010 windows service 项目不能引用类库项目
在一个windows 服务项目A中,引用了另外一个项目B,可以使用自动完成,引用其他项目中的类,按理,可以自动提示了,应该就是没问题了,但编译时却提示"未能找到类型或命名空间名称" ...
- Windows Service 项目中 Entity Framework 无法加载的问题
Windows Service 项目引用了别的类库项目,别的项目用到了 Entity Framework(通过Nuget引入),但是我的 Windows Service 无法开启,于是我修改了 App ...
- 让自己的C++程序(非服务程序)运行为一个windows service
因为项目的一些变化和原因,需要把数据处理的一个后台程序创建为一个windows服务,运行以下命令能创建成功: sc create "MyApp Service Name" binP ...
- 使用Windows Service Wrapper快速创建一个Windows Service
前言 今天介绍一个小工具的使用.我们都知道Windows Service是一种特殊的应用程序,它的好处是可以一直在后台运行,相对来说,比较适合一些需要一直运行同时不需要过多用户干预的应用程序,这一类我 ...
- 使用Windows Service Wrapper快速创建一个Windows Service 如nginx
前言 今天介绍一个小工具的使用.我们都知道Windows Service是一种特殊的应用程序,它的好处是可以一直在后台运行,相对来说,比较适合一些需要一直运行同时不需要过多用户干预的应用程序,这一类我 ...
- 创建一个Windows Service 程序
1.新建Windows项目,选择"Windows服务"类型的项目. 2.在生成的Service1.cs中代码中写你需要的代码,如下: using System; using Sys ...
- 新建一个Windows Service的方法
http://www.cnblogs.com/YanPSun/archive/2010/05/22/1741381.html http://blog.csdn.net/m15188153014/art ...
- Windows Service 服务搭配FluentScheduler实现定时任务调度
Windows Service 服务 创建Windows Service 项目 创建一个Windows Service项目,并将项目名称改为 TaskWindowService 在解决方案资源管理器内 ...
随机推荐
- C# WinForm中 让控件全屏显示的实现代码
夏荣全 ( lyout(at)163.com )原文 C#中让控件全屏显示的实现代码(WinForm) 有时候需要让窗口中某一块的内容全屏显示,比如视频播放.地图等等.经过摸索,暂时发现两种可行方法, ...
- [原创]C语言利用pcre正则表达式库
C语言使用正则表达式,可以利用pcre库,这个比较不错的哦. 在使用过程中,利用python进行测试正则表达式是否OK,后发现出现了问题.如下所示: regex.c:11:18: warning: u ...
- Linux--使用expect进行自动交互
在linux下进行一些操作时,有时需要与机器进行一些交互操作,比如切换账号时输入账号密码,传输文件时输入账号密码登陆远程机器等,但有时候这些动作需要在shell脚本中进行,这个时候就可以使用expec ...
- QT多线程笔记
1.QT多线程涉及到主线程和子线程之间交互大量数据的时候,使用QThread并不方便,因为run()函数本身不能接受任何参数,因此只能通过信号和槽的交互来获取数据,如果只是单方面简单交互数据还过得去, ...
- java 中 正则 正则表达式 匹配 url
不多说 [http|https]+[://]+[0-9A-Za-z:/[-]_#[?][=][.][&]]* 这个就是匹配 网络上的网址 又称 url . 最起码 绝大部分的taobao ur ...
- spring-boot系列:初试spring-boot
部署父工程 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http: ...
- 第二百五十三、四、五天 how can I 坚持
出去玩了几天,好累啊. 周五,坐了半天车.到了西柏坡,下午撕名牌,好疯狂啊,最终还是以有人受伤为代价结束了战斗.晚上吃蛋糕.水饺,还有面条,就是我的奖品没拿到.哎.. 周六,上午滑雪,两年没滑了,都忘 ...
- PopupWindow添加动画效果
1.定义PopupWindow弹出与消失的两个动画文件,放在anim文件夹下 popup_enter.xml <?xml version="1.0" encoding=&qu ...
- #c word转换PDF
需要引用Microsoft.Office.Interop.Word,版本是07之上的. 这个版本会判断文件是否被占用. using Microsoft.Office.Interop.Word; usi ...
- uc/os学习入门:在32位pc机上搭建编译环境
由于学习ucos的入门资料中所使用的编译器大多都是Borland c ++ 3.1或者Borland c++4.5,为了降低学习的难度最好所用的编译器与书本上的一致.由于4.5版本稍高,所以最终决定用 ...