By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,680 Members | 1,868 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,680 IT Pros & Developers. It's quick & easy.

Am in a Windows Service or in a Application?

P: n/a
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!

Jul 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
"ToeKnee" <To*****@discussions.microsoft.com> wrote in message
news:41**********************************@microsof t.com...
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!

Jul 21 '05 #2

P: n/a
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
Jul 21 '05 #3

P: n/a
"ToeKnee" <To*****@discussions.microsoft.com> wrote in message
news:57**********************************@microsof t.com...
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
Jul 21 '05 #4

P: n/a
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

"John Saunders" wrote:
"ToeKnee" <To*****@discussions.microsoft.com> wrote in message
news:57**********************************@microsof t.com...
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

Jul 21 '05 #5

P: n/a
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/de...onhandlers.asp

http://msdn.microsoft.com/library/de...onhandlers.asp

http://msdn.microsoft.com/library/de...onsections.asp

http://msdn.microsoft.com/library/de...tiongroups.asp

Hope this helps
Jay
"ToeKnee" <To*****@discussions.microsoft.com> wrote in message
news:19**********************************@microsof t.com...
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

"John Saunders" wrote:
"ToeKnee" <To*****@discussions.microsoft.com> wrote in message
news:57**********************************@microsof t.com...
> 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

Jul 21 '05 #6

P: n/a
"ToeKnee" <To*****@discussions.microsoft.com> wrote in message
news:19**********************************@microsof t.com...
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
Jul 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.