Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-01-13
7
Medium Priority
?
445 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 1000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

719 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