Solved

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

Posted on 2014-01-13
7
427 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
Comment Utility
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
Comment Utility
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

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:rye004
Comment Utility
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
Comment Utility
XML config sounds like a reasonable solution. Avoids the coupling issue.
0
 
LVL 22

Accepted Solution

by:
Snarf0001 earned 250 total points
Comment Utility
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
Comment Utility
This is great thank you.  I ended up passing a value to determine this, but will try this in the future.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Help with checking if excel exist in client's computer 9 35
Visual Studio installation 5 32
Wpf develop 5 29
Expando 4 33
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now