Files
ServiceDashBored/Endpoint/WindowsServiceEndpoint.cs
T
2025-10-11 09:42:00 -04:00

74 lines
1.6 KiB
C#

using System;
using System.ServiceProcess;
namespace Endpoint
{
/// <summary>
/// Endpoint for a Windows Service
/// </summary>
public class WindowsServiceEndpoint : IEndpoint
{
/// <summary>
/// Gets the service name
/// </summary>
public string Name { get; set; }
/// <summary>
/// Returns a string explaining details on the current status
/// </summary>
/// <returns>string with status message</returns>
public string StatusDescription { get; protected set; }
/// <summary>
/// Gets or sets the name of the windows service.
/// </summary>
/// <value>The name of the service.</value>
public string ServiceName { get; set; }
/// <summary>
/// Gets or sets the name of the machine the service runs on.
/// </summary>
/// <value>The name of the machine.</value>
public string MachineName { get; set; }
/// <summary>
/// Gets the current status result of the endpoint
/// </summary>
/// <returns>Status</returns>
public Status GetStatus()
{
ServiceController myservice;
try
{
if (!string.IsNullOrEmpty(MachineName))
myservice = new ServiceController(ServiceName, MachineName);
else
myservice = new ServiceController(ServiceName);
}
catch (Exception ex)
{
StatusDescription = ex.Message;
return Status.Unreachable;
}
try
{
switch (myservice.Status)
{
case ServiceControllerStatus.Running:
StatusDescription = "Running";
return Status.Up;
default:
StatusDescription = myservice.Status.ToString();
return Status.Error;
}
}
catch (Exception ex)
{
StatusDescription = ex.Message;
return Status.Error;
}
}
}
}