@@ -106,24 +106,31 @@ public static NetworkInterface GetBestNetworkInterface(IPAddress remoteAddress)
106106 foreach ( NetworkInterface networkInterface in NetworkInterface . GetAllNetworkInterfaces ( ) )
107107 {
108108 var ipProperties = networkInterface . GetIPProperties ( ) ;
109- switch ( remoteAddress . AddressFamily )
109+ try
110110 {
111- case AddressFamily . InterNetwork :
112- if ( ipProperties . GetIPv4Properties ( ) . Index == interfaceIndex )
113- {
114- return networkInterface ;
115- }
116-
117- break ;
118- case AddressFamily . InterNetworkV6 :
119- if ( ipProperties . GetIPv6Properties ( ) . Index == interfaceIndex )
120- {
121- return networkInterface ;
122- }
123-
124- break ;
125- default :
126- throw new ArgumentOutOfRangeException ( nameof ( remoteAddress ) ) ;
111+ switch ( remoteAddress . AddressFamily )
112+ {
113+ case AddressFamily . InterNetwork :
114+ if ( ipProperties . GetIPv4Properties ( ) . Index == interfaceIndex )
115+ {
116+ return networkInterface ;
117+ }
118+
119+ break ;
120+ case AddressFamily . InterNetworkV6 :
121+ if ( ipProperties . GetIPv6Properties ( ) . Index == interfaceIndex )
122+ {
123+ return networkInterface ;
124+ }
125+
126+ break ;
127+ default :
128+ throw new ArgumentOutOfRangeException ( nameof ( remoteAddress ) ) ;
129+ }
130+ }
131+ catch ( NetworkInformationException )
132+ {
133+ // Interface does not support the IP address, ignore
127134 }
128135 }
129136
@@ -133,6 +140,7 @@ public static NetworkInterface GetBestNetworkInterface(IPAddress remoteAddress)
133140 public static async Task < ConnectionProfile ? > GetNetworkProfileForIpAddress ( IPAddress remoteAddress )
134141 {
135142 var networkInterface = GetBestNetworkInterface ( remoteAddress ) ;
143+
136144 var networkInterfaceGuid = Guid . Parse ( networkInterface . Id ) ;
137145 var adapters = NetworkInformation . GetConnectionProfiles ( )
138146 . Select ( existingProfile => existingProfile . NetworkAdapter ) . Distinct ( ) ;
0 commit comments