Looking at the documentation it seems like Windows uses it in 2 scenarios:
The ServiceBase.ServiceName needs to be the same name as when it is installed, however when starting my service I am able to call ServiceBase.Run() without specifying the service name or specifying a different service name altogether and my application still starts correctly. I am using a separate WiX project to install my service and define the service name there depending on some TRANSFORMS.
Windows uses the ServiceBase.ServiceName to specify the EventLog.Source. I am successfully able to use Log4Net's EventLogAppender to log to the EventLog, manually specifying the applicationName in my log4net configs.
I want to make sure that I don't run into any repercussions down the road in the case that I don't specify the ServiceName correctly, however I am currently able to hit all my typical use cases as is. After calling ServiceBase.Run() I am able to use System.Management to determine my service name in case of any additional needs.
My main concern with avoiding setting the service name here is because my MSI installer can install different instances of my exe as different services via TRANSFORMs I create a sort of chicken-and-egg problem where I can't call GetServiceName() without calling ServiceBase.Run(), but I can't call ServiceBase.Run() without defining the ServiceBase.ServiceName.
Some example code of what I am running:
public aync Task<int> RunAsync()
{
var serviceToRun = new ServiceBase{/*ServiceName = "Avoiding.."*/};
var runServiceTask = Task.Run(() => ServiceBase.Run(serviceToRun));
logger.Warn($"ServiceName : '{GetServiceName()}'");
logger.Warn($"Service ShortName : '{serviceToRun.ServiceName}'");
await runServiceTask.ConfigureAwait(false);
return serviceToRun.ExitCode;
}
public string GetServiceName()
{
var processId = Process.GetCurrentProcess().Id;
var query = $"SELECT * FROM Win32_Service where ProcessId = {processId}";
var managementObject = new ManagementObjectSearcher(query).Get().Cast<ManagementObject>().FirstOrDefault();
if (managementObject == null)
{
throw new Exception("Could not get service name");
}
var serviceName = managementObject["Name"].ToString();
return serviceName;
}