Configuring Window Services Using ASP.NET Core WebAPI And Getting Values From AppSettings

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.
 
Configuring Window Services Using ASP.NET Core WebAPI And Getting Values From AppSettings 
 
Name the project as you want, I’m naming it as “WindowServicesConfigrator” and then click OK.
 
Configuring Window Services Using ASP.NET Core WebAPI And Getting Values From AppSettings 
 
Select API from the templates.
 
Configuring Window Services Using ASP.NET Core WebAPI And Getting Values From AppSettings 
 
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
 
Configuring Window Services Using ASP.NET Core WebAPI And Getting Values From AppSettings 
 
Click to browse and type “System.ServiceProcess.ServiceController” and then click install.
 
Configuring Window Services Using ASP.NET Core WebAPI And Getting Values From AppSettings 
 
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.
 
Configuring Window Services Using ASP.NET Core WebAPI And Getting Values From AppSettings 
 
You will see its name there, copy this.
 
Configuring Window Services Using ASP.NET Core WebAPI And Getting Values From AppSettings 
 
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.
 
Configuring Window Services Using ASP.NET Core WebAPI And Getting Values From AppSettings 
 
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
  1. using System.ServiceProcess;  
  2. using Microsoft.Extensions.Configuration;  
Now get the values from appsettings.json.
 
C# Code
  1. ServiceController sc;  
  2. public string serviceName = "";  
  3.         IConfiguration configuration;  
  4.         public ValuesController(IConfiguration iConfig)  
  5.         {  
  6.             configuration = iConfig;  
  7.             serviceName = configuration.GetValue<string>("ServiceName");  
  8. sc = new ServiceController(serviceName);  
  9.         }  
This will get the name of service from appsettings.json. Now add the following API calls to perform other functionalities.
 
C# Code
  1. [HttpGet]  
  2.  public string GetStatus()  
  3.  {  
  4.      return sc.Status.ToString();  
  5.  }  
  6.   
  7.  [HttpGet]  
  8.  public string StartService()  
  9.  {  
  10.      if (sc.Status.Equals(ServiceControllerStatus.Stopped) || sc.Status.Equals(ServiceControllerStatus.StopPending))  
  11.      {  
  12.          // Start the service if the current status is stopped.  
  13.          sc.Start();  
  14.      }  
  15.      else  
  16.      {  
  17.          return "Service Already Running.";  
  18.      }  
  19.   
  20.      // Refresh and display the current service status.  
  21.      sc.Refresh();  
  22.   
  23.      return sc.Status.ToString();  
  24.  }  
  25.   
  26.  [HttpGet]  
  27.  public string StopService()  
  28.  {  
  29.      if (sc.Status.Equals(ServiceControllerStatus.Running) || sc.Status.Equals(ServiceControllerStatus.StartPending))  
  30.      {  
  31.          // Stop the service if its status is not set to "Stopped".  
  32.          sc.Stop();  
  33.      }  
  34.      else  
  35.      {  
  36.          return "Service Already Stopped.";  
  37.      }  
  38.   
  39.      sc.Refresh();  
  40.   
  41.      return sc.Status.ToString();  
  42.  }  
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.
 
Configuring Window Services Using ASP.NET Core WebAPI And Getting Values From AppSettings
 
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.
 
Configuring Window Services Using ASP.NET Core WebAPI And Getting Values From AppSettings
 
Also you can verify this from the Window Services.
 
Configuring Window Services Using ASP.NET Core WebAPI And Getting Values From AppSettings 
 
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!