MongoDb .NET驱动程序System.FormatException / DecoderFallbackException
反序列化description属性时发生错误 类KerbToolsService.Models.promotion: 无法在索引136处转换字节[ED] [A0] 指定的代码页为Unicode。
我编写的一个.NET Web API应用程序,用于监视基于MongoDB / Node.js的系统的状态,开始抛出DecoderFallbackException。
这是一个堆栈跟踪
System.FormatException occurred
HResult=-2146233033
Message=An error occurred while deserializing the description property of class KerbToolsService.Models.promotion: Unable to translate bytes [ED][A0] at index 136 from specified code page to Unicode.
Source=MongoDB.Bson
StackTrace:
at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.DeserializeMemberValue(BsonDeserializationContext context, BsonMemberMap memberMap)
InnerException: System.Text.DecoderFallbackException
HResult=-2147024809
Message=Unable to translate bytes [ED][A0] at index 136 from specified code page to Unicode.
Source=mscorlib
Index=136
StackTrace:
at System.Text.DecoderExceptionFallbackBuffer.Throw(Byte[] bytesUnknown, Int32 index)
at System.Text.DecoderExceptionFallbackBuffer.Fallback(Byte[] bytesUnknown, Int32 index)
at System.Text.DecoderFallbackBuffer.InternalFallback(Byte[] bytes, Byte* pBytes)
at System.Text.UTF8Encoding.GetCharCount(Byte* bytes, Int32 count, DecoderNLS baseDecoder)
at System.String.CreateStringFromEncoding(Byte* bytes, Int32 byteLength, Encoding encoding)
at System.Text.UTF8Encoding.GetString(Byte[] bytes, Int32 index, Int32 count)
at MongoDB.Bson.IO.Utf8Helper.DecodeUtf8String(Byte[] bytes, Int32 index, Int32 count, UTF8Encoding encoding)
at MongoDB.Bson.IO.ByteBufferStream.ReadString(UTF8Encoding encoding)
at MongoDB.Bson.IO.BsonBinaryReader.ReadString()
at MongoDB.Bson.Serialization.Serializers.StringSerializer.DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.Serializers.SealedClassSerializerBase`1.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.Serializers.SerializerBase`1.MongoDB.Bson.Serialization.IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize(IBsonSerializer serializer, BsonDeserializationContext context)
at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.DeserializeMemberValue(BsonDeserializationContext context, BsonMemberMap memberMap)
到目前为止,我知道在bson中编码为U+D83D
的字符ED A0 BD
位于数据库的文档中,并导致.NET UTF8Encoding抛出DecoderFallbackException。其他代码(MongoLab UI和基于Node.js的应用程序)将其替换为?
,但它会在.NET驱动程序中终止查询。
我想让.NET Mongo驱动程序的行为与其他驱动程序相同,如果它看到无效的UTF-8,则不会爆炸。
答案 0 :(得分:4)
尝试以这种方式在MongoClient上设置ReadEncoding属性:
MongoClient client = new MongoClient(new MongoClientSettings()
{
Server = new MongoServerAddress("<serveraddress>", 27017),
ReadEncoding = new System.Text.UTF8Encoding(false, false)
});
(编辑:正如@Craig Wilson所说,设置只是在构造函数之后)
UTF8Encoding的第二个参数是“throwOnInvalidBytes”:
true 指定在检测到无效编码时抛出异常;否则, false
来源:https://jira.mongodb.org/browse/CSHARP-996和https://msdn.microsoft.com/en-us/library/302sbf78(v=vs.110).aspx