• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1012
  • Last Modified:

How to get the windows service name within a windows service writting in C# derived from ServiceBase

Hi folks

I'm writing a windows service using VS2010 and C# 4.0. The idea is to have multiple windows services installed using the same executable. To identify the current instance, I need to figure out the current service name of the instance.

The template in VS2010 derives the service from
System.ServiceProcess.ServiceBase

Open in new window

. Who thinks that the property ServiceName in ServiceBase points to the according SCM service name, is wrong. ServiceName always contains the fixed string entered in the property dialog when creating the service. Removing it doesn't help either.

ServiceController.GetServices()

Open in new window

returns a list of all services (as a ServiceController object) on the machine. The ServiceController contains a property ServiceName as well, which contains also the name I'm looking for, but I don't know which ServiceController matches my service. Actually the name is exactly what I'm looking for. And if I have multiple services from the same executable, I cannot distinguishe them.

Does anybody have an idea how to get the name of the current service?

Thomy
0
Thomy-M
Asked:
Thomy-M
  • 2
  • 2
1 Solution
 
gsiricCommented:
You can use  process initialization arguments to identify type of service.
Startup arguments for service are obtained in OnStart method.

 See more at:
http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.onstart.aspx
0
 
Thomy-MAuthor Commented:
As far as I know args in OnStart gets the parameters specified in the start parameter textbox, but how can they be persisted? I install the service using InstallUtil.exe, I created a project installer that sets several parameters, e.g. service name.
0
 
gsiricCommented:
As you can see in documentation, to add parameters that will always be present you must edit ImagePath in registry for your service

 (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<service name>\ImagePath).

You can do this during installation of your service.

0
 
Thomy-MAuthor Commented:
This hint was what I needed, this seem so work. During installation the parameters have to be written in the ImagePath of the service reg key.

public override void Install(IDictionary stateSaver)
{
	base.Install(stateSaver);

	string serviceInstance = this.Context.Parameters["serviceInstance"] as string;
	string serviceRegistryPath = "System\\CurrentControlSet\\Services\\" + this.serviceInstaller.ServiceName;

	RegistryKey servicesKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(serviceRegistryPath, true);
	// Should never happen
	if (servicesKey == null)
		throw new InstallException("Service key not found in the registry [" + serviceRegistryPath + "]");

	string imagePath = (string)servicesKey.GetValue("ImagePath");
	// Should never happen
	if (imagePath == null)
		throw new InstallException("ImagePath not found in the registry [" + serviceRegistryPath + ".ImagePath]");

	imagePath += " -instance " + serviceInstance;
	servicesKey.SetValue("ImagePath", imagePath);
	servicesKey.Close();
}

Open in new window


See also http://www.mail-archive.com/dotnet@discuss.develop.com/msg02393.html

Another possibility to figure out the current service configuration is to compare the entries in ServiceController.GetServices() with the current ProcessID. For this, you need to lookup the service process PID using a ManagementObject.

private string getCurrentServiceName()
{
	Process prc = Process.GetCurrentProcess();
	List<ServiceController> dswServices = ServiceController.GetServices()
		.Where(s => s.ServiceName.StartsWith("myService_") && s.Status != ServiceControllerStatus.Stopped)
		.ToList();
	foreach (var s in dswServices)
	{
		ManagementObject service = new ManagementObject(@"Win32_service.Name='" + s.ServiceName + "'");
		object o = service.GetPropertyValue("ProcessId");
		int processId = (int)((UInt32)o);
		if (processId == prc.Id) return s.ServiceName;
	}
	return null;
}

Open in new window


0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now