C #. NET 0 command line to install the Windows service program,

Source: Internet
Author: User

C #. NET 0 command line to install the Windows service program,

Design Principle: all objects

Background: In my project, I need to connect to the hardware through Socket and provide the Wcf Service operation interface to the App, but it is a console (although I like the console very much, because it is easy to use), put it on the server to run, there is always a dark window, always feeling indecent (Forgive my obsessive-compulsive disorder ). As a result, Baidu and Google can create and run WIndows service programs, just like SQL server data.

However, many of them are so troublesome and need batch processing or something. I have always liked conciseness, So I designed such a Windows Service helper class. That's right, it's a 0 command.

Body:

1. I will not release the source code of the class library, and I will put it on GitHub.

2. Usage:

Using Coldairarrow. util. windowsService; using System; namespace WindowsServiceTest {class Program {public static void Main (string [] argc) {// create a service container. The first parameter is the specified service name, the second parameter is the main function entry parameter argc WindowsServiceContainer serviceContainer = new WindowsServiceContainer ("A_Test_Service", argc); // events executed when the service is started, the Main function of the console can be regarded as serviceContainer. handleOnStart = new Action <string []> (args = >{// you can add the services you need here, such as creating a Socket Message, Wcf Service, balabala ~~~~~~~~~~ }); // ServiceContainer for processing logs. handleLog = new Action <string> (log => {Console. writeLine (log) ;}); // handle the abnormal event serviceContainer. handleException = new Action <Exception> (ex => {Console. writeLine (ex. message) ;}); // start to run serviceContainer. start ();}}}

Run the console directly:

Select 1 to install the service:

Yes, you can see that the service has been successfully run !!!, Is it a fake?

 

Select 2 to uninstall the service:

 

Summary:

By encapsulating a series of service operations, it is very easy to convert the console program into a service program. Without any batch processing commands, you only need a few simple lines of code, I feel very good. Please like it!

Old rule, GitHub address:

Https://github.com/Coldairarrow/WindowsServiceDemo

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.