使用Topshelf创建Windows 服务简要的介绍了创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的介绍使用使用Topshelf创建Windows 服务。Topshelf是一个开源的跨平台的宿主服务框架,支持Windows和Mono,只需要几行代码就可以构建一个很方便使用的服务宿主。

1、Topshelf的代码托管在http://topshelf-project.com/,可以在这里下载到最新的代码。

2、使用Visual Studio创建一个控制台应用程序引用程序集TopShelf.dll 合log4net.dll 。

3、创建一个简单的服务类,里面包含两个方法Start和Stop,这个服务只是演示代码,所以我们每隔5秒输出一个日志。

using System;
using System.Timers;
using log4net;

namespace SampleWindowsService
{
public class SampleService
{
private Timer _timer = null;
readonly ILog _log = LogManager.GetLogger(typeof(SampleService));

public SampleService()
{
double interval = 5000;
_timer = new Timer(interval);
_timer.Elapsed += new ElapsedEventHandler(OnTick);
}

protected virtual void OnTick(object sender, ElapsedEventArgs e)
{
_log.Debug("Tick:" + DateTime.Now.ToLongTimeString());
}

public void Start()
{
_log.Info("SampleService is Started");

_timer.AutoReset = true;
_timer.Enabled = true;
_timer.Start();
}

public void Stop()
{
_log.Info("SampleService is Stopped");

_timer.AutoReset = false;
_timer.Enabled = false;
}
}
}

4、在Main方法中使用Topshelf宿主我们的服务,主要是告诉Topshelf如何设置我们的服务的配置和启动和停止的时候的方法调用。

using System.IO;
using log4net.Config;
using Topshelf;

namespace SampleWindowsService
{
class Program
{
static void Main(string[] args)
{
XmlConfigurator.ConfigureAndWatch(
new FileInfo(".\\log4net.config"));

var host = HostFactory.New(x =>
{
x.EnableDashboard();
x.Service<SampleService>(s =>
{
s.SetServiceName("SampleService");
s.ConstructUsing(name => new SampleService());
s.WhenStarted(tc =>
{
XmlConfigurator.ConfigureAndWatch(
new FileInfo(".\\log4net.config"));
tc.Start();
});
s.WhenStopped(tc => tc.Stop());
});

x.RunAsLocalSystem();
x.SetDescription("SampleService Description");
x.SetDisplayName("SampleService");
x.SetServiceName("SampleService");
});

host.Run();
}
}
}

4、配置Log4net和运行我们的服务,服务可以当作控制台来运行,这在开发的时候是非常方便的。服务的安装很方便
SampleWindowsService.exe install
安装成功后,可以通过服务控制台启动,或者也可以通过一下命令运行
SampleWindowsService.exe start
服务的卸载方法也非常简单了
SampleWindowsService.exe uninstall

使用Topshelf 5步创建Windows 服务的更多相关文章

  1. 使用Topshelf 5步创建Windows 服务 z

    使用Topshelf创建Windows 服务简要的介绍了创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with T ...

  2. 使用 Topshelf 组件一步一步创建 Windows 服务 (2) 使用Quartz.net 调度

    上一篇说了如何使用 Topshelf 组件快速创建Windows服务,接下来介绍如何使用 Quartz.net 关于Quartz.net的好处,网上搜索都是一大把一大把的,我就不再多介绍. 先介绍需要 ...

  3. 使用Topshelf组件 一步一步创建 Windows 服务

    我们先来介绍一下使用它的好处,以下论述参考自其他大神. topshelf是创建windows服务的一种方式,相比原生实现ServiceBase.Install.Installer更为简单方便, 我们只 ...

  4. 使用Topshelf创建Windows 服务

    本文转载: http://www.cnblogs.com/aierong/archive/2012/05/28/2521409.html http://www.cnblogs.com/jys509/p ...

  5. 使用Topshelf创建Windows服务

    概述 Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的 ...

  6. [Solution] Microsoft Windows 服务(2) 使用Topshelf创建Windows服务

    除了通过.net提供的windows服务模板外,Topshelf是创建Windows服务的另一种方法. 官网教程:http://docs.topshelf-project.com/en/latest/ ...

  7. Topshelf创建Windows服务

    使用Topshelf创建Windows服务 概述 Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps ...

  8. 使用 Topshelf 结合 Quartz.NET 创建 Windows 服务

    Ø  前言 之前一篇文章已经介绍了,如何使用 Topshelf 创建 Windows 服务.当时提到还缺少一个任务调度框架,就是 Quartz.NET.而本文就展开对 Quartz.NET 的研究,以 ...

  9. 使用 Topshelf 创建 Windows 服务

    Ø  前言 C# 创建 Windows 服务的方式有很多种,Topshelf 就是其中一种方式,而且使用起来比较简单.下面使用 Visual Studio Ultimate 2013 演示一下具体的使 ...

随机推荐

  1. mysql关于数据库表的水平拆分和垂直拆分

    最初知道水平垂直分表的时候是刚参加工作不久的时候,知道了这个概念,但是公司用户量和数据量始终没上来,所以也没用到过,知道有一天到了一家新公司后,这些才被应用到实际开发中,这里我就大概说说关于水平和垂直 ...

  2. UVALive - 6912 Prime Switch (状压DP)

    题目链接:传送门 [题意]有n个灯,m个开关,灯的编号从1~n,每个开关上有一个质数,这个开关同时控制编号为这个质数的倍数的灯,问最多有多少灯打开. [分析]发现小于根号1000的质数有10个左右,然 ...

  3. android 后台 activity 被系统回收 保存状态

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 活动被系统回收, 要保存状态 ,用到 活动的 在保存实例时候 的 这个方法. 当系统异常 ...

  4. CodeForces600E Lomsat gelral 线段树合并

    从树上启发式合并搜出来的题 然而看着好像线段树合并就能解决??? 那么就用线段树合并解决吧 维护\(max, sum\)表示值域区间中的一个数出现次数的最大值以及所有众数的和即可 复杂度\(O(n \ ...

  5. bzoj 4573: [Zjoi2016]大森林 lct splay

    http://www.lydsy.com/JudgeOnline/problem.php?id=4573 http://blog.csdn.net/lych_cys/article/details/5 ...

  6. 【assembly】用汇编写的一个BMP图片读取器

    ;----------------------------- ;文件满足256色调的 ;----------------------------- Stack    Segment           ...

  7. 鸟哥的私房菜:Bash shell(一)-Bash shell功能简介

    Bash shell系列里,由变量谈起,先讲到环境变量的功能与修改的问题, 然后会继续提到历史指令的运用.接下来,就会谈一下『数据流重导向』这个重要概念, 最后就是管线命令的利用! 一  Bash s ...

  8. hdu 5228 枚举

    题意:在前往ZJOI2015一试的路上,ZCC在同Fsygd打德州扑克时输光了所有的筹码.不过ZCC最近学会了一些黑技术.现在,他能够在游戏过程中更换任何他想要更换的牌.ZCC想要通过更换尽量少的牌得 ...

  9. Python168的学习笔记8

    #coding:utf8 #斐波那契数列,第三项起,每一项都等于前两项之和 def memo(func): cache = {}#闭包 def wrap(*args): if args not in ...

  10. euclidea 3.0 全三星 攻略

    euclidea攻略 游戏地址 http://www.euclidea.xyz/en/game/#/packs 攻略 Alpha level : 1.1 line tool 3L3E 智障题 1.2 ...