Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / January 2005

Tip: Looking for answers? Try searching our database.

How can I know if I am in a Service or Application?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ToeKnee - 05 Jan 2005 21:26 GMT
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!
John Saunders - 06 Jan 2005 00:06 GMT
>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
[quoted text clipped - 3 lines]
> 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!

I'm curious - why do you want to know? What if it happens to get used in a
Windows Forms application or ASP.NET application later down the road?

If, for instance, you need to know where to send output, you could let the
caller of the component pass you a TextWriter to output to. You could also
consider using System.Diagnostics.Trace, and let the caller decide which
listeners he likes.

John Saunders
ToeKnee - 06 Jan 2005 00:20 GMT
John,

Thanks for your reply. Well, the situation is that it is a well published
interface and I cannot change it at this stage. From my perspective all I
need to know if a Windows Service is instantiating the component or
'something else'. I know it sounds like a curious requirement - it is long
story, but in a nutshell "they" can run their app as a Service or as a
Console App and "they" want me (my component)  to be able to automatically
determine what mode it is running in and behave accordingly. Seems I am stuck
with this *requirement* :(

Isn't there a way I can automatically determine at runtime if the component
got instantiated inside a Windows Service or a Console App?  I am sure
someone can figure out (with a touch of Reflection magic?) how to do this...
TIA TIA TIA!

-ToeKnee

> >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
[quoted text clipped - 13 lines]
>
> John Saunders
Andrew - 06 Jan 2005 01:07 GMT
There is a static method of the ServiceController class in named
GetServices() which returns an array of ServiceController objects one
representing each service on the computer search through these and find the
service then use the Status property to find if the service is running. If
the service is running do the stuff associated with the service if not
assume console

> John,
>
[quoted text clipped - 41 lines]
>>
>> John Saunders
Richard Grimes [MVP] - 06 Jan 2005 21:20 GMT
> Thanks for your reply. Well, the situation is that it is a well published
> interface and I cannot change it at this stage. From my perspective all I
[quoted text clipped - 5 lines]
> stuck
> with this *requirement* :(

<sigh> customers, don't you just hate them?

The difference between a service and a GUI (or Console) app is three fold:

1) the service can start when the machine starts
2) the service can be automatically restarted if it dies
3) the service can (and should) run under an account other than the
interactive user

Given those, here's some ideas.

#3 is the only one that you can detect. You could get the WindowsPrincipal
and check whether it is in the BUILTIN\Users role (for a GUI/console app),
but that seems a bit lame.

Another idea is to get a StackTrace and walk the stack to the top. In a GUI
or console app the top of the stack will be the entrypoint (usually Main)
for the main thread or its the thread procedure for a worker thread. If the
app is a service then the service control manager will start the service and
will also start a thread that will run in the service. The top of stack for
OnStart top will be ServiceBase.ServiceQueuedMainCallback. Unfortunately,
most services start one or more worker threads to do their work and so
there's no specific method you can look for.

Another idea, which should work, is to get the entrypoint
(Assembly.GetExecutingAssembly() will give the current assembly, EntryPoint
will be non-null if the current assembly has the entrypoint, if it is null
call GetCallingAssembly() recursively until EntryPoint is non-null).
EntryPoint returns the MethodInfo for the entrypoint. Call
MethodInfo.Reflected type to get the type that has the entrypoint method,
and then call BaseType to see if that type is derived from ServiceBase. (For
a GUI its likely to derive from Form, for a console the base type will be
Object).

> Isn't there a way I can automatically determine at runtime if the
> component
> got instantiated inside a Windows Service or a Console App?  I am sure
> someone can figure out (with a touch of Reflection magic?) how to do
> this...
> TIA TIA TIA!

Personally, I think the EntryPoint solution is the best one (and it fits
your request for some reflection magic ;-) ).

Richard
Signature

www.richardgrimes.com
my email evpuneqt@zicf.bet is encrypted with ROT13 (www.rot13.org)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.