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