7

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?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Alican Uzun
  • 349
  • 1
  • 6
  • 20

3 Answers3

6

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

Community
  • 1
  • 1
Elshan
  • 7,339
  • 4
  • 71
  • 106
  • 2
    This solution would require Windows Authentication on your site. If that is okay, this is a decent and secure solution. – Patrick Hofman Apr 29 '16 at 10:52
  • how to implement windows authentication on site? can you explain this? – Alican Uzun Apr 29 '16 at 10:54
  • @AlicanUzun check this tutorial http://www.codeproject.com/KB/cs/svcmgr.aspx?display=Print – Elshan Apr 29 '16 at 10:57
  • @CodeCaster I am searching for 4 hours! – Alican Uzun Apr 29 '16 at 10:57
  • @AlicanUzun Did you try setting the application pool identity to an administrator? – Elshan Apr 29 '16 at 11:01
  • @Alican you learned about Windows Authentication on IIS ten minutes ago, so I'd be impressed if you searched for four hours already. – CodeCaster Apr 29 '16 at 11:02
  • @CodeCaster how did you learn that I learn about windows authentication seven minutes ago, it is now 8 mins by the way. please stop being rude and provide an answer or look for another questions. I have knowledge about windows authentication already but I couldn't implement it, the codes that I have tried to implement, didn't work and I am asking how! – Alican Uzun Apr 29 '16 at 11:05
  • @AlicanUzun Bellow WCF answer is better than mine. This example demonstrate WCF service tutorial http://msdn.microsoft.com/en-us/library/ms733069%28v=vs.110%29.aspx And convert that tutorial into control windows services and then access that service with your web application.This is windows service tutorial http://stackoverflow.com/a/16319055/1641556 – Elshan Apr 29 '16 at 11:06
2

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.

bob dawson
  • 91
  • 5
  • Yes, decoupling the two is the best solution. This is the best option security-wise. – Patrick Hofman Apr 29 '16 at 10:51
  • 1
    And depending on the intended audience, make sure to whitelist the services one can start or stop. – CodeCaster Apr 29 '16 at 10:56
  • 1
    @AlicanUzun This is good solution.This example demonstrate WCF service tutorial https://msdn.microsoft.com/en-us/library/ms733069%28v=vs.110%29.aspx And convert that tutorial into control windows services and then access that service with your web application – Elshan Apr 29 '16 at 11:05
-1

Follow below steps to start/stop window services:

1) To access windows service you need to add below namespace in your controller:

using System.ServiceProcess;

If you are getting error as “namespace name ‘ServiceController’ could not be found” while adding above namesapce you need to add using “Add Reference”. For more details see my blog HERE

2) Add below code to your controller:

ServiceController service = new ServiceController("Test Windows Service");
service.Start();

It will start your services. Source: http://sforsuresh.in/starting-windows-services-c-mvc-4/

Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90
  • thanks for the answer, but my problem was access denied exception with using System.ServiceProcess; and servicecontroller. – Alican Uzun Sep 04 '18 at 12:09
  • This is not a solution, you have to impersonate a user. Please write an answer you tried or you know it's working – Yaman Dec 12 '20 at 22:30