zoukankan      html  css  js  c++  java
  • Topshelf+Quartz.net+Dapper+Npoi(一)

      背景 


       前段时间公司有个需求(每天给业务导出一批数据,以excel的形式通过邮件发送给他)。A说:直接写个服务,判断等于某个时间点,执行一下sql语句,生成excel,写个EmaiHelper发送给他不就得了,这有什么麻烦的?B说:我了个亲娘来,还写服务呢?你还需要搞个timer去判断时间点?多费劲啊,直接写个控制台程序,添加个任务计划,不就搞定了吗?我只想说:你们都是大神,每次都不加点新的东西,还写什么代码,多么没劲啊,前两天看到了topshelf+quartz.net这个东东,可以做个练习了。。。。

        目的 


       使用topshelf+quartz.net以windows服务形式来导出excel数据

        dapper只是懒得进行数据库相关操作,这个orm可以帮我省下不少工作

        npoi当然是生成excel的了,一直在用npoi跟excel打交道(不管获取excel数据,还是生成excel文件)

          ioc我使用的是autofact

       介绍 


           好了,接下来大体说一下。

       topshelf官方网站:http://topshelf-project.com/     

       github地址:https://github.com/Topshelf/Topshelf/ 

       topshelf文档:http://docs.topshelf-project.com/en/latest/configuration/quickstart.html

       topshelf是创建windows服务的一种方式,相比原生实现ServiceBase、Install.Installer更为简单方便, 我们只需要几行代码即可实现windows服务的开发。topshelf本身支持windows及linux下mono上部署安装,同样也是开源的。

       topshelf相对原生来说,调试起来比较方便,可以在开发时以控制台的形式直接f5调试,发布时用命令以服务的形式部署。还一个比较有用的特性是支持多实例的部署,这样可以在一台机器上部署多个相对的服务。类似的工具有instsrv和srvany。

      topshelf有两种使用方式,下面代码来自官方文档推荐用法

     1 public class TownCrier
     2 {
     3     readonly Timer _timer;
     4     public TownCrier()
     5     {
     6         _timer = new Timer(1000) {AutoReset = true};
     7         _timer.Elapsed += (sender, eventArgs) => Console.WriteLine("It is {0} and all is well", DateTime.Now);
     8     }
     9     public void Start() { _timer.Start(); }
    10     public void Stop() { _timer.Stop(); }
    11 }
    12 
    13 public class Program
    14 {
    15     public static void Main()
    16     {
    17         HostFactory.Run(x =>                                 //1
    18         {
    19             x.Service<TownCrier>(s =>                        //2
    20             {
    21                s.ConstructUsing(name=> new TownCrier());     //3
    22                s.WhenStarted(tc => tc.Start());              //4
    23                s.WhenStopped(tc => tc.Stop());               //5
    24             });
    25             x.RunAsLocalSystem();                            //6
    26 
    27             x.SetDescription("Sample Topshelf Host");        //7
    28             x.SetDisplayName("Stuff");                       //8
    29             x.SetServiceName("Stuff");                       //9
    30         });                                                  //10
    31     }
    32 }
    View Code

        效果如下图:

    没错,一个简单的topshelf程序就是这么简单,接下来,只需要简单配置一下,即可以当服务来使用了。安装很方便:

    安装:TopshelfDemo.exe install
    启动:TopshelfDemo.exe start
    卸载:TopshelfDemo.exe uninstall

    安装成功后,接下来,我们就可以看到服务里多了一个服务:

    说完topshelf,接下来说说quartz.net

      

          

  • 相关阅读:
    redis集群报Jedis does not support password protected Redis Cluster configurations异常解决办法
    redis集群密码设置
    Redis 3.2.4集群实战
    Redis3.2.4 Cluster集群搭建
    redis集群环境的搭建和错误分析
    Centos iptables防火墙关闭启动详解
    动态的表格数据
    ubuntu使用抓包工具,charles
    layer结合art实现弹出功能
    禅道开源版源码安装
  • 原文地址:https://www.cnblogs.com/jellyzhang/p/5742844.html
Copyright © 2011-2022 走看看