Background
Some time ago the company had a need (to export a batch of data to the business every day, in the form of Excel e-mail sent to him). A says: Write a service directly, Judge equals a certain time point, Execute SQL statement, generate Excel, write a emaihelper send to him not to have, this have what trouble? B said: I have a mother to come, also write service? You still need a timer to judge the time? How much effort ah, directly write a console program, add a task plan, do not get it done? I just want to say: You are the great God, every time do not add new things, but also write what code, how boring Ah, two days ago saw Topshelf+quartz.net this east, can do a practice ....
Purpose
use Topshelf+quartz.net to export Excel data as a Windows service
Dapper just too lazy to do database-related operations, this ORM can help me save a lot of work
Npoi, of course, is the generation of Excel, has been working with Npoi with Excel (whether to get Excel data, or to generate Excel files)
IOC, I'm using Autofact.
Introduction
All right, let's talk about this a little bit.
Topshelf Official website: http://topshelf-project.com/
GitHub Address: https://github.com/Topshelf/Topshelf/)
Topshelf Document: http://docs.topshelf-project.com/en/latest/configuration/quickstart.html
Topshelf is a way to create a Windows service that is simpler and easier than native implementations of ServiceBase and Install.installer, and we only need a few lines of code to enable the development of Windows services. The topshelf itself supports the deployment of Windows and Linux under Mono, which is also open source.
Topshelf relatively native, debugging is more convenient, can be developed in the form of a console directly F5 debugging, the release of the command as a service to deploy. A more useful feature is the ability to support multiple instances of deployment so that multiple relative services can be deployed on a single machine. Similar tools are available for instsrv and Srvany.
There are two ways to use Topshelf, the following code is from the official document recommended usage
1 Public classTowncrier2 {3 ReadOnlyTimer _timer;4 PublicTowncrier ()5 {6_timer =NewTimer ( +) {AutoReset =true};7_timer. Elapsed + = (sender, EventArgs) = Console.WriteLine ("It's {0} and all are well", DateTime.Now);8 }9 Public voidStart () {_timer. Start (); }Ten Public voidStop () {_timer. Stop (); } One } A - Public class Program - { the Public Static voidMain () - { -Hostfactory.run (x =//1 - { +X.service<towncrier> (s = =//2 - { +S.constructusing (name=>NewTowncrier ());//3 As.whenstarted (TC = TC). Start ());//4 ats.whenstopped (TC = TC). Stop ());//5 - }); -X.runaslocalsystem ();//6 - -X.setdescription ("Sample topshelf Host");//7 -X.setdisplayname ("Stuff");//8 inX.setservicename ("Stuff");//9 -});//Ten to } +}View Code
Effects such as:
Yes, a simple topshelf program is so simple, next, simply configure, that can be used when the service. Installation is convenient:
Installation: TopshelfDemo.exe Install start: TopshelfDemo.exe start uninstall: TopshelfDemo.exe Uninstall
After the installation is successful, we can see one more service in the service:
Finish Topshelf, then talk about Quartz.net
Topshelf+quartz.net+dapper+npoi (i)