【Window Service】关于Window Service的两三事
引言
Window Service通常用于寄宿WCF服务或者定时作业.下面记录一下它的用法.
创建
创建Window Service项目后,可以看到Program和Service1类.Program是程序的主入口,而Service1则是我们逻辑实现的主要地方 ,两个关键方法是OnStart和OnStop,用于实现服务启动和结束时的逻辑.
安装
在Service1类的设计界面上右击,选择添加安装程序,就可以完成了安装程序的创建.
Nlog
Window Service作为一个后台程序,发生了什么错误很难获取的信息的,所以需要做个日志记录.下面做个示例,每3秒输出日志记录.
1.打开NuGet程序包管理界面,输入Nlog搜索,安装其中的Nlog和Nlog Configuration,接着在NLog.config做下修改,如下
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
<target xsi:type="Console" name="c" layout="[${longdate}][${level:uppercase=true}][${logger}]${message}${exception}" />
</targets>
<rules
<logger name="*" minlevel="Debug" writeTo="f,c" />
</rules>
2.在Service1中使用Timer和Nlog类,实现每3秒输出日志记录,如下
public partial class Service1 : ServiceBase
{
readonly Logger logger = LogManager.GetCurrentClassLogger();
readonly Timer timer=new Timer();
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer.AutoReset = true;
timer.Interval = ; //单位毫秒
timer.Elapsed+=timer_Elapsed;
timer.Start();
}
protected override void OnStop()
{
timer.Stop();
}
private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
logger.Info(DateTime.Now.ToShortTimeString());
}
//下面两个方法是为了方便调试而创建
public void Start()
{
OnStart(null); }
public void Stop()
{
OnStop();
}
}
部署
下面是安装和卸载脚本,分别保存成Bat文件,放在程序的根目录就可以.
@echo 安装服务
set svc_file=%cd%\WindowsServiceDemo.exe
sc create Service1 binpath= "%svc_file%" displayName= "Service1" depend= tcpip start= auto
net start Service1
@pause
@exit @echo 卸载服务
net stop Service1
sc delete Service1
@pause
@exit
调试
VS貌似没有提供给Window Service调试的工具,要测试只能通过实际部署才能看到效果,但是可以利用TopShell寄宿服务来达到调试的目的.
1. http://topshelf-project.com/ 可以下到最新的类库
2.新建一个控制台程序,引用上面的服务,TopShell,Nlog,就可以编码了,如下
class Program
{
static void Main(string[] args)
{
HostFactory.Run(x =>
{
x.UseNLog(); x.Service<Service1>(s =>
{
s.ConstructUsing(name => new Service1());
s.WhenStarted(tc => tc.Start());
s.WhenStopped(tc => tc.Stop());
}); x.SetServiceName("Service1");
x.SetDisplayName("Service1");
x.SetDescription("每3秒记录日志");
x.RunAsLocalSystem();
x.StartAutomatically();
}); }
3.运行控制台程序,可以看到

小结
本文简单介绍了Window Service从创建到调试的步骤.如有更好的建议,请不吝指教.
参考资料
C# 编写Windows Service(windows服务程序)
【Window Service】关于Window Service的两三事的更多相关文章
- 在Activity,Service,Window中监听Home键和返回键的一些思考,如何把事件传递出来的做法!
在Activity,Service,Window中监听Home键和返回键的一些思考,如何把事件传递出来的做法! 其实像按键的监听,我相信很多人都很熟练了,我肯定也不会说这些基础的东西,所以,前期,还是 ...
- [PWA] 9. Service worker registerion && service work's props, methods and listeners
In some rare cases, you need to ask user to refresh the browsser to update the version. Maybe becaus ...
- service: no such service mysqld 与MySQL的开启,关闭和重启
1.问题原因与解决办法 因为修改了MySQL临时文件的目录后,使用service mysqld restart重启MySQL出现如下错误: service: no such service mysql ...
- Failed to stop iptables.service: Unit iptables.service not loaded.
redhat 7 [root@lk0 ~]# service iptables stop Redirecting to /bin/systemctl stop iptables.service Fai ...
- window.onload和window.document.readystate的探究
在编写前端页面的时候,我们时常需要对页面加载的状态进行判断,以便进行相应的操作. 比如在移动端,时常需要在页面完全加载完成之前,先显示一个loading的图标,等待页面完成加载完成后,才显示出真正要展 ...
- window.location.href = window.location.href 跳转无反应 a 超链接 onclick 点击跳转无反应
错误写法 , 主要是在 href="#"这里 <a href="#" id="send" onclick="return b ...
- window.parent与window.openner区别介绍
今天总结一下js中几个对象的区别和用法: 首先来说说 parent.window与top.window的用法 "window.location.href"."locati ...
- window.parent 与 window.opener
window.parent针对iframe,window.opener针对window.open 父页面parent.jsp: <%@ page language="java" ...
- window.location和window.open
window.location和window.open的区别 window.location = "http://www.baidu.com" 跳转后有后退功能 window.lo ...
- JavaScript(Iframe、window.open、window.showModalDialog)父窗口与子窗口之间的操作
一.Iframe 篇 公共部分 //父对象得到子窗口的值 //ObjectID是窗口标识,ContentID是元素ID function GetValue(ObjectID,ContentID) { ...
随机推荐
- Python之------初识面向对象(Day22)
一.面向过程 VS 面向对象 面向过程的程序设计的核心是过程(流水线式思维),过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西. 优点是:极大的降低了写程序 ...
- Android:日常学习笔记(5)——探究活动(2)
Android:日常学习笔记(5)——探究活动(2) 使用Intent在活动之间穿梭 什么是Intent Intent时Android程序中各组件之间进行交互的一种重要方式,他不仅可以指明当前组件想要 ...
- focus + select
focus使光标定位到目标节点之后 select选中光标所在位置的全部内容
- Shell编程之case条件
一.case条件语句 1.语法 case "变量" in 值 1) 指令 1... ;; 值 2) 指令 2... ;; *) 指令 3... esac case条件语句的执行流程 ...
- P4271 [USACO18FEB]New Barns
题目 P4271 [USACO18FEB]New Barns 做法 这题很长见识啊!! 知识点:两棵树\((A,B)\)联通后,新树的径端点为\(A\)的径端点与\(B\)的径端点的两点 不断加边,那 ...
- jQuery带闹钟的数字时钟
在线演示 本地下载
- python异步库
https://github.com/aio-libs 异步库
- IEnumerable的一些基本方法
在说明用法之后,先要弄点数据. class Product { public int ID { get; set; } public string Name { get; set; } public ...
- Linux下解压分包文件zip(zip/z01/z02)
分包压缩的zip文件不能被7z解压,且这种格式是Windows才能创建出来,在Linux下不会以这种方式去压包.下面是在Linux下处理这种文件的做法: 方法一: cat xx.z01 xx.zip ...
- Java 类及类的构造方法
类 类是一个模子,确定对象将会拥有的特性(属性)和行为(方法). 类的特点 类时对象的类型 具有相同属性和方法的一组对象的集合 构造方法 作用就是对类进行初始化. 如果你没有定议任何构造方法的形式,J ...