Get current Service and it's name

  • Thread starter Thread starter Benjamin Wegner
  • Start date Start date
B

Benjamin Wegner

Hello!

I have an helper assembly, which runs in different applications. In
each application I would like to determine the name of the current
application.
I want to know, how I can get the name of the current service instance.
I thougt it would be possible to get the current service using
something like System.ServiceProcess... Then I can use the Name or
DisplayName property.

Has anyone an idea?

Regards,
Benjamin
 
Hi Benjamin,

you get the current name via reflection.

Assembly.GetExecutingAssembly().GetName().Name

If your app refernce any other dll's don't
place the code there! It will return the
name of these assemblies. Place it within the
class which has the main method or any other
class which is part of the main app.

Cheers
Lars Behrmann

_________________
Nothing is impossible. UML is the key for all your problems.
AODL - Make your .net apps OpenOffice ready
http://aodl.sourceforge.net/
 
Thanks Lars,
but then I only get the name of the current assembly and this is not
the name of the service. We have different service instances using the
same assembly name with differen titles in the services list
(management console).

Therefor I need the title from the services list and not the name of
the assembly.

Regards,
Benjamin
 
Your service class inherits from System.ServiceProcess.ServiceBase which has
a ServiceName property:

protected override void OnStart(string[] args)
{
if (EventLog.Exists("WindowsService1"))
EventLog.CreateEventSource("WindowsService", "Application");
EventLog.WriteEntry("WindowsService1", "Name: " + this.ServiceName,
EventLogEntryType.Information);
}

HTH, Jakob.
 
thx Jakob,

as far as I understand your code snipet, this must be executed in the
servicebase class, but I want to know the service name within another
class within another assembly.

Regards,
Benjamin
 
Benjamin,
as far as I understand your code snipet, this must be executed in the
servicebase class, but I want to know the service name within another
class within another assembly.

Yes, my code must be run from within a class inherited from ServiceBase.
But if your code is run from within the same process as the service itself,
then it should be fairly easy for you to somehow pass the servicename to your
class? E.g. you could place the servicename in a static variable when the
service starts. This would allow any code running in the same process to get
the servicename.

Regards, Jakob.
 
Jakob,
thats possible. I thought it would be possible to access the name of
the service (from management console) from somewhere else than a static
variable.

As far as I know the servicename property of the servicebase class is
not the name of the service in the management console.

A dirty way could be to iterate over all services within the current
system and compare the path of the service exe and the current
assembly. If it equals, this must be 'me' and I can take the name of
the service ... (I never tried this)

Regards,
Benjamin
 
Hi Benjamin

I encounter exactly the same problem as yours: Need to retrieve service name
in management console and I still not find any way to do so. I would like to
ask have you got better ways to do so now?

Many Thanks!!!

Tom
 
Back
Top