Topshelf and quartz, topshelfquartz

Source: Internet
Author: User
Tags class manager

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 ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.