Topshelf and quartz, topshelfquartz
Reading directory:
After a sharing in the company last month, we have compiled the PPT and part of the communication content into a blog.
Introduction
Topshelf is a method for creating windows Services. Compared with native implementations of ServiceBase and Install. Installer, it is easier and more convenient. We only need a few lines of code to develop windows Services. Topshelf supports mono deployment and installation in windows and linux, and is also open-source.
Topshelf is easier to debug than native. You can directly debug it in the form of f5 on the console during development, and deploy it in the form of a service when releasing it. Another useful feature is the support for multi-instance deployment. This allows you to deploy multiple services on one machine. Similar Tools include instsrv and srvany.
Basic usage
Topshelf is easy to use and can be directly installed through nuget. The latest version is 3.2:
Sample Code:
HostFactory. run (x => {x. service <Manager> (s => {s. constructUsing (name => new Manager (); s. whenStarted (tc => tc. onStart (); s. whenStopped (tc => tc. onStop () ;}); x. runAsLocalSystem (); x. setDescription ("Topshelf test example"); x. setDisplayName ("TopshelfExample"); x. setServiceName ("TopshelfExample ");});
The callback Parameter Method of the Run method is executed at runtime, and some configuration information is contained in the method.
Here, the manager class is our service implementation class. when the service is started, WhenStarted calls the OnStart of the corresponding implementation, and the stop is the same.
RunAsLocalSystem () indicates running as a local system account. Optional options include Network Service and Local Service account.
SetDescription ("Topshelf test example"); sets the service description.
SetDisplayName ("TopshelfExample"); set the display name of the service.
X. SetServiceName ("TopshelfExample"); set the service name
Manager Service implementation class:
public class Manager { public void OnStart() { Console.WriteLine("Service start."); } public void OnStop() { Console.WriteLine("Service stopped."); } }
Debugging and Installation
Debugging is the same as normal, just press f5:
Run CMD as an administrator during installation to enter the program directory.
Enter the installation command: TopshelfExample.exe install topshelf is installed in the form of a transaction. Only the installation succeeds and fails, and the compatibility is good. Example:
The startup command is topshelfexample.exe startand topshelfexample.exe stop.
Optional
Topshelf is small but supports many configurable options. The following is an example:
- SetStartTimeout startup timeout
- SetStopTimeout stop timeout
- Before BeforeUninstall
- Callback after uninstalling AfterUninstall
- Callback after installing AfterInstall
- Callback after AfterRollback rollback
- DependsOnMsmq
- EnablePauseAndContinue can be paused.
- UseLog4Net (". config") integrated with log4Net
- UseNLog integrates Nlog
Multi-instance support and related information
Native services are not supported. topshelf supports deploying multiple identical program instances with different names. Start a new instance:
TopshelfExample.exe –instance “newinstallname” install
The start and top commands are in the same format as the original parameters, such as uninstalling:
TopshelfExample.exe –instance "TopshelfExample2" uninstall
One advantage of Multi-instance is disaster tolerance. When a service is deployed in multiple copies, if any of the service instances fails, the remaining instances can continue to be executed. Multiple instances can be in the master-slave mode, and are executed only when the master-slave service fails. It can also be implemented in the form of load balancing, multi-instance preemption process lock or distributed lock, who gets who executes.
Document Index
Github address
Topshelf command list
TopshelfExample.exe help
Quartz.net
For more information about quartz.net, post only links.
Net job Scheduling (5) -quartz.net dynamic addition job design
Net Job Scheduling (iv) -quartz.net persistence and Cluster
Net Job Scheduling (III)-Quartz. Net advanced
Net Job Scheduling (II)-CrystalQuartz Remote Management
Net Job Scheduling (1)-Quartz. Net entry
Topshelf and quartz.net can be used together to develop some scheduled task services conveniently.
PS: I quit my job last Friday ~