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

3

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?

Anglian answered 29/4, 2016 at 10:39 Comment(4)
To control services, you must run with elevated permissions. A web app doesn't do that by default.Waterline
how can I get these permission?Anglian
If i recall it was by setting the app pool identity to system (or a better-planned user level). IHypothermia
And for all the people suggesting to run the application pool as an user with administrative privileges: don't do that.Gilburt
K
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://mcmap.net/q/1622902/-start-stop-window-service-from-asp-net-page

Kwei answered 29/4, 2016 at 10:51 Comment(8)
This solution would require Windows Authentication on your site. If that is okay, this is a decent and secure solution.Rake
how to implement windows authentication on site? can you explain this?Anglian
@AlicanUzun check this tutorial codeproject.com/KB/cs/svcmgr.aspx?display=PrintKwei
@Gilburt I am searching for 4 hours!Anglian
@AlicanUzun Did you try setting the application pool identity to an administrator?Kwei
@Alican you learned about Windows Authentication on IIS ten minutes ago, so I'd be impressed if you searched for four hours already.Gilburt
@Gilburt 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!Anglian
@AlicanUzun Bellow WCF answer is better than mine. This example demonstrate WCF service tutorial 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 https://mcmap.net/q/519645/-how-to-stop-windows-service-programmaticallyKwei
T
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.

Thera answered 29/4, 2016 at 10:47 Comment(3)
Yes, decoupling the two is the best solution. This is the best option security-wise.Rake
And depending on the intended audience, make sure to whitelist the services one can start or stop.Gilburt
@AlicanUzun This is good solution.This example demonstrate WCF service tutorial 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 applicationKwei
F
-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/

Figurehead answered 4/9, 2018 at 11:17 Comment(2)
thanks for the answer, but my problem was access denied exception with using System.ServiceProcess; and servicecontroller.Anglian
This is not a solution, you have to impersonate a user. Please write an answer you tried or you know it's workingInartistic

© 2022 - 2024 — McMap. All rights reserved.