Search code examples
c#windows-phone-8windows-phone-8.1network-service

NetworkService exception occurs in WP 8 not WP 8.1. Is there a way I can re-write this to work for both 8 and 8.1?


Finally found out why some users have reported crashes and others say they have no issues. I use NetworkService class to check internet connectivity. But apparently this doesnt work well with WP 8. Is there a way to redo this so it will work on both OS's? I get an exception at this line in my NetworkServices.cs

return profile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;

NetworkServices.cs:

{
public class NetworkService : INetworkService
{
    public bool IsConnectionAvailable
    {
        get
        {
            ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile();

            if (profile == null)
            {
                return false;
            }

            return profile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
        }
    }
}

relevant MainPage.cs:

if (data.IsDownloaded)
        {
            this.PlaySound(IsolatedStorageFile.GetUserStoreForApplication().OpenFile(data.SavePath, FileMode.Open, FileAccess.Read, FileShare.Read));
        }
        else
        {
            if (!SimpleIoc.Default.GetInstance<INetworkService>().IsConnectionAvailable)
            {
                MessageBox.Show("You need an network or cellular connection to download.");
            }

Stacktrace:

{System.NotImplementedException: The method or operation is not implemented. at Windows.Networking.Connectivity.ConnectionProfile.GetNetworkConnectivityLevel() at App.Services.NetworkService.get_IsConnectionAvailable() at App.MainPage.LongListSelector_SelectionChanged(Object sender, SelectionChangedEventArgs e) at Microsoft.Phone.Controls.LongListSelector.set_SelectedItem(Object value) at Microsoft.Phone.Controls.LongListSelector.OnItemTap(Object sender, GestureEventArgs e) at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args) at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)} System.Exception {System.NotImplementedException}


Solution

  • The NetworkInformation.GetInternetConnectionProfile() on Windows Phone 8 is a newer WinRT based API but unfortunately it doesn't look like most of the methods were implemented.

    At runtime, using the debugger, you'll see that nearly all of the properties throw exceptions of type NotImplementedException. But because the methods and properties were stubbed, Visual Studio doesn't know any better and allows the code to be compiled.

    I have tested the following methods in WP8 SL, WP8.1 SL, WP8.1 XAML projects with the WP8 and WP8.1 emulators, where possible, to confirm the following...


    Windows Phone 7.5 and 8.0

    As NetworkInformation.GetInternetConnectionProfile() either isn't implemented fully (or at all on 7.5), the MSDN documentation recommends...

    DeviceNetworkInformation.IsNetworkAvailable();
    

    Note: this can still be used in Windows Phone 8.1 Silverlight


    Windows Phone 8.1 SL and XAML/WinRT

    Due to increasing convergence between Windows 8 and Windows Phone 8, nearly all of WinRT APIs are now available in Windows Phone 8.1. If you're making a Windows Phone 8.1 (SL or XAML) or a Universal all (Windows 8.1 + Windows Phone 8.1), it makes sense to use the latest API...

    NetworkInformation.GetInternetConnectionProfile()