Network - NKS0051
Use NetworkTools.NetworkAvailable
Last updated
Use NetworkTools.NetworkAvailable
Last updated
This analyzer provides the following strings:
Context | String |
---|---|
This code analyzer detects the usage of GetIsNetworkAvailable
from the NetworkInterface
class found in the System.Net.NetworkInformation
namespace.
Normally, NetworkInterface.GetIsNetworkAvailable
would return true if there are network interfaces that are ready to connect (their state is UP). However, this approach doesn't work on Android systems as Nitrocid doesn't build for Xamarin.Android, which has the necessary libraries for Java bindings to the Android API.
As a result, you'll have to use the sensible NetworkTools.NetworkAvailable
property to allow you to check to see if there are ready network interfaces. Please note that if you have no working internet connection, even if your WiFi is turned on in your Android device, it'll report as false.
To get a brief insight about how this analyzer works, compare the two code blocks shown to you below:
You can suppress this suggestion by including it in the appropriate place, whichever is convenient.
For more information about how to suppress any warning issued by the Nitrocid analyzer, visit the below page:
We recommend that every caller which use this function use the recommended abovementioned method.
Error List
Caller uses NetworkInterface.GetIsNetworkAvailable
instead of NetworkTools.NetworkAvailable
Suggestion Box
Use NetworkTools.NetworkAvailable
instead of NetworkInterface.GetIsNetworkAvailable
Description
While NetworkInterface.GetIsNetworkAvailable
checks your network, it doesn't work on Android systems, so it's better to use NetworkTools.NetworkAvailable
. Please note that this will return false
if there is no connectivity for Android systems.