gpt4 book ai didi

c# - 如何停止 Mongodb .NET 驱动程序抛出 System.FormatException?

转载 作者:可可西里 更新时间:2023-11-01 10:00:36 27 4
gpt4 key购买 nike

MongoDb .NET 驱动程序 System.FormatException/DecoderFallbackException

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.

我编写的用于监视基于 MongoDB/Node.js 的系统状态的 .NET Web API 应用开始抛出 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 中编码为 ED A0 BD 的字符 U+D83D 位于数据库的文档中,并且导致 .NET UTF8Encoding 到抛出 DecoderFallbackException。其他代码(MongoLab UI 和我们基于 Node.js 的应用程序)将其替换为 ? 但它会终止 .NET 驱动程序中的查询。

我想让 .NET Mongo 驱动程序的行为与其他驱动程序相同,并且在看到无效的 UTF-8 时不会崩溃。

最佳答案

尝试以这种方式在 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 to specify that an exception be thrown when an invalid encoding is detected; otherwise, false

来源:https://jira.mongodb.org/browse/CSHARP-996https://msdn.microsoft.com/en-us/library/302sbf78(v=vs.110).aspx

关于c# - 如何停止 Mongodb .NET 驱动程序抛出 System.FormatException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33134616/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com