Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NetworkChange NetworkAddressChanged event throws NetworkInformationException on Android #77822

Closed
chriskarlsson opened this issue Nov 3, 2022 · 12 comments

Comments

@chriskarlsson
Copy link

Description

NetworkInformationException is thrown when trying to subscribe to NetworkChange.NetworkAddressChanged on Android.

There is no indication in the source code that Android is UnsupportedOSPlatform but Android is not listed as supported in Microsoft Learn.

Reproduction Steps

Add the following to MainActivity of a clean Android or Maui application project:

NetworkChange.NetworkAddressChanged += (sender, args) =>
{
    Console.WriteLine($"Sender: {sender} args: {string.Join(", ", args.ToString())}");
};

Expected behavior

Console output when network address changes.

Actual behavior

NetworkInformationException is thrown with the quite cryptic exception message "Success".

Regression?

This was working fine when the application ran on Mono. It also works fine on .Net6-ios and .Net6-mac.

Known Workarounds

No response

Configuration

Which version of .NET is the code running on? - Behavior seen on both .Net6-android with and without Maui
What OS and version, and what distro if applicable? - Android 12/API 31
What is the architecture (x64, x86, ARM, ARM64)? - ARM

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Nov 3, 2022
@ghost
Copy link

ghost commented Nov 3, 2022

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

NetworkInformationException is thrown when trying to subscribe to NetworkChange.NetworkAddressChanged on Android.

There is no indication in the source code that Android is UnsupportedOSPlatform but Android is not listed as supported in Microsoft Learn.

Reproduction Steps

Add the following to MainActivity of a clean Android or Maui application project:

NetworkChange.NetworkAddressChanged += (sender, args) =>
{
    Console.WriteLine($"Sender: {sender} args: {string.Join(", ", args.ToString())}");
};

Expected behavior

Console output when network address changes.

Actual behavior

NetworkInformationException is thrown with the quite cryptic exception message "Success".

Regression?

This was working fine when the application ran on Mono. It also works fine on .Net6-ios and .Net6-mac.

Known Workarounds

No response

Configuration

Which version of .NET is the code running on? - Behavior seen on both .Net6-android with and without Maui
What OS and version, and what distro if applicable? - Android 12/API 31
What is the architecture (x64, x86, ARM, ARM64)? - ARM

Other information

No response

Author: chriskarlsson
Assignees: -
Labels:

area-System.Net, untriaged

Milestone: -

@ghost
Copy link

ghost commented Nov 3, 2022

Tagging subscribers to 'arch-android': @steveisok, @akoeplinger
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

NetworkInformationException is thrown when trying to subscribe to NetworkChange.NetworkAddressChanged on Android.

There is no indication in the source code that Android is UnsupportedOSPlatform but Android is not listed as supported in Microsoft Learn.

Reproduction Steps

Add the following to MainActivity of a clean Android or Maui application project:

NetworkChange.NetworkAddressChanged += (sender, args) =>
{
    Console.WriteLine($"Sender: {sender} args: {string.Join(", ", args.ToString())}");
};

Expected behavior

Console output when network address changes.

Actual behavior

NetworkInformationException is thrown with the quite cryptic exception message "Success".

Regression?

This was working fine when the application ran on Mono. It also works fine on .Net6-ios and .Net6-mac.

Known Workarounds

No response

Configuration

Which version of .NET is the code running on? - Behavior seen on both .Net6-android with and without Maui
What OS and version, and what distro if applicable? - Android 12/API 31
What is the architecture (x64, x86, ARM, ARM64)? - ARM

Other information

No response

Author: chriskarlsson
Assignees: -
Labels:

area-System.Net, os-android, untriaged

Milestone: -

@am11
Copy link
Member

am11 commented Nov 3, 2022

(aside from the NetworkAddressChanged event issue)

NetworkInformationException is thrown with the quite cryptic exception message "Success".

If this exception is thrown from

if (result != Interop.Error.SUCCESS)
{
string message = Interop.Sys.GetLastErrorInfo().GetErrorMessage();
sh.Dispose();
throw new NetworkInformationException(message);
it shouldn't print "Success". @akoeplinger, looks like there is also something wrong in the error mapping? 🤔

@steveisok
Copy link
Member

@simonrozsival, please take a look.

@simonrozsival
Copy link
Member

I can reproduce the bug locally. I'll continue investigating the issue tomorrow.

@akoeplinger
Copy link
Member

@simonrozsival did you happen to find out whether we could support this?

@simonrozsival
Copy link
Member

@akoeplinger I looked into it and I didn't find a good 1:1 replacement Android API. There are some ConnectivityManager APIs that seemed promissing but they don't behave the same way (for example ConnectivityManager.registerNetworkCallback). I will need to revisit this and look for some alternative ways of implementing it if it's even possible with the current Android restrictions.

@akoeplinger
Copy link
Member

I wonder if we could start a timer/polling loop whenever someone registers the event that would continuously look for network changes

@simonrozsival
Copy link
Member

We could do that. It would certainly work but wouldn't that be quite inefficient to do on a mobile device?

@simonrozsival
Copy link
Member

I'm experimenting with PeriodicTimer to poll network information. I'm still not convinced that this is the optimal implementation but it's likely good enough. I need to do more testing before I submit the PR for full review. For the time being I opened a draft PR and any suggestions are welcome.

@akoeplinger akoeplinger removed the untriaged New issue has not been triaged by the area owner label Jan 23, 2023
@akoeplinger akoeplinger added this to the 8.0.0 milestone Jan 23, 2023
@akoeplinger
Copy link
Member

With #80548 being merged we should be done here right? I don't think it makes sense to backport this to 7.0

@simonrozsival
Copy link
Member

I don't think we should backport the implementation. I think we can close this issue now.

@ghost ghost locked as resolved and limited conversation to collaborators Feb 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants