I have a WCF service under development, and often switch between hosting in a Windows Service, and hosting in a Console app. The service and console app share one config file, so how else can I tell, in my WPF client, if the service is hosted in the console app?
Asked
Active
Viewed 699 times
2
-
1The WCF **service** itself only knows about it's `ServiceHost` - but not about the environment the `ServiceHost` is existing in.... I don't think there's anything out of the box to differentiate this. Why do you need this, anyway?? If you absolutely **must have** this feature, I guess you'll have to create a `ServiceHost` descendant that you can tell if it's running in a console app or a Windows service .... – marc_s Mar 08 '15 at 07:48
-
The WPF client app makes too many assumptions that the WCF is Windows Service hosted. As a compromise while under dev and debugging, I want to check the type of host and skirt access to the Windows Service. i will be revising the client app quite heavily, but my utter priority is testing and debugging the WCF itself. The client app is fairly low priority: if I can start the Windows service and the WCF runs quietly in that service, I can give my client an overdue on-site test version. A servicehost descendant or extension is high on my list of better but longer term solutions. – ProfK Mar 08 '15 at 07:59
-
For now, I check if the windows service is installed and enabled, and if not, I avoid anything to do with it. Then, if my wcf is still running, it means i's console hosted. I feel dirty. – ProfK Mar 08 '15 at 08:39
1 Answers
2
bool windowsServiceHosted = !Environment.UserInteractive;
More hacky (shouldnt be necessary above should work)
private bool? _ConsolePresent;
public bool ConsolePresent {
get {
if (_ConsolePresent == null) {
_ConsolePresent = true;
try { int window_height = Console.WindowHeight; }
catch { _ConsolePresent = false; }
}
return _ConsolePresent.Value;
}
}
bool windowsServiceHosted = !ConsolePresent;
If you need to know from client then you'll need to expose a bool WindowServicesHosted
propetry from your server that uses one of the above server side.

Ricibob
- 7,505
- 5
- 46
- 65
-
Nice hack with the `WindowHeight`. But won't `!Environment.UserInteractive` return true for other non-interactive hosts like IIS? – ProfK Mar 09 '15 at 12:55
-
1Good question! I havent personally tested with IIS but this link suggests IIS hosting will return false for Enviroment.UserInteractive. https://social.microsoft.com/Forums/en-US/04b767d4-3e3e-47e2-abd8-7bfad2b0a749/web-development-server-code-running-in-userinteractive-mode?forum=whatforum – Ricibob Mar 09 '15 at 17:42