using System;
using System.ServiceProcess;
namespace Endpoint
{
///
/// Endpoint for a Windows Service
///
public class WindowsServiceEndpoint : IEndpoint
{
///
/// Gets the service name
///
public string Name { get; set; }
///
/// Returns a string explaining details on the current status
///
/// string with status message
public string StatusDescription { get; protected set; }
///
/// Gets or sets the name of the windows service.
///
/// The name of the service.
public string ServiceName { get; set; }
///
/// Gets or sets the name of the machine the service runs on.
///
/// The name of the machine.
public string MachineName { get; set; }
///
/// Gets the current status result of the endpoint
///
/// Status
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;
}
}
}
}