日期:2014-05-17  浏览次数:20984 次

有个异常catch不住,大家帮忙看看代码吧 谢谢
我在学习用windows phone写一个通过网络服务查询GPS地理位置名称的方法
用一个方法GetCity来初始化了一个Timer;
然后在GetCity方法里面 用Dispatcher.BeginInvoke方法试图每隔几秒访问一次网络来得到GPS地址;
然而在我断掉所有网络连接之后,这个代码就扔了个异常出来:

“System.ServiceModel.EndpointNotFoundException”类型的未经处理的异常出现在 System.ServiceModel.dll 中。
其他信息: There was no endpoint listening at http://msrmaps.com/TerraService2.asmx that could accept the message. 
我比较菜,找不到是谁扔的异常,所以老catch不住
请大家看看 这个异常应该怎样抓?
代码:
namespace GPS
{
  public partial class MainPage : PhoneApplicationPage
  {
  //声明
  GeoCoordinateWatcher watcher;
  Timer timerForCity;
  TerraService.TerraServiceSoapClient client;
  double longitude, latitude;
  // 构造函数
  public MainPage()
  {
  InitializeComponent();
  watcher = new GeoCoordinateWatcher();
  timerForCity = new Timer(GetCity);
  watcher.MovementThreshold = 3;
  PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
  client = new TerraService.TerraServiceSoapClient();
  longitude = latitude = 0.0;
  }


  private void button1_Click(object sender, RoutedEventArgs e)
  {
  watcher.Start();
  timerForCity.Change(0, 20000);
  }
  private void GetCity(object state)
  {
  Dispatcher.BeginInvoke(delegate()
  {
  client.ConvertLonLatPtToNearestPlaceCompleted += new EventHandler<TerraService.ConvertLonLatPtToNearestPlaceCompletedEventArgs>(client_ConvertLonLatPtToNearestPlaceCompleted);

  TerraService.LonLatPt point = new TerraService.LonLatPt();
  point.Lat = latitude;
  point.Lon = longitude;
  //在执行下面这个语句之后就出现了一个错误,
//“System.ServiceModel.EndpointNotFoundException”类型的未经处理的异常出现在 System.ServiceModel.dll 中。
//其他信息: There was no endpoint listening at http://msrmaps.com/TerraService2.asmx that could accept the //message. 
//原因嘛,就是我关掉了手机的任何网络连接。
//
//而且这个try-catch貌似没有用,并没有catch到什么异常 这个异常一直存在;
  try{
  client.ConvertLonLatPtToNearestPlaceAsync(point);}
  catch(System.ServiceModel.EndpointNotFoundException ex)
  {
  cityTextBlock.Text = ex.Message;
  }
   
  });
  }

  void client_ConvertLonLatPtToNearestPlaceCompleted(object sender, TerraService.ConvertLonLatPtToNearestPlaceCompletedEventArgs e)
  {
  cityTextBlock.Text = e.Result;
  }


  }
}