本文介绍了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上不起作用。 



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


解决方案

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.

解决方案


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

10-20 23:56