Introduction
In this article you are going to learn about how to get the status of any Windows service and how to stop it or run it again, and also how to get any variable for appsettings.json using .Net Core Web API.
Tools
Visual Studio.
Steps
Open Visual Studio and create new .Net Core Web API Project as shown below.
Name the project as you want, I’m naming it as “WindowServicesConfigrator” and then click OK.
Select API from the templates.
Now we have to do our functionality, .Net Core 2.1 and further versions need to get nuget package to use default ServiceController functionality. So we will right click to to solution and go to manage nuget package
Click to browse and type “System.ServiceProcess.ServiceController” and then click install.
After installation you have to add Window Service name to appsettings.json. So type Window Service in the window search tab and a window will open. Right click to desired service and go to properties.
You will see its name there, copy this.
Now to your Project, open appsettings.json and add a variable “ServiceName”. You can use a string variable in your controller to work it as a name but I’m declaring it in appsettings.json just to show how to get values from here.
Now we have to write our functions, you can add new controller but currently for learning purposes, I’m using existing ValuesController. So in your controller first add the following name spaces.
C# Code
- using System.ServiceProcess;
- using Microsoft.Extensions.Configuration;
Now get the values from appsettings.json.
C# Code
- ServiceController sc;
- public string serviceName = "";
- IConfiguration configuration;
- public ValuesController(IConfiguration iConfig)
- {
- configuration = iConfig;
- serviceName = configuration.GetValue<string>("ServiceName");
- sc = new ServiceController(serviceName);
- }
This will get the name of service from appsettings.json. Now add the following API calls to perform other functionalities.
C# Code
- [HttpGet]
- public string GetStatus()
- {
- return sc.Status.ToString();
- }
-
- [HttpGet]
- public string StartService()
- {
- if (sc.Status.Equals(ServiceControllerStatus.Stopped) || sc.Status.Equals(ServiceControllerStatus.StopPending))
- {
-
- sc.Start();
- }
- else
- {
- return "Service Already Running.";
- }
-
-
- sc.Refresh();
-
- return sc.Status.ToString();
- }
-
- [HttpGet]
- public string StopService()
- {
- if (sc.Status.Equals(ServiceControllerStatus.Running) || sc.Status.Equals(ServiceControllerStatus.StartPending))
- {
-
- sc.Stop();
- }
- else
- {
- return "Service Already Stopped.";
- }
-
- sc.Refresh();
-
- return sc.Status.ToString();
- }
After all this run your project and type the following url after your end point “api/values/getstatus” and you will see the result in your browser.
For Starting and Stopping the service you can change the call to StartService or StopService. This will not work in some cases due to window restrictions do don’t forget to run Visual Studio as Administrator and then test these cases. Showing below the result of StartService.
Also you can verify this from the Window Services.
This article gave a brief introduction on how to Run or Stop any window services in ASP .NET Core API project. Hope you find something usefull to work with. Good Luck!