简介

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. javascript中的innerHTML是什么意思,怎么个用法?

    innerHTML在JS是双向功能:获取对象的内容 或 向对象插入内容:如:<div id="aa">这是内容</div> ,我们可以通过 document ...

  2. myeclipse中Web App Libraries无法自动识别lib下的jar包

    在项目目录下找到.object文件修改 <natures> <nature>org.eclipse.jem.workbench.JavaEMFNature</nature ...

  3. oracle 同时更新(update)多个字段多个值

    --创建表A,B: create table A (a1 varchar2(33),a2 varchar2(33),a3 varchar2(33)); create table B (b1 varch ...

  4. golang--gopher北京大会(1)

    大会感想:牛人真的很能写代码,实现很多功能,而且开源的精品越多,影响力越大,越能过上dream life.比如beego的作者,去了America,进入了Apple.另外,精英们特点是表达能力很强,也 ...

  5. ZERO 笔试

    1.大多考到了 计算机网络 tcpip  和  操作系统 多线程的知识  直接 懵逼 2.  考到了 递归的全排列 #include<iostream> using namespace s ...

  6. LeetCode.4 两个有序数组的中位数问题

    这道题是那种典型的有显而易见的解法, 但是想要达到较优的时间复杂度的话就不是这么好做的题目. 我来说说我自己的思考过程 : 首先最先想到的是 O(m + n) 的解法, 也就是利用归并排序的归并将两个 ...

  7. php 图片处理类

    <?php /** * 图片类 * @author <420012223@qq.cn> */ class Image { public $uploadImagePath = './t ...

  8. Position a child div relative to parent container in CSS: [设置 子DIV位置 跟 父DIV相关联]

    最近调DIV的位置比较头疼,各种position: relative / absolute google到一篇好文章[http://www.webdevdoor.com/html-css/css-po ...

  9. 0731am视图 模型

    跨控制器调用方法 function DiaoYong(){ 造对象$sc = new \Home\Controller\GoodsController();echo $sc->aa(); 如果在 ...

  10. SpringMVC+MyBatis项目分析与开发实例

    一.用到的Jar包 <!-- ************ Spring测试包 ************* --><artifactId>spring-test</artif ...