Solved

Can a C# Class Determine if it is being called from a Console, Service or Windows Form?

Posted on 2014-01-13
7
430 Views
Last Modified: 2014-01-17
I have a static class that I have created in C#.  I have been testing my static Class using a console application.  Now that my testing is done, I am going to integrate this to a Windows Service.

Is it possible to determine from within the static Class if it is being called from a Console, Service or a Windows Form application?  Depending on the type of application it is being used in, there are some functions that I would want to preform differently.

Many Thanks!
0
Comment
Question by:rye004
7 Comments
 
LVL 10

Expert Comment

by:Korbus
ID: 39776956
I dont think so.  I suspect you will need to add a parameter to your class constructor to tell it how to operate. (In fact, I would recommend this anyway, so users of the class have more control.)

But again, I'm not sure, lets see what other experts say.
0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39777027
Why you probably shouldn't

I agree with Korbus. If you detect inside your code you tightly couple it to the user interface which is a bad dependency. It's better to invert control and have the calling application configure your class as required.

The fact that you're trying to detect the calling code also suggests you're doing something in your C# class that should in fact be in your user interface code.

It will also make testing harder since you can't isolate your class properly in your unit tests.

If you absolutely must

If you desperately want to do this any way I know in our one of our apps we have a horrible dependency in our business layer to that grabs System.Web.HttpContext.Current.Response - I believe this is null when it's not run from a web app. There are probably similar environment variables specific to winforms and console apps you could test.

You could also perhaps try running the command line app with a flag and checking Environment.GetCommandLineArgs(), or check the process name System.Diagnostics.Process.GetCurrentProcess().ProcessName.
0
 
LVL 40
ID: 39777167
Be aware that a service does not behave the same way as a "standard" dll.

It is a stateless object, it does not retain values between calls. Each time you call a method in the service, it is a one shot deal. So stuff such as Shared variables won't retain their values between calls.

As far as I know, the only way you can do what you want in a service is either to have different methods for different functions, or pass a parameter to the methods that specifies the type of the caller.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:rye004
ID: 39777792
Thank you everyone for your input.
I wanted to give everyone a little more detail.  My class was designed to run as a service or console application.  It is designed to be lightweight and run on multiple machines.  The application gets called from using Remoting. Basically, I have a master application that calls the machines.  The master application does not care if it runs in a service or console.
My main reason for asking this, is showing output differs from a service or a command line running the application.
I have an XML file that the Master application uses to see which machines are running the class.  From my reading, it looks like I should add an option to my XML file to indicate if the class is running as a service or a console application.
0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39777888
XML config sounds like a reasonable solution. Avoids the coupling issue.
0
 
LVL 22

Accepted Solution

by:
Snarf0001 earned 250 total points
ID: 39778077
I've often had to run services like this, where standard it will run as a windows service, but I'll also launch it in console mode to trigger different tasks or interface with the running instance.

Environment.UserInteractive

is a handy way to detect that IF the win service is a standard install and you know those are the two cases you'll be using it in.
0
 

Author Closing Comment

by:rye004
ID: 39789915
This is great thank you.  I ended up passing a value to determine this, but will try this in the future.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question