目前很多项目都是B/S架构的,我们经常会用到webapi、MVC等框架,实际项目中可能不仅仅是一些数据的增删改查,需要对数据进行计算,但是将计算逻辑放到api层又会拖累整个项目的运行速度,从而会写一些计划任务(使用quartz.net,在上一篇中已经提到)将需要计算的数据提前计算并入库,理论上api层需要做的只是增删改查,从而加快程序运行速度。

传统windows 服务需要创建一个windows service 项目,我自己没有用过,但是看过别人的整个开发流程,感觉及其繁杂并且不容易调试。

使用topshelf可以很快的创建一个服务,而我们需要做的只是创建一个控制台程序,调试成本也大大降低。

步骤如下:

  1. 首先还是通过nuget添加topshelf程序集
  2. 添加ServiceRunner类,这里用Quartz做例子。当然不仅仅适用于次,也可以用于wcf等。

    using Quartz;
    using Quartz.Impl;
    using Topshelf; namespace KfqPointService
    {
    public sealed class ServiceRunner : ServiceControl, ServiceSuspend
    {
    private readonly IScheduler scheduler; public ServiceRunner()
    {
    scheduler = StdSchedulerFactory.GetDefaultScheduler();
    } public bool Start(HostControl hostControl)
    {
    scheduler.Start();
    return true;
    } public bool Stop(HostControl hostControl)
    {
    scheduler.Shutdown(false);
    return true;
    } public bool Continue(HostControl hostControl)
    {
    scheduler.ResumeAll();
    return true;
    } public bool Pause(HostControl hostControl)
    {
    scheduler.PauseAll();
    return true;
    }
    }
    }
  3. 在程序入口中添加代码

    using System.Configuration;
    using KfqPointService;
    using Topshelf; namespace LfsZqHbInterface
    {
        class Program
        {
            static void Main(string[] args)
            {
                //LogWriter.Debug("Begin Service");
                HostFactory.Run(x =>
                {
                    x.Service<ServiceRunner>();
                    x.SetDescription(ConfigurationManager.AppSettings["Description"]);
                    x.SetDisplayName(ConfigurationManager.AppSettings["DisplayName"]);
                    x.SetServiceName(ConfigurationManager.AppSettings["ServiceName"]);
                    x.EnablePauseAndContinue();
                });
            }
        }
    }
  4. 将Windows服务安装到服务器上

(1)依次打开服务文件夹以管理员权限运行powershell,并进入服务所在文件夹。

(2)输入.\文件名.exe install 回车安装此服务,如下图所示

出现此场景为安装成功,此时大功告成,打开服务面板就可看到我们刚刚安装的服务。

通过TopShelf简单创建windows service的更多相关文章

  1. C#创建Windows Service(Windows 服务)基础教程

    Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...

  2. .net core+topshelf+quartz创建windows定时任务服务

    .net core+topshelf+quartz创建windows定时任务服务 准备工作 创建.net core 控制台应用程序,这里不做过多介绍 添加TopShelf包:TopShelf: 添加Q ...

  3. 创建Windows Service

    基本参照使用C#创建Windows服务,添加了部分内容 目录 创建Windows Service 可视化管理Windows Service 调试 示例代码 创建Windows Service 选择C# ...

  4. C# 创建Windows Service(Windows服务)程序

    本文介绍了如何用C#创建.安装.启动.监控.卸载简单的Windows Service 的内容步骤和注意事项. 一.创建一个Windows Service 1)创建Windows Service项目 2 ...

  5. .NET 6学习笔记(2)——通过Worker Service创建Windows Service

    通过Visual Studio中的Windows Service模板,我么可以创建.NET Framework版本的Windows Service,网络上对此已有详细且丰富的各路教程.但在我们升级到. ...

  6. Windows Service 学习系列(一):建立简单的Windows service

    参考:https://www.cnblogs.com/cncc/p/7170951.html 一.开发环境 操作系统:Windows 7 X64 开发环境:VS2017 编程语言:C# .NET版本: ...

  7. C# 创建Windows Service

    当我们需要一个程序长期运行,但是不需要界面显示时可以考虑使用Windows Service来实现.这篇博客将简单介绍一下如何创建一个Windows Service,安装/卸载Windows Servi ...

  8. 目前.NET Core创建Windows Service比较好的一个开源框架:DasMulli.Win32.ServiceUtils

    新建一个.NET Core控制台程序,搜索并下载Nuget包:DasMulli.Win32.ServiceUtils GitHub 链接及使用指南 Write a windows service us ...

  9. 使用.net 创建windows service

    最近公司项目需要,写了个windows 服务,windows 服务的内容可以在VS 中新建一个"windows服务项目", (1)服务中的主要代码: public partial ...

随机推荐

  1. idea启动项目连接mysql数据库后台报duplicate name异常

    自己写的sql语句在MySQL数据库中运行是没有问题的 但是在使用idea启动项目的时候,后台在运行这行sql语句的时候居然报错了,duplicate name:重复的名字,最后自己经过思考,修改了一 ...

  2. 浏览器渲染详细过程:重绘、重排和 composite 只是冰山一角

    https://juejin.im/entry/590801780ce46300617c89b8 渲染 这张很经典的图许多人都看过,其中的概念大家应该都很熟悉,也就是这么几个步骤:js修改dom结构或 ...

  3. scala 型变

    型变是复杂类型的子类型关系与其组件类型的子类型关系的相关性. Scala支持 泛型类 的类型参数的型变注释,允许它们是协变的,逆变的,或在没有使用注释的情况下是不变的. 在类型系统中使用型变允许我们在 ...

  4. Codechef July Challenge 2019 Hit the Coconuts

    假设现在有一堆数,我想要保证能取出一个,至少需要敲 (数的个数)*(这些数里的最小值)那么把这些数从大到小排序,$dp[i][j]$ 表示前 $i$ 个里面保证能取出 $j$ 个需要敲的次数.$dp[ ...

  5. Python列表生成式练习

    ''' 如果list中既包含字符串,又包含整数,由于非字符串类型没有lower()方法,所以列表生成式会报错 使用内建的isinstance函数可以判断一个变量是不是字符串: 返回True 或 Fal ...

  6. 计蒜客 41387.XKC's basketball team-线段树(区间查找大于等于x的最靠右的位置) (The Preliminary Contest for ICPC Asia Xuzhou 2019 E.) 2019年徐州网络赛

    XKC's basketball team XKC , the captain of the basketball team , is directing a train of nn team mem ...

  7. ARC098F Donation

    传送门 Atcoder Solution 首先是几个引理: 重新定义权值\(val_i=max(a_i-b_i,0)\),那么通过这个点必须需要\(val_i+b_i\)的钱. 多次经过一个点一定是在 ...

  8. java基础之 clone

    参考文档:深拷贝&浅拷贝:http://blog.csdn.net/cws1214/article/details/52193341 克隆的分类:  (1)浅克隆(shallow clone) ...

  9. 经典算法(四) 数组相关 & 螺旋矩阵 & 数字大小写转换 & 字符串相关

    一.求所有子数组的和的最大值 public static void main(String[] args) { int[] a = { 1, -2, 3, 10, -4, 7, 2, -5 }; Fi ...

  10. 【Beta阶段】第十次Scrum Meeting

    每日任务内容 队员 昨日完成任务 明日要完成的任务 张圆宁 #69 后端文档https://github.com/rRetr0Git/rateMyCourse/issues/69 网站测试.推广 牛宇 ...