none
System.Net.NetworkInformation.NetworkChange class not fully working with .NET Core 2.0 or 2.1 RRS feed

  • Question

  • I have a simple .NET Core Console application with the following code in Program.cs:

    using System;
    using System.Net.NetworkInformation;
    
    namespace ApplicationBridgeTestConsole
    {
        class Program
        {
            static void Main(string[] args)
            {
                NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged;
                NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;
                Console.WriteLine("Hit any key to exit.");
                Console.ReadKey();
            }
    
            private static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
            {
                Console.WriteLine($"NetworkAvailability Changed: IsAvailable = {e.IsAvailable}");
            }
    
            private static void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
            {
                Console.WriteLine($"Network Address Changed");
            }
        }
    }
    

    When I target the console app to .NET Core 2.0, and I connect/disconnect an ethernet cable to my network, I get the following output:


    This tells me that the NetworkAvailabilityChanged event either isn't implemented, or it's not working!  Even though Microsoft docs indicate that it is supports: https://docs.microsoft.com/en-us/dotnet/api/system.net.networkinformation.networkchange.networkavailabilitychanged?view=netframework-4.7.2

    When I target the console app to .NET Core 2.1, and I connect/disconnect an ethernet cable to my network, I get the following output:

    This is even worse on Core 2.1 than on 2.0!  As you can see, not only is NetworkAvailabilityChanged not working, but also NetworkAddressChanged only worked on ethernet disconnection, but does not work when I connect the ethernet cable back.  I have tested this multiple times, and on three different devices, all with similar results.  NetworkAvailabilityChanged event just does not work on .NET Core. 

    Can anyone else confirm?  I thank you much and appreciate you taking time to read this post.

    Wednesday, January 9, 2019 8:57 PM

All replies

  • Hi Tam Bui,

    Thank you for posting here.

    For your question, I test the code you provided. But I get the different result. Based on my test, I could trigger the NetworkAvailabilityChanged event.

    In .Net Core 2.0

    In .Net Core 2.1

    Only the difference is that the .net core 2.1 call the NetworkAddressChanged again. Maybe there is something difference between .net core 2.1 source code and .net core 2.0 source code.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 10, 2019 7:58 AM
    Moderator
  • Michelle,

    Thank you for your help in testing my sample!  May I ask, did you disconnect then reconnect the network?  Because your console output only shows disconnection.  I'm curious if you reconnected but it didn't detect the connection, or if you simply didn't do it at all.

    Regards,

    Tam

    Thursday, January 10, 2019 5:55 PM
  • Hi Tam Bui,

    I test the code again. If I disconnect the network and reconnect, the .net core 2.0 works well. But the .net core 2.1 only record once, it miss the next network connection. 

    Maybe the change of source code between .net core 2.0 and .net core 2.1 cause the error. You could post a bug using your VS.

    Click Help in VS menu, choose Send Feedback to report a Problem.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Friday, February 1, 2019 7:40 AM
    Tuesday, January 15, 2019 7:39 AM
    Moderator