Search code examples
c#mongodbcharacter-encoding

mongocsharpdriver DecoderFallbackException on an empty database


I've installed an instance of MongoDB on a local test server with the system language set to ko-KR. This was a simple install with a single instance running as a replica set. I was able to connect to it via the command line and run queries against the (empty) database.

However, when trying to connect to it using the C# driver (in order to populate it), an exception had been thrown with the following stacktrace:

MongoDB.Driver.MongoConnectionException: Unable to connect to a member of the replica set matching the read preference Primary: 지정한 코드 페이지의 -1 인덱스에서 바이트 [B1]을(를) 유니코드로 변환할 수 없습니다.. ---> System.Text.DecoderFallbackException: 지정한 코드 페이지의 -1 인덱스에서 바이트 [B1]을(를) 유니코드로 변환할 수 없습니다. 
위치: System.Text.DecoderExceptionFallbackBuffer.Throw(Byte[] bytesUnknown, Int32 index) 
위치: System.Text.DecoderExceptionFallbackBuffer.Fallback(Byte[] bytesUnknown, Int32 index) 
위치: System.Text.DecoderFallbackBuffer.InternalFallback(Byte[] bytes, Byte* pBytes) 
위치: System.Text.UTF8Encoding.GetCharCount(Byte* bytes, Int32 count, DecoderNLS baseDecoder) 
위치: System.String.CreateStringFromEncoding(Byte* bytes, Int32 byteLength, Encoding encoding) 
위치: System.Text.UTF8Encoding.GetString(Byte[] bytes, Int32 index, Int32 count) 
위치: MongoDB.Bson.IO.BsonBuffer.DecodeUtf8String(UTF8Encoding encoding, Byte[] buffer, Int32 index, Int32 count) 
위치: MongoDB.Bson.IO.BsonBuffer.ReadString(UTF8Encoding encoding) 
위치: MongoDB.Bson.IO.BsonBinaryReader.ReadString() 
위치: MongoDB.Bson.Serialization.Serializers.BsonStringSerializer.Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) 
위치: MongoDB.Bson.Serialization.Serializers.BsonBaseSerializer.Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) 
위치: MongoDB.Bson.Serialization.Serializers.BsonValueSerializer.Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) 
위치: MongoDB.Bson.Serialization.Serializers.BsonBaseSerializer.Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) 
위치: MongoDB.Bson.Serialization.Serializers.BsonArraySerializer.Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) 
위치: MongoDB.Bson.Serialization.Serializers.BsonBaseSerializer.Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) 
위치: MongoDB.Bson.Serialization.Serializers.BsonValueSerializer.Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) 
위치: MongoDB.Bson.Serialization.Serializers.BsonBaseSerializer.Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) 
위치: MongoDB.Bson.Serialization.Serializers.BsonDocumentSerializer.Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) 
위치: MongoDB.Bson.Serialization.Serializers.BsonBaseSerializer.Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) 
위치: MongoDB.Driver.CommandResultSerializer.Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) 
위치: MongoDB.Bson.Serialization.Serializers.BsonBaseSerializer.Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) 
위치: MongoDB.Driver.Internal.MongoReplyMessage`1.ReadBodyFrom(BsonBuffer buffer) 
위치: MongoDB.Driver.Internal.MongoConnection.ReceiveMessage[TDocument](BsonBinaryReaderSettings readerSettings, IBsonSerializer serializer, IBsonSerializationOptions serializationOptions) 
위치: MongoDB.Driver.Operations.CommandOperation`1.Execute(MongoConnection connection) 
위치: MongoDB.Driver.MongoServerInstance.RunCommandAs[TCommandResult](MongoConnection connection, String databaseName, IMongoCommand command) 
위치: MongoDB.Driver.MongoServerInstance.LookupServerInformation(MongoConnection connection) 
위치: MongoDB.Driver.MongoServerInstance.Connect() 
위치: MongoDB.Driver.Internal.MultipleInstanceMongoServerProxy.<>c__DisplayClassd.b__c(Object _) 
--- 내부 예외 스택 추적의 끝 --- 
위치: MongoDB.Driver.Internal.MultipleInstanceMongoServerProxy.ThrowConnectionException(ReadPreference readPreference) 
위치: MongoDB.Driver.Internal.MultipleInstanceMongoServerProxy.Connect(TimeSpan timeout, ReadPreference readPreference) 
위치: MongoDB.Driver.Internal.MultipleInstanceMongoServerProxy.ChooseServerInstance(ReadPreference readPreference) 
위치: MongoDB.Driver.MongoServer.AcquireConnection(ReadPreference readPreference) 
위치: MongoDB.Driver.MongoCursor`1.MongoCursorConnectionProvider.AcquireConnection() 
위치: MongoDB.Driver.Operations.QueryOperation`1.GetFirstBatch(IConnectionProvider connectionProvider) 
위치: MongoDB.Driver.Operations.QueryOperation`1.Execute(IConnectionProvider connectionProvider) 
위치: MongoDB.Driver.MongoCursor`1.GetEnumerator() 
<Application Call>

The Korean exception translates roughly to: Byte [B1] at index -1 of the specified text could not be converted to Unicode.

I've found a result on google which says this is potentially a data issue, but there is no data to be poorly encoded, so that can't be it.

Why is the C# driver throwing?


Solution

  • I've found the problem to be the host name. The server's machine name was in Korean, and presumably encoded in something other than UTF8.

    Once I've changed the server's name, I was able to connect in code.