简介

Topshelf允许我们快速的开发、调试和部署windows服务。

官方网站

使用方法

第一步:安装

Install-Package Topshelf

Install-Package Topshelf.Log4Net

虽然安装Topshelf.Log4Net不是必须的,不过建议安装。

第二步:实现服务逻辑

ServiceRunner.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.IO; using Topshelf;
using log4net;
using log4net.Config; namespace WHTR.Demos.Topshelf
{
public sealed class ServiceRunner : ServiceControl, ServiceSuspend
{
private static ILog Logger = LogManager.GetLogger(typeof(Program)); private Timer timer; static ServiceRunner()
{
var logCfg = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config");
XmlConfigurator.ConfigureAndWatch(logCfg);
} public bool Start(HostControl hostControl)
{
this.timer = new Timer(new TimerCallback(this.PrintMessage), null, 1000, 1000); return true;
} public bool Stop(HostControl hostControl)
{
throw new NotImplementedException();
} public bool Continue(HostControl hostControl)
{
throw new NotImplementedException();
} public bool Pause(HostControl hostControl)
{
throw new NotImplementedException();
}
private void PrintMessage(object state)
{
Logger.Info(DateTime.Now);
}
}
}

备注:要实现的接口及方法名意义非常明显,这里就不做过多说明了。

第三步:调用

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; using Topshelf;
using Topshelf.ServiceConfigurators; namespace TopshelfDemos
{
class Program
{
static void Main(string[] args)
{
HostFactory.Run(x =>
{
//x.UseLog4Net("~/log4net.config"); x.Service<ServiceRunner>(); x.SetDescription("TopshelfDemos说明");
x.SetDisplayName("TopshelfDemos显示名称");
x.SetServiceName("TopshelfDemos服务名称"); x.EnablePauseAndContinue();
});
}
}
}

第四步:安装&卸载

  • 安装:TopshelfDemos.exe install
  • 卸载:TopshelfDemos.exe uninstall
  • 安装不同的实例:TopshelfDemos.exe install -instance "xxx" -servicename "xxx" -description "xxx" -displayname "xxx"
  • 卸载不同的实例 TopshelfDemos.exe uninstall -instance "xxx"

备注

以前以为开发Windows服务是多么高大上的东西,没想到这么简单。就两步,引用Topshelf.dll,然后实现接口,完成。

Topshelf入门的更多相关文章

  1. topshelf和quartz内部分享

    阅读目录: 介绍 基础用法 调试及安装 可选配置 多实例支持及相关资料 quartz.net 上月在公司内部的一次分享,现把PPT及部分交流内容整理成博客. 介绍 topshelf是创建windows ...

  2. topshelf和quartz

    topshelf和quartz内部分享 阅读目录: 介绍 基础用法 调试及安装 可选配置 多实例支持及相关资料 quartz.net 上月在公司内部的一次分享,现把PPT及部分交流内容整理成博客. 介 ...

  3. 参照示例搭建一个Quertz + Topshelf的一个作业调度服务(基础)

    学习网址:Quartz.NET 入门.使用Topshelf创建Windows服务 来自七七资料 1.直接下载源码 2.配置完成后,安装服务测试应用. 以下是遇到情况和加入的一些内容 1.在进行服务安装 ...

  4. C# Windows服务开发从入门到精通

    一.课程介绍 大家都知道如果想要程序一直运行在windows服务器上,最好是把程序写成windows服务程序:这样程序会随着系统的自动启动而启动,自动关闭而关闭,不需要用户直接登录,直接开机就可以启动 ...

  5. topshelf 开发windows 服务资料

    官方配置 http://docs.topshelf-project.com/en/latest/configuration/config_api.html#service-start-modes to ...

  6. Topshelf 和 Katana:统一的 Web 和服务体系结构

    Topshelf 和 Katana:统一的 Web 和服务体系结构 Wes McClure 下载代码示例 使用 IIS 托管 ASP.NET Web 应用程序已成为业界标准十年有余.构建此类应用程序的 ...

  7. 关于使用Topshelf创建服务

    目录 0. 背景说明 1. 使用Topshelf组件创建Windows服务 1.1 依赖Quartz.net实现定时任务 1.2 依赖于Topshelf创建服务类 1.3 log4net的配置文件lo ...

  8. Angular2入门系列教程7-HTTP(一)-使用Angular2自带的http进行网络请求

    上一篇:Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数 感觉这篇不是很好写,因为涉及到网络请求,如果采用真实的网络请求,这个例子大家拿到手估计还要自己写一个web ...

  9. ABP入门系列(1)——学习Abp框架之实操演练

    作为.Net工地搬砖长工一名,一直致力于挖坑(Bug)填坑(Debug),但技术却不见长进.也曾热情于新技术的学习,憧憬过成为技术大拿.从前端到后端,从bootstrap到javascript,从py ...

随机推荐

  1. projecteuler Problem 9 Special Pythagorean triplet

    A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2 Fo ...

  2. C/C++ 活动预处理器

    错误 1 fatal error C1083: 无法打开包括文件:“iec/i.h”: No such file or directory #ifdef SUPPROT_IEC61850 #inclu ...

  3. 如何获得 request, "request.getSession(true).setAttribute("a",a);"与“request.setAttribute("a",a);”区别

    protected ServletContext getServletContext() { return ServletActionContext.getServletContext();} pro ...

  4. openssl命令用法

    openssl命令 配置文件:/etc/pki/tls/openssl.cnf 命令格式: openssl command [ command_opts ] [ command_args ] 众多子命 ...

  5. poj1741 树上的点分治

    题意: 一棵10000个点的树,每条边的长不超过1000,给定一个值k,问距离不超过k的点对数有多少.(多组数据) 输入样例: 5 4 1 2 3 1 3 1 1 4 2 3 5 1 0 0输出样例: ...

  6. gcc编译时指定链接库的查找目录

    gcc编译时,如果需要链接的库的目录不在标准目录,则需要通过将保护库的目录/aa/bb/cc通过-L/aa/bb/cc 添加到搜索路径中,如: gcc -o xmltest xml_test.cpp ...

  7. mysql处理字符串

    1.从左开始截取字符串 left(str, length) 说明:left(被截取字段,截取长度) 例:select left(content,200) as abstract from my_con ...

  8. flash builder4.7bug

    flash builder4.7项目,swc中的button实例出来有bug,解决办法: 1,把button都改成movieclip. 2,改用swf做资源.

  9. 算法与数据结构实验题6.4 order (二叉树)

    1.题目: 2.代码: #include<iostream> #include<algorithm> using namespace std; struct Node { in ...

  10. WPF中设置快捷键

    方式1: 窗体中加入资源 <UserControl.Resources>        <RoutedUICommand x:Key="Cut" Text=&qu ...