Solved

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

Posted on 2014-01-13
7
437 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 23

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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET Calendar Control 5 54
Inserting LocalDB Table to SQL Server C# 3 51
Import a excel sheet in a grid 2 45
Which is best Image Resizing Web service 11 42
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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 an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 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