Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

c# - Converting between MongoDB.Bson versions

I have an application writing BsonDocuments created by MongoDB C# driver (v1.10.xxx) as byte array to RabbitMQ. Then I have another application which was using the same driver to read and convert the byte representation of the BsonDocument back to it's object form.

Then, I needed to upgrade the MongoDB C# driver to version 2.3, now I can't read back the bytes waiting in RabbitMQ queue back to BsonDocument, the structure remains but the values are gone.

Does anyone have an idea for that?

The code responsible for writing to RabbitMQ:

BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
    bf.Serialize(ms, prm_xBsonDocument);
    lock (m_xChannelMessageTrace)
    {
        m_xChannelMessageTrace.QueueBind(m_strQueueNameMessageTrace, m_strExchangeNameCommunication, prm_TraceType.ToString(), null);
        m_xChannelMessageTrace.BasicPublish(m_strExchangeNameCommunication, prm_TraceType.ToString(), null, ms.ToArray());
    }
}

For reading I use:

var consumer = new EventingBasicConsumer((IModel)property);
consumer.Received += (model, ea) =>
{
    var ms = new MemoryStream(ea.Body);
 
    try
    {
        var formatter = new BinaryFormatter();
        BsonDocument xBsonDocument = ((BsonDocument)formatter.Deserialize(ms));

Writer app MongoDB C# driver version: v1.10.0.62

Reader app MongoDB C# driver version: v2.3.0.157

The RabbitMQ record starts like:

????fQMongoDB.Bson, Version=1.10.0.62, Culture=neutral, PublicKeyToken=f686731cfb9cc103MongoDB.Bson.BsonDocument_elements_indexes_allowDuplicateNamesBsonValue+bsonType?System.Collections.Generic.List1[[MongoDB.Bson.BsonElement, MongoDB.Bson, Version=1.10.0.62, Culture=neutral, PublicKeyToken=f686731cfb9cc103]]?System.Collections.Generic.Dictionary2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]MongoDB.Bson.BsonType????MongoDB.Bson.BsonTypeavalue_?System.Collections.Generic.List`1[[MongoDB.Bson.BsonElement, MongoDB.Bson, Version=1.10.0.62, Culture=neutral,...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...