.netwcfself-hostingservicehostwcf-hosting

How can I tell whether a WCF service is hosted in a Console app?


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?


Solution

  • 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.