开发者

WP7 check if internet is available

开发者 https://www.devze.com 2023-01-29 21:21 出处:网络
My app WP7 was not accepted because it fails to load if the internet is not available. I looked for a way to check it and found this command

My app WP7 was not accepted because it fails to load if the internet is not available. I looked for a way to check it and found this command

NetworkInterface.GetIsNetworkAvailable()

But it isn't working on the emulator and I do not have any device to test it. Could someone tell me if it returns false if the device is in Airplane mode? If not, how can I check for it?

Thanks, Oscar

Edit: I also tried with this code:

try
{
    wsClient.CurrenciesCompleted += new EventHandler&开发者_如何学编程lt;CurrencyConversion.CurrenciesCompletedEventArgs>(wsClient_CurrenciesCompleted);
    wsClient.CurrenciesAsync(null);
}
catch
{
     NetworkNotAvailable();
}

But I am not able to catch the exception, I also tried in the wsClient_CurrenciesCompleted method, but also no good.

Where could I test it?


Don't test for "the internet in general" - test for the service you'll actually be connecting to. Test for it by trying to connect to it - make some simple, non-destructive request on start-up. Yes, that will take a tiny bit of the user's data allowance, but:

  • You will be warming up the networking stack and making a connection which should end up being kept alive automatically, so future latency will be reduced.
  • You could warn the user that they may have limited functionality if the connection fails.


An Alternative to Jon's suggestion is to check which network interface is available. This is very handy in cases were you need to adjust which service you call based on network speed. For example the switch statement below could be modified to return an Enum to represent the quality of the network.

public class NetworkMonitorClass 
{
   private Timer timer;
   private NetworkInterfaceType _currNetType = null; 
   private volatile bool _valueRetrieved = false;

   public NetworkMonitorClass()
   {
       //using a timer to poll the network type.
       timer = new Timer(new TimerCallBack((o)=>
       {
           //Copied comment from Microsoft Example:
           //  Checking the network type is not instantaneous
           //  so it is advised to always do it on a background thread.
           _currNetType = Microsoft.Phone.Net.NetworkInformation.NetworkInterface.NetworkInterfaceType;
           _valueRetrieved = true;
       }), null, 200, 3000); // update the network type every 3 seconds.
   }

   public NetworkInterfaceType CurrentNetworkType 
   { 
       get
       {  
           if(false == _valueRetrieved ) return NetworkInterfaceType.Unknown;  
           return _currNetType; 
       } 
       private set { ;} 
   }

   public bool isNetworkReady()
   {
       if(false == _valueRetrieved ) return false;

       switch (_currentNetworkType)
       {
          //Low speed networks
          case NetworkInterfaceType.MobileBroadbandCdma:
          case NetworkInterfaceType.MobileBroadbandGsm:
            return true;
          //High speed networks
          case NetworkInterfaceType.Wireless80211:
          case NetworkInterfaceType.Ethernet:
            return true;
          //No Network
          case NetworkInterfaceType.None:
          default:
             return false;
      } 
   }
}

See http://msdn.microsoft.com/en-us/library/microsoft.phone.net.networkinformation.networkinterface.networkinterfacetype(VS.92).aspx


GetIsNetworkAvailable() will always return true in the emulator. For testing in the emulator you'll need to work round this in code.

This can be a useful quick check but you also (as Jon pointed out) need to handle the scenario of not being able to connect to your specific server.

Handling this can be done by catching the WebException when you try and get the response in the callback.

private static void DownloadInfoCallback(IAsyncResult asynchronousResult)
{
    try
    {
        var webRequest = (HttpWebRequest)asynchronousResult.AsyncState;

        // This will cause an error if the request failed
        var webResponse = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);

        .....
    }
    catch (WebException exc)
    {
        // Handle error here
    }
}


GetIsNetworkAvailable() works properly on device.

You can mock your handling of this for testing in the emulator using Microsoft.Devices.Environment.DeviceType.

I would be inclined to test both for avaiability of the internet and availability of your site through exception handling and provide feedback to the user of the app that indicates what the true reason is for features being unavailable.

0

精彩评论

暂无评论...
验证码 换一张
取 消