Windows Phone 应用程序在没有互联网连接的情况下与 WCF 服务通信时崩溃



我已将 Web 服务添加到我的 WPF Windows Phone 应用商店应用程序中,当我在模拟器中运行我的应用程序时,它可以工作,但有时它会出现裂缝导致缺乏互联网连接。

我正在检查我的模拟器 IMEI 是否已在数据库中注册main_pageload使用 WCF 服务在数据库中我的代码看起来像这样

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
    SchoolWebService.SchoolAppWebServiceSoapClient proxy = new SchoolAppWebServiceSoapClient();
    proxy.CheckIMEIRegisteredOrNotCompleted += new EventHandler<CheckIMEIRegisteredOrNotCompletedEventArgs>(proxy_CheckIMEIRegisteredOrNotCompleted);
    proxy.CheckIMEIRegisteredOrNotAsync(strIMEI);
}

在此服务中,我检查移动IMEI是否注册。 我已经通过调试应用程序进行了检查,它上升到proxy.CheckIMEIRegisteredOrNotAsync(strIMEI); 当它离开上下文时,它会通过错误

System.ServiceModel.ni 中发生了类型为"System.ServiceModel.CommunicationException"的异常.dll但未在用户代码中处理

请给我一些建议,,,提前谢谢

要检查互联网连接是否可用,我只需创建一个方法来检查它并在应用程序启动或页面加载时执行它。我在 App.xaml 中创建的此方法.cs:

public bool CheckInternetConnection()
{
    bool connection = true;
     ConnectionProfile currentConnection = NetworkInformation.GetInternetConnectionProfile();
     if (currentConnection == null)
     {
         connection = false;
     }
     return connection;
}

然后在某种page_loaded事件中,我执行它:

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
    bool connection = ((App)Application.Current).CheckInternetConnection();
    if (connection == false)
    {
         MessageBox.Show("Internet connection is not available", "Internet connection", MessageBoxButton.OK);
         Application.Current.Terminate();
    }
}

现在,客户端没有可用的互联网连接,它不会崩溃,但它会向用户显示一条消息。我希望它会有所帮助。

相关内容

最新更新