Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop or start Windows Service in C# mvc?

I tried System.ServiceProcess.ServiceController System.Diagnostics.Process;

to control windows services in my web form. With System.ServiceProcess.ServiceController I am getting Access Denied Exception.

With System.Diagnostics.Process I get nothing. How can I start/stop Windows Services with my web form, any idea?

like image 407
Alican Uzun Avatar asked Apr 29 '16 10:04

Alican Uzun


People also ask

How do I start a service in C#?

Create the Windows Service, Open Visual Studio, from the File Menu, select New and then choose Project. It will open a New Project window where you can choose various type of the project. Go to Visual C# and select Classic Desktop and from the next window, choose Windows Service.


2 Answers

Here are the important points to accomplish:

1 You need to add System.ServiceProcess reference in your application. This namespace holds ServiceController Class to access the window service.

2 You need to check the status of the window services before you explicitly start or stop it.

3 By default, IIS application runs under ASP.NET account which doesn't have access rights permission to window service. So, Very Important part of the solution is: Impersonation. You need to impersonate the application/part of the code with the User Credentials which is having proper rights and permission to access the window service.

Refer this blog entry from asp.net And also look at this https://stackoverflow.com/a/17497084/1641556

like image 127
Elshan Avatar answered Sep 28 '22 10:09

Elshan


Knelis is correct, I think you just need another process to do that. Maybe your own windows service, which run under local system, and provide a wcf service to control windows service, and your web app can call the wcf service.

like image 42
bob dawson Avatar answered Sep 28 '22 09:09

bob dawson