Am in a Windows Service or in a Application?

G

Guest

I have a c# component that can be used in various executables. I need a way
in the code of this component to determine if the executable that loaded it
is a Windows Service -or- a Console/Windows application? i.e. within the code
of this component how can I determine if the process that loaded it is a
Service or App? I am looking for a C# sample.... thanks!
 
R

Robby

Why not just expose a property that the user of your components can set.
For example, expose a ServiceApplication property that the user must set to
TRUE if the component is in a service application or FALSE if the component
is not in a service application. Then all you would need to do is check the
related variable when you need to do something different for a service
application.

Quick, easy and flexible.

Robby
VB.Net
 
G

Guest

Robby,

Thanks for your reply. But for reasons that are beyond my control, I do need
to automatically infer if the component is instantiated inside a service or
application. I cannot change the interface and have to rely on some automated
mechanism of detecting if the component is running within a Service or
Application? Any other suggestions??

-ToeKnee
 
J

John Saunders

ToeKnee said:
Robby,

Thanks for your reply. But for reasons that are beyond my control, I do
need
to automatically infer if the component is instantiated inside a service
or
application. I cannot change the interface and have to rely on some
automated
mechanism of detecting if the component is running within a Service or
Application? Any other suggestions??

What aspect of your component needs to change because it's in a service? In
other words, "whether I'm in a service" is an implementation detail.
Something like "where should I write my output" is more general and makes
better sense.

John Saunders
 
G

Guest

John,

Well, the architecture of the toplevel application is that when it is
running as a Service, it is assumed to be in a special environment (in a
Virtual Server with some custom redundancy/hot-standby logic that the
component has to support). But when they run it as an Console Application,
it does not need to that hot-standby logic.
Unfortunately the "architects" of this stuff "forgot" to explicitly tell
any of the components what mode the app is in (Console vs. Windows Service).
This stuff is deployed in many locations and it is for us mere component
developers to figure this out bow..l. we're too late in the game and I am
sure theer has to be a way in C#/.NET to figure this out... but I'm not
having any luck yet....

I know you are curious as to "why" I am needing this, but assuimng that I
really do (and hoping I really have explored most other avenues), can you
tell me "how" I would go about achieving this? THANKS A LOT!!

-ToeKnee
 
J

Jay B. Harlow [MVP - Outlook]

ToeKnee,
Does it only run as a Service on a single machine?

If it only runs on as a Service on a single machine or a limited number of
machines, I would (seriously) consider adding a custom section to the
app.config that indicates to use the custom redundancy/hot-standby logic.
The custom section would tell the component "what mode the app is in"...

The component would "fail gracefully" if this custom section is not
available (Allowing the console applications to have a "bare" app.config).

This way if you ever need to allow the console version to support custom
redundancy/hot-standby logic, its a matter of modifying the app.config. Like
wise if you ever need to run the service on a different server, its a matter
of modifying the app.config.

Of course if you have multiple Virtual Servers then updating all of the
app.configs may be more work...

I would use a custom section over appSettings to ensure proper isolation. I
normally make the custom section name reflect (or at least closely related
to) the namespace of the component...

For information on custom sections in the app.config see:

http://msdn.microsoft.com/library/d...uide/html/cpconcreatingnewsectionhandlers.asp

http://msdn.microsoft.com/library/d...de/html/cpconconfigurationsectionhandlers.asp

http://msdn.microsoft.com/library/d...cpcondeclaringcustomconfigurationsections.asp

http://msdn.microsoft.com/library/d.../cpguide/html/cpcondeclaringsectiongroups.asp

Hope this helps
Jay
 
J

John Saunders

ToeKnee said:
John,

Well, the architecture of the toplevel application is that when it is
running as a Service, it is assumed to be in a special environment (in a
Virtual Server with some custom redundancy/hot-standby logic that the
component has to support). But when they run it as an Console
Application,
it does not need to that hot-standby logic.
Unfortunately the "architects" of this stuff "forgot" to explicitly tell
any of the components what mode the app is in (Console vs. Windows
Service).
This stuff is deployed in many locations and it is for us mere component
developers to figure this out bow..l. we're too late in the game and I am
sure theer has to be a way in C#/.NET to figure this out... but I'm not
having any luck yet....

I know you are curious as to "why" I am needing this, but assuimng that I
really do (and hoping I really have explored most other avenues), can you
tell me "how" I would go about achieving this? THANKS A LOT!!

Sorry, no clue, other than fire the designers.

John Saunders
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top