星空网 > 软件开发 > ASP.net

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有两种使用方式,下面代码来自官方文档推荐用法

Topshelf+Quartz.net+Dapper+Npoi(一)Topshelf+Quartz.net+Dapper+Npoi(一)
 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 Program14 {15   public static void Main()16   {17     HostFactory.Run(x =>                 //118     {19       x.Service<TownCrier>(s =>            //220       {21        s.ConstructUsing(name=> new TownCrier());   //322        s.WhenStarted(tc => tc.Start());       //423        s.WhenStopped(tc => tc.Stop());        //524       });25       x.RunAsLocalSystem();              //626 27       x.SetDescription("Sample Topshelf Host");    //728       x.SetDisplayName("Stuff");            //829       x.SetServiceName("Stuff");            //930     });                         //1031   }32 }

View Code

    效果如下图:

Topshelf+Quartz.net+Dapper+Npoi(一)

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

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



Topshelf+Quartz.net+Dapper+Npoi(一)

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

Topshelf+Quartz.net+Dapper+Npoi(一)

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

 

  

 

 

 

      




原标题:Topshelf+Quartz.net+Dapper+Npoi(一)

关键词:.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流