2
votes

I have added the web service to my WPF windows phone store app, when i run my app in emulator it works, but sometime it get creshes cause lack of internet connectivity.

i'm checking my emulator IMEI is registerd or not in database using WCF service on main_pageload event my code looks like this

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

in this service im checking the mobile IMEI registerd or not. i have checked by debugging the app it goes upto proxy.CheckIMEIRegisteredOrNotAsync(strIMEI); when it leave the context it throuw the error

An exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.ni.dll but was not handled in user code

please suggest me some advice,,,thanks in advance

1
Running wcf service method from client side requires an Internet connection. So when you don't have that access to the Internet - it crashes. It is normal.XardasLord
You are right XardasLord but i want keep its state and show the error message to user the Connection is not available without crashing the application, and i can't able to use try{} catch{} blockCharan Ghate

1 Answers

2
votes

To check if the Internet connection is available I just simply create a method to check it and execute it then application is launching or page is loading. This method I create in App.xaml.cs:

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

     return connection;
}

Then in some page_loaded event I execute it:

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();
    }
}

Now then a client don't have the Internet connection available it won't crash, but it will show a message for the user. I hope it will help.