Topshelf入门
简介
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入门的更多相关文章
- topshelf和quartz内部分享
阅读目录: 介绍 基础用法 调试及安装 可选配置 多实例支持及相关资料 quartz.net 上月在公司内部的一次分享,现把PPT及部分交流内容整理成博客. 介绍 topshelf是创建windows ...
- topshelf和quartz
topshelf和quartz内部分享 阅读目录: 介绍 基础用法 调试及安装 可选配置 多实例支持及相关资料 quartz.net 上月在公司内部的一次分享,现把PPT及部分交流内容整理成博客. 介 ...
- 参照示例搭建一个Quertz + Topshelf的一个作业调度服务(基础)
学习网址:Quartz.NET 入门.使用Topshelf创建Windows服务 来自七七资料 1.直接下载源码 2.配置完成后,安装服务测试应用. 以下是遇到情况和加入的一些内容 1.在进行服务安装 ...
- C# Windows服务开发从入门到精通
一.课程介绍 大家都知道如果想要程序一直运行在windows服务器上,最好是把程序写成windows服务程序:这样程序会随着系统的自动启动而启动,自动关闭而关闭,不需要用户直接登录,直接开机就可以启动 ...
- topshelf 开发windows 服务资料
官方配置 http://docs.topshelf-project.com/en/latest/configuration/config_api.html#service-start-modes to ...
- Topshelf 和 Katana:统一的 Web 和服务体系结构
Topshelf 和 Katana:统一的 Web 和服务体系结构 Wes McClure 下载代码示例 使用 IIS 托管 ASP.NET Web 应用程序已成为业界标准十年有余.构建此类应用程序的 ...
- 关于使用Topshelf创建服务
目录 0. 背景说明 1. 使用Topshelf组件创建Windows服务 1.1 依赖Quartz.net实现定时任务 1.2 依赖于Topshelf创建服务类 1.3 log4net的配置文件lo ...
- Angular2入门系列教程7-HTTP(一)-使用Angular2自带的http进行网络请求
上一篇:Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数 感觉这篇不是很好写,因为涉及到网络请求,如果采用真实的网络请求,这个例子大家拿到手估计还要自己写一个web ...
- ABP入门系列(1)——学习Abp框架之实操演练
作为.Net工地搬砖长工一名,一直致力于挖坑(Bug)填坑(Debug),但技术却不见长进.也曾热情于新技术的学习,憧憬过成为技术大拿.从前端到后端,从bootstrap到javascript,从py ...
随机推荐
- Bootstrap修改input file默认样式
html部分 <div class="form-group"> <label class="col-sm-3 control-label"&g ...
- 多台web如何共享session进行存储(转载)
session的存储了解以前是怎么做的,搞清楚了来龙去脉,才会明白进行共享背后的思想和出发点.我喜欢按照这样的方式来问(或者去搞清楚):为什么要session要进行共享,不共享会什么问题呢? php中 ...
- (转)javascript中的对象查找
本文转自:http://otakustay.com/object-lookup-in-javascript/ ---很棒的一篇文章,作者的其他文章还暂时没读,但相信作者是一个谦虚 谨慎的好工程师 近 ...
- JStorm注意事项
storm.yaml的配置可以参照defaults.yaml,例如: nimbus.childopts: " -Xms256m -Xmx256m -XX:+UseConcMarkSweepG ...
- {CSDN}{英雄会}{砍树、石子游戏}
砍树 思路: 可以将题目意图转化为:给定一棵树,求其中最接近总权值一半的子树. DFS求每个节点的所有子节点的权值和,遍历每个节点,最接近总权值一半的即为答案.复杂度O(N). 石子游戏: 思路: 一 ...
- visual studio installer制作安装包——Installer 类
链接:https://msdn.microsoft.com/zh-cn/library/system.configuration.install.installer.aspx Installer 类 ...
- linux crontab命令参数及用法详解--linux自动化定时任务cron
声明:本文转自Linux 安全网,在此基础上加上自己的体会! crontab 命令 如果发现您的系统里没有这个命令,在ubuntu server 中用的是 sudo apt-get install c ...
- nested exception is com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1044 > 1024
HTTP Status 500 - type Exception report message description The server encountered an internal error ...
- ubuntu下安装迅雷
----------------------------------2016-03-28新增适用于ubuntu16.04系列的安装包---------------------------------- ...
- as 和 is 区别
as 将对象转换某类型,如果失败则返回null.is用来判断对象是否为某个类型,在判断过程中会对对象进行两次转换,而as只有一次转换,故as效率高.