2

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

Charan Ghate
  • 1,384
  • 15
  • 32
  • 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 Dec 18 '14 at 10:27
  • 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{} block – Charan Ghate Dec 18 '14 at 10:35

1 Answers1

2

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.

XardasLord
  • 1,764
  • 2
  • 20
  • 45
  • I disconnected my development PC Internet connection and also Make Off the Data connection of my emulator still My emulator always return true by calling the method ** CheckInternetConnection() **, that means network is available, and again application get crashes, I also try the **bool status= DeviceNetworkInformation.IsNetworkAvailable;** and it also return the true while no network available, Please suggest :) – Charan Ghate Dec 18 '14 at 11:07
  • @CharanGhate, when you unplug your PC from the network, can you run the application on your emulator ? – XardasLord Dec 18 '14 at 11:16
  • Yes Mr. @XardasLord I run the application after unplug my PC from the network,and checked by debugging the application it can't return **currentConnection =false** in any how.. – Charan Ghate Dec 18 '14 at 11:21
  • Anyway I always work on my device, I have never worked on WP emulator, so I don't know how that emulator really behaves. I only heart that it can be quite difficult to testing Internet or lack of Internet on emulators. Maybe this will help you -> http://stackoverflow.com/questions/5874885/testing-connectivity-issues-in-windows-phone-7-using-the-emulator I know this is for WP7 but maybe will help you – XardasLord Dec 18 '14 at 11:23
  • *"The emulator always returns NetworkInterface.GetIsNetworkAvailable() as true, even if you emulate network conditions as having no network. I faced this problem myself and the only truly way of testing this behaviour is to deploy the application to a physical device running Windows Phone and test it with the data turned off."* From here -> http://stackoverflow.com/questions/25117193/check-internet-connection-on-phone – XardasLord Dec 18 '14 at 11:36
  • Thanks for your support, I don't have device with me yet, so i will check it out on device. – Charan Ghate Dec 18 '14 at 11:46