Bug 41897 - NotSupportedException thrown from IPInterfaceProperties.UnicastAddresses
Summary: NotSupportedException thrown from IPInterfaceProperties.UnicastAddresses
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: master
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
Depends on:
Reported: 2016-06-16 14:04 UTC by Mike Voorhees
Modified: 2016-06-21 14:04 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Mike Voorhees 2016-06-16 14:04:55 UTC
Test Case :

foreach (var localAdapter in NetworkInterface.GetAllNetworkInterfaces())
	var ipProperties = localAdapter.GetIPProperties();
	foreach (var localAddress in ipProperties.UnicastAddresses)

Console.WriteLine("All Good!");

Results in exception :

Unhandled Exception:
System.NotSupportedException: The collection is read-only.
  at System.Net.NetworkInformation.UnicastIPAddressInformationCollection.Add (System.Net.NetworkInformation.UnicastIPAddressInformation address) <0x2aa5018 + 0x00037> in <filename unknown>:0
  at System.Net.NetworkInformation.Win32IPInterfaceProperties2.Win32FromUnicast (System.Int32 ifIndex, System.IntPtr ptr) <0x2aa4c20 + 0x0013e> in <filename unknown>:0
  at System.Net.NetworkInformation.Win32IPInterfaceProperties2.get_UnicastAddresses () <0x2aa4388 + 0x00073> in <filename unknown>:0
  at DateTimeProblem.Program.Main (System.String[] args) <0x2aa11a8 + 0x00050> in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: Nested exception trying to figure out what went wrong


UnicastIPAddressInformationCollection is coming from reference source now, while Win32IPInterfaceProperties2 is a mono implementation.  Looks like the assumption made by the following code that it can call Add on UnicastIPAddressInformationCollection is no longer true.

private static UnicastIPAddressInformationCollection Win32FromUnicast(int ifIndex, IntPtr ptr)
	UnicastIPAddressInformationCollection unicastIPAddressInformationCollection = new UnicastIPAddressInformationCollection();
	IntPtr intPtr = ptr;
	while (intPtr != IntPtr.Zero)
		unicastIPAddressInformationCollection.Add(new Win32UnicastIPAddressInformation(ifIndex, info));
		intPtr = info.Next;
	return unicastIPAddressInformationCollection;
Comment 1 Marek Safar 2016-06-21 14:04:07 UTC
Fixed in master and Mono 4.5.1