You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Android] NetworkInterface.GetAllNetworkInterfaces failed with NetworkInformationException (message: Success) #75809
On Android 11 release build, I was encountering System.Net.NetworkInformation.NetworkInformationException when called System.Net.NetworkInformation.NetworkInterface。GetAllNetworkInterfaces, and the error string is just success. I have added two permissions: INTERNET and ACCESS_NETWORK_STATE to which no avail. I think it's worth mentioning.
I saw a log category named DOTNET_LINK so I reported the issue here.
Reproduction Steps
Using System.Net namespace, call NetworkInterface.GetAllNetworkInterfaces
Expected behavior
The function completes with no exception
Actual behavior
The function throws System.Net.NetworkInformationException
Regression?
No response
Known Workarounds
No response
Configuration
.NET 6.0.9
Other information
I skimmed through the PRs and there was a change to exclusively use the Xamarin's implementation of getifaddrs recently. But on Android 11 Google blocked NETLINK_ROUTE family socket from being bind.
This is also the log that I was getting from adb
I think it's correspond to this. I hope I'm right.
One small note, I think this quirk is per vendor distribution, as my Xiaomi phone does not seems have this problem.
Thanks - could you paste in the stacks/messages as regular text? You can use triple back tick above and below for formatting. This makes it possible for search to find them
[ERROR] FATAL UNHANDLED EXCEPTION: System.Net.NetworkInformation.NetworkInformationException (0x80004005): Success
at System.Net.NetworkInformation.NetworkInterfacePal.TransformNetworkInterfacess[AndroidNetworkInterface[]](Func`5 transform)
at System.Net.NetworkInformation.NetworkInterfacePal.GetAndroidNetworkInterfaces()
at System.Net.NetworkInformation.NetworkInterfacePal.GetAllNetworkInterfaces()
at System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
at Microsoft.Phone.Net.NetworkInformation.NetworkInterface.get_NetworkInterfaceType()
at TestApp.MetricsSender.CheckConnectionType()
at System.Threading.Thread.StartCallback()
The log message I think to be related is this line. This is the only error line I found near this exception:
I started looking into the problem and as a quick workaround it is possible to modify AndroidManifest.xml with:
<uses-sdkandroid:minSdkVersion="29" />
When the minSdkVersion is 29 or lower, the exception isn't thrown and a correct array of interfaces is returned (tested on a Samsung phone with Android 12). In the case when it is necessary to use 30 or newer, it is necessary to use Java.Net.NetworkInterface.NetworkInterfaces.
I'll keep working on a proper fix for this issue that will work with new SDKs.
If anyone is looking for a workaround, without changing minSdkVersion and as @simonrozsival is suggesting with java bindings you can do something like this:
publicIList<IPAddress>GetAllLocalValidIp4Addresses(){
#if ANDROID// This is a workaround for this issue of MAUI on Android -> https://github.com/dotnet/runtime/issues/75809 // Instead of using regular .net we use Java Interop bindings directly.varinetEnum=Java.Net.NetworkInterface.NetworkInterfaces;if(inetEnumisnull){returnnewList<IPAddress>();}varipAddresses=newList<IPAddress>();foreach(varinterfacesinJava.Util.Collections.List(inetEnum)){varaddresses=(interfacesasJava.Net.NetworkInterface)?.InetAddresses;if(addresses==null){continue;}foreach(Java.Net.InetAddressaddressinJava.Util.Collections.List(addresses)){if(address.HostAddress==null||address.IsLoopbackAddress||addressis not Java.Net.Inet4Address){continue;}ipAddresses.Add(IPAddress.Parse(address.HostAddress));}}returnipAddresses;
#else
#pragma warning disable CS8619// This is not going to have any null, but the compiler is not able to determine it.returnNetworkInterface.GetAllNetworkInterfaces().Where(n =>n.OperationalStatus==OperationalStatus.Up).Where(n =>n.NetworkInterfaceType==NetworkInterfaceType.Wireless80211||n.NetworkInterfaceType==NetworkInterfaceType.Ethernet).Where(n =>!n.Description.ToLower().Contains("virtual")).Where(n =>!n.Description.ToLower().Contains("pseudo")).SelectMany(n =>n.GetIPProperties().UnicastAddresses).Where(n =>n.Address.AddressFamily==AddressFamily.InterNetwork).Select(g =>g?.Address).Where(a =>a!=null)// Here I am checking to not adding anything null..ToList();
#pragma warning restore CS8619
#endif
}
Sorry @simonrozsival I didn't noticed you're a developer of the project :D
I started looking into the problem and as a quick workaround it is possible to modify AndroidManifest.xml with:
<uses-sdk android:minSdkVersion="29" />
When the minSdkVersion is 29 or lower, the exception isn't thrown and a correct array of interfaces is returned (tested on a Samsung phone with Android 12). In the case when it is necessary to use 30 or newer, it is necessary to use Java.Net.NetworkInterface.NetworkInterfaces.
I'll keep working on a proper fix for this issue that will work with new SDKs.
I am on an issue with a project that uses a third party library ( sipsorcery ) which is .net 6, even setting minSdkVersion on my project my app still crashes as now the crash is into that library.
I am checking how to patch it as it's opensource, but it's pure .net6 so not sure if I am going to be able to access Java.Net.NetworkInterface. It doesn't looks like that at first glance..
@simonrozsival I can see this has been back-ported to net7, but it also affects net6, and net7 isn't released yet. Can we get a back port to net6 please?
Activity
danmoseley commentedon Sep 18, 2022
Thanks - could you paste in the stacks/messages as regular text? You can use triple back tick above and below for formatting. This makes it possible for search to find them
8212369 commentedon Sep 20, 2022
Here is the exception stack:
The log message I think to be related is this line. This is the only error line I found near this exception:
runtime/src/native/libs/System.Native/pal_ifaddrs.c
Line 104 in 55060eb
steveisok commentedon Sep 20, 2022
/cc @simonrozsival
forlayo commentedon Sep 20, 2022
Another one suffering the issue here , this is what I see on logcat..
simonrozsival commentedon Sep 27, 2022
I started looking into the problem and as a quick workaround it is possible to modify
AndroidManifest.xmlwith:When the
minSdkVersionis 29 or lower, the exception isn't thrown and a correct array of interfaces is returned (tested on a Samsung phone with Android 12). In the case when it is necessary to use 30 or newer, it is necessary to useJava.Net.NetworkInterface.NetworkInterfaces.I'll keep working on a proper fix for this issue that will work with new SDKs.
EDIT: The
targetSdkVersionneeds to be 29 or lower which unfortunately makes this workaround unusable for apps uploaded to the Google Play Store (https://developer.android.com/google/play/requirements/target-sdk).forlayo commentedon Sep 28, 2022
If anyone is looking for a workaround, without changing minSdkVersion and as @simonrozsival is suggesting with java bindings you can do something like this:
Sorry @simonrozsival I didn't noticed you're a developer of the project :D
Slm0nB commentedon Sep 28, 2022
We are struggling with this issue as well, and we looking forward to a fix. Thank you for the work around - appreciated!
forlayo commentedon Oct 5, 2022
I am on an issue with a project that uses a third party library ( sipsorcery ) which is .net 6, even setting minSdkVersion on my project my app still crashes as now the crash is into that library.
I am checking how to patch it as it's opensource, but it's pure .net6 so not sure if I am going to be able to access Java.Net.NetworkInterface. It doesn't looks like that at first glance..
Workaround for .NET 6 issue 75809: dotnet/runtime#75809
jahmai-ca commentedon Oct 10, 2022
@simonrozsival I can see this has been back-ported to net7, but it also affects net6, and net7 isn't released yet. Can we get a back port to net6 please?
MartinRothschink commentedon Oct 19, 2022
I second that, we need net6 support.
That function was always not working correctly on Android: dotnet/maui#6649
steveisok commentedon Oct 19, 2022
This was an oversight on my part as I only backported our change to .NET 7. I'll work to get this back to .NET 6.
Workaround for .NET 6 issue 75809: dotnet/runtime#75809 (#855)
karelz commentedon Mar 22, 2023
Fixed in main (8.0) in PRs #76370 and #76541, and in 7.0 (RC2?) in PR #76565 and in 6.0.12 in PR #77260.