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 ...
随机推荐
- python类——黑板客老师课程学习
1.基本语法 class class_name(base_class): base_class是它继承的父类 class_var def methods(self,args): statements ...
- LINUX内核分析期末总结
韩玉琪 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.课程总结 1 ...
- Android菜鸟成长记3-activity类
Activity 一.什么是activity Activity 是用户接口程序,原则上它会提供给用户一个交互式的接口功能.它是 android 应用程序的基本功能单元.Activity 本身是没有界面 ...
- linq 延迟执行带来的困扰
有这样一个案例: var filteredResult = from f in orgFileList select f; ; i < WorkStatusFilters.ListWorkSta ...
- openLDAP
错误提示: D:\OpenLDAP>slapd -d 256 515a48ae OpenLDAP 2.4.34 Standalone LDAP Server (slapd)515a48af co ...
- Python基础(七)内置函数
今天来介绍一下Python解释器包含的一系列的内置函数,下面表格按字母顺序列出了内置函数: 下面就一一介绍一下内置函数的用法: 1.abs() 返回一个数值的绝对值,可以是整数或浮点数等. 1 2 3 ...
- mysql convert
SELECT id,boshidianshu,boshidianshu_shuzi,CONVERT(REPLACE(boshidianshu, '个', ''),SIGNED) aaa from lg ...
- machine learning----->有监督学习和无监督学习的区别
1.有监督学习和无监督学习的区别: 1.1概述: 有监督学习是知道变量值(数据集)和结果(已知结果/函数值),但是不知道函数样式(函数表达式)的情况下通过machine learning(ML)获得正 ...
- 【Tyvj1601】魔兽争霸(主席树,树套树)
题意:要求在N个数的序列中支持以下操作: 1:将第X个元素加上Y 2:询问当前K大值 n<=30000,m<=50000 思路:树状数组套主席树 Tyvj又炸了,还不知道对不对 ..12] ...
- 济南学习D2T1__折纸带
他[问题描述]一张长度为n的纸带,我们可以从左至右编号为0 −n(纸带最左端标号为0) .现在有m次操作,每次将纸带沿着某个位置进行折叠,问所有操作之后纸带的长度是多少.[输入格式]第一行两个数字n, ...