-
Type:
Bug
-
Resolution: Fixed
-
Priority:
Unknown
-
Affects Version/s: None
-
Component/s: None
-
None
-
None
-
Fully Compatible
-
C Drivers
-
Not Needed
-
-
None
-
None
-
None
-
None
-
None
-
None
.NET's standard `System.Net.IPAddress` class has an obscure nested subtype called `ReadOnlyIPAddress`, which is used for readonly static fields on `IPAddress` such as `IPAddress.Any`, `IPAddress.Loopback`, and others — see the source here.
The MongoDB.Driver's built-in `MongoDB.Bson.Serialization.Serializers.IPAddressSerializer` doesn't work for such instances. The minimal one-liner repro would be this:
IPAddress.Any.ToBson(BsonSerializer.LookupSerializer<IPAddress>())
Which when run currently throws a `BsonSerializationException` with the following message:
> An error occurred while serializing the ScopeId property of class System.Net.IPAddress: Operation not supported
This hints that the built-in `IPAddressSerializer` isn't even used in this case. I dug a little deeper and found out that the base class from which `IPAddressSerializer` derives (i.e. `ClassSerializerBase`) bypass the serialization method by default if the nominal type of the value isn't identical to its runtime type — see this.
This little quirk wasted a few hours of my time. The same issue existed a few years ago for .NET's `System.Text.Json`'s serializer, which was reported and subsequently fixed here. Please consider doing the same.