How to start a Windows service on a Web page

Source: Internet
Author: User

As the company has many Windows services to deal with the business, so-called maintenance of the service is also a headache problem, because they do not know when the service automatically stopped, and the main reason is that the service is maintained by the Operations department in the maintenance of management, development side does not directly operate the service rights, So the use of web monitoring services is very important (in the service can also increase the e-mail alerts, such as service errors or broken off).

How to get all the services on your computer:

Use. NET Framework class library with its own component ServiceController can implement operations on Windows services.

1, first to add a reference System.ServiceProcess namespace in your project;

2, create an instance of ServiceController;

// get all non-device driver services on the current computer servicecontroller[] myservices = Servicecontroller.getservices ();

The GetServices method has an overload that gets the local service by default, and if you want to get a service on a machine, specify the machine name, provided that you have permission to get it.

Because it is to use the web to get all the services on the computer, I directly posted the background code of the Web page, as follows:

Protectedvoid Page_Load (Objectsender, EventArgs e) {servicecontroller[] myservices =Servicecontroller.getservices (); List =New list<servicemodel>();foreach (var item in MyServices) {if (item. ServiceType == servicetype.win32ownprocess) {ServiceModel model = new ServiceModel (); model. ServiceName = item. ServiceName; Model. DisplayName = item. DisplayName; if (item. Status == servicecontrollerstatus.running) model. isrunning = trueelse model. isrunning = false

Where ServiceModel is a new entity class that I created to hold information about the service. The Prerequisites page section reads as follows:

Depending on the current state of the service, you can do the following: Stop, start, restart, and so on. Since I am based on the operation of the click, to the general handler for processing, so directly on the code, very simple:

PublicvoidProcessRequest (HttpContext context) {context. Response.ContentType ="Text/plain";//Service NameString serviceName = context. request.querystring["ServiceName"];//Operation type "Restart, stop, restart"String type = context. request.querystring["Type"];Try{Switch(type) {Case"Start": StartService (ServiceName);Break;Case"Stop": StopService (ServiceName);Break;Case"Reset": Resetservice (ServiceName);Break;Default: Resetservice (ServiceName);Break; } context. Response.Write ("Ok"); }Catch(Exception ex) {context. Response.Write (ex. Message); } }///<summary>///Start the service///</summary>///<param name= "ServiceName" >Service Name</param>Privatevoid StartService (StringServiceName) {ServiceController service =NewServiceController (ServiceName);if (service. Status = =servicecontrollerstatus.stopped) {service. Start (); Service. WaitForStatus (servicecontrollerstatus.running); Service. Close (); } }///<summary>///Stop Service///</summary>///<param name= "ServiceName" >Service Name</param>Privatevoid StopService (StringServiceName) {ServiceController service =NewServiceController (ServiceName);if (service. Status = =servicecontrollerstatus.running) {service. Stop (); Service. WaitForStatus (servicecontrollerstatus.stopped); Service. Close (); } }///<summary>///Restart Service///</summary> / <param name= "ServiceName" > service name </param> private void Resetservice (string ServiceName) {ServiceController service = new< Span style= "color: #000000;" > ServiceController (serviceName); if (service. Status = = Servicecontrollerstatus.running | | Service. Status == servicecontrollerstatus.stopped) {service. Stop (); Service. WaitForStatus (servicecontrollerstatus.stopped); Service. Start (); Service. WaitForStatus (servicecontrollerstatus.running); Service. Close (); } }

The code is very simple, a look simple and clear, hehe ...

Development considerations:

1, if using the VS debugging program to stop, start and other operations, reported

Workaround: Turn off vs and run the program as an administrator to resolve the issue

2, when the Web page is deployed to IIS for access, you may be prompted to do not have permission to operate, and other reasons:

First of all, the installation of IIS is introduced, it is estimated that everyone has no problem with this

Control Panel---> Programs and features---> Enable or disable Windows features--->internet information services, such as:

Because I use WIN8, the first I only checked the first level of directory, click Install, the results when running the IIS Web site, always prompt 403 error, and later found that IIS is not installed completely problem, please follow the check.

After the IIS installation is complete, the Web site can run, but in the operation of the service when prompted for insufficient reasons, please set the following in IIS:

Here is the DefaultAppPool program pool, right-click the program pool--Advanced settings as follows:

Logo Here Select the built-in account as: LocalSystem, or choose a custom account, create a new authorized account can also.

Run the website again, stop the service, start the operation, and find everything is OK.

The above is today in the Web page on the operation of the service said the problems encountered, hoping to help people with the same needs.

How to start a Windows service on a Web page

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.