我的客户端应用程序中有一个Connection
类,它将连接到 WCF 服务器。
如果服务器未启动,或者客户端无法连接到服务器,则应捕获异常并在标签上显示错误消息。
我第一次创建Connection
类的实例是在这里,在我的LoginViewModel
类中:
sta = Connection.Instance.HttpProxy.Login(LoginID, LoginPassword);
这就是引发第一个异常的地方。但是,如果我添加一个 try/catch 块,则不会捕获异常,而是XamlParseException
会抛出一个异常。类的构造函数中的 try/catch 块Connection
也不起作用。我该如何处理?
Connection
班级:
public sealed class Connection {
private readonly string _address = ConfigurationManager.AppSettings["ConnectionAddress"];
private static Connection _instance;
private static object _padLock = new Object();
private static ChannelFactory<IPoS> 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<IPoS>(
new BasicHttpBinding(),
new EndpointAddress(_address));
_httpProxy = httpFactory.CreateChannel();
}
catch (Exception ex) {
BaseViewModel.SetErrorMessage("Error: " + ex, this);
}
}
}