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



Homayoon TahvildariPosted Dec 10, 2022, 5:38 PM
It doesn't work when you deploy your api in iis