System.Net.NetworkInformation.NetworkChange类未完全使用.NET Core 2.0或2.1 [英] System.Net.NetworkInformation.NetworkChange class not fully working with .NET Core 2.0 or 2.1

查看:278
本文介绍了System.Net.NetworkInformation.NetworkChange类未完全使用.NET Core 2.0或2.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的.NET Core Console应用程序,在Program.cs中包含以下代码:

 using System; 
使用System.Net.NetworkInformation;

命名空间ApplicationBridgeTestConsole
{
class Program
{
static void Main(string [] args)
{
NetworkChange。 NetworkAddressChanged + = NetworkChange_NetworkAddressChanged;
NetworkChange.NetworkAvailabilityChanged + = NetworkChange_NetworkAvailabilityChanged;
Console.WriteLine("按任意键退出。");
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");
}
}
}

当我将控制台应用程序定位到.NET Core 2.0时,我连接/断开连接一条到我网络的以太网电缆,我得到以下输出:





这告诉我NetworkAvailabilityChanged事件未实现,或者它无法正常工作! 尽管Microsoft文档表明它是支持的:  https://docs.microsoft.com/en-us/dotnet/api/system.net.networkinformation.networkchange.networkavailabilitychanged?view=netframework-4.7。 2



当我将控制台应用程序定位到.NET Core 2.1,并且我将以太网电缆连接/断开连接到我的网络时,我得到以下输出:




Core 2.1比2.0更差! 正如您所看到的,NetworkAvailabilityChanged不仅不起作用,而且NetworkAddressChanged仅在以太网断开连接时工作,但在我连接以太网电缆时不起作用。 我
已经多次测试了这个,并且在三个不同的设备上,都有类似的结果。  NetworkAvailabilityChanged事件在.NET Core上不起作用。 



其他人可以确认吗? 非常感谢你,感谢你花时间阅读这篇文章。


解决方案

嗨  ; Tam Bui,


感谢您发布此处。


对于您的问题,我测试您提供的代码。但我得到了不同的结果。根据我的测试,我可以触发NetworkAvailabilityChanged事件。


在.Net Core 2.0中



在.Net Core 2.1中



唯一的区别是.net核心2.1再次调用  NetworkAddressChanged。也许.net core 2.1源代码和.net core 2.0源代码之间存在一些区别。


最好的问候,


Wendy


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.

解决方案

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


这篇关于System.Net.NetworkInformation.NetworkChange类未完全使用.NET Core 2.0或2.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆