引言 

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服务程序)

使用Topshelf创建Windows 服务

NLog文章系列——系列文章目录以及简要介绍

【Window Service】关于Window Service的两三事的更多相关文章

  1. 在Activity,Service,Window中监听Home键和返回键的一些思考,如何把事件传递出来的做法!

    在Activity,Service,Window中监听Home键和返回键的一些思考,如何把事件传递出来的做法! 其实像按键的监听,我相信很多人都很熟练了,我肯定也不会说这些基础的东西,所以,前期,还是 ...

  2. [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 ...

  3. service: no such service mysqld 与MySQL的开启,关闭和重启

    1.问题原因与解决办法 因为修改了MySQL临时文件的目录后,使用service mysqld restart重启MySQL出现如下错误: service: no such service mysql ...

  4. 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 ...

  5. window.onload和window.document.readystate的探究

    在编写前端页面的时候,我们时常需要对页面加载的状态进行判断,以便进行相应的操作. 比如在移动端,时常需要在页面完全加载完成之前,先显示一个loading的图标,等待页面完成加载完成后,才显示出真正要展 ...

  6. window.location.href = window.location.href 跳转无反应 a 超链接 onclick 点击跳转无反应

    错误写法 , 主要是在 href="#"这里 <a href="#" id="send" onclick="return b ...

  7. window.parent与window.openner区别介绍

    今天总结一下js中几个对象的区别和用法: 首先来说说 parent.window与top.window的用法 "window.location.href"."locati ...

  8. window.parent 与 window.opener

    window.parent针对iframe,window.opener针对window.open 父页面parent.jsp: <%@ page language="java" ...

  9. window.location和window.open

    window.location和window.open的区别 window.location = "http://www.baidu.com" 跳转后有后退功能 window.lo ...

  10. JavaScript(Iframe、window.open、window.showModalDialog)父窗口与子窗口之间的操作

    一.Iframe 篇 公共部分 //父对象得到子窗口的值 //ObjectID是窗口标识,ContentID是元素ID function GetValue(ObjectID,ContentID) { ...

随机推荐

  1. 判断点是否在区域的python实现(射线法)

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2018-10-07 15:49:37 # @Author : Sheldon (thi ...

  2. 阿里云公网IP不能使用

    1.开通专用网络 2.在ECS的安全组 创建 专用网络 3.配置规则 4.快速创建规则,增加自己需要入网的端口号,授权对象写:0.0.0.0/0

  3. 剑指offer 面试20题

    面试20题: 题目:表示数值的字符串 题:请实现一个函数用来判断字符串是否表示数值(包括整数和小数).例如,字符串"+100","5e2","-123 ...

  4. mapInfo文件格式详解

    from:http://hotolee.blog.163.com/blog/static/3815229920098434956370 MapInfo以表(Tab)的形式存储信息,每个表是由一组Map ...

  5. 前端之 JQuery

    一.基本选择器 1.#id 概述: 根据给定的ID匹配一个元素. 使用任何的元字符(如 !"#$%&'()*+,./:;<=>?@[\]^`{|}~)作为名称的文本部分, ...

  6. POJ 3468 A Simple Problem with Integers 【线段树】

    题目链接 http://poj.org/problem?id=3468 思路 线段树 区间更新 模板题 在赋初始值的时候,按点更新区间就可以 AC代码 #include <cstdio> ...

  7. Qt核心机制和原理

    转:http://blog.csdn.net/light_in_dark/article/details/64125085 ★了解Qt和C++的关系 ★掌握Qt的信号/槽机制的原理和使用方法 ★了解Q ...

  8. com.android.dex.DexIndexOverflowException: Cannot merge new index 66299 into a non-jumbo instruction

    打包时控制台输出: Error:Execution failed for task ':app:transformClassesWithDexForAll32Release'. > com.an ...

  9. 六、golang中的结构体和方法、接口

    结构体: 1.用来自定义复杂数据结构 2.struct里面可以包含多个字段(属性) 3.struct类型可以定义方法,注意和函数的区分 4.strucr类型是值类型 5.struct类型可以嵌套 6. ...

  10. 写makefile时候的cc和gcc

    Linux 下 的 cc 和 gcc     Linux 下 的 cc 和 gcc 周银辉 在Linux下一会看到cc,另一会又看到gcc,感觉又点混乱的样子.它们是同一个东西么,有啥区别呢 一分为二 ...