title: Catching exception on failure to connect to server with WCF:
body: <p>I have a <code>Connection</code> class in my client application, which will connect to a WCF server.</p>
  
  <p>If the server is not started, or for the client could not connect to the server, the exception should be caught and an error message is displayed on a label.</p>
  
  <p>The first time I create an instance of the <code>Connection</code> class is here, in my <code>LoginViewModel</code> class:</p>
  
  <pre><code>sta = Connection.Instance.HttpProxy.Login(LoginID, LoginPassword);
  </code></pre>
  
  <p>and that is where the first exception is thrown. However, if I add a try/catch block, the exception will not be caught, but instead an <code>XamlParseException</code> is thrown instead. The try/catch block in the constructor of the <code>Connection</code> class will not work either. How do I handle this?</p>
  
  <p><code>Connection</code> class:</p>
  
  <pre><code>public sealed class Connection {
  
      private readonly string _address = ConfigurationManager.AppSettings["ConnectionAddress"];
      private static Connection _instance;
      private static object _padLock = new Object();
      private static ChannelFactory&lt;IPoS&gt; httpFactory;
  
      private static IPoS _httpProxy; //Singleton
      public IPoS HttpProxy { get { return _httpProxy; } }
  
      public static Connection Instance {
          get {
              if (_instance == null) {
                  lock (_padLock) {
                      if (_instance == null) {
                          _instance = new Connection();
                      }
                  }
              }
              return _instance;
          }
      }
  
      private Connection() {
          try {
              httpFactory = new ChannelFactory&lt;IPoS&gt;(
                  new BasicHttpBinding(),
                  new EndpointAddress(_address));
              _httpProxy = httpFactory.CreateChannel();
          }
          catch (Exception ex) {
              BaseViewModel.SetErrorMessage("Error: " + ex, this);
          }
      }
  }
  </code></pre>