简体   繁体   中英

How to create a BsonDocument from stream (c#)

I want to insert an email read from a server to a MongoDB. I use MimeKit to get the emails from the server. Trying to insert it directly mimeMessage.ToBsonDocument() I get System.InvalidOperationException: 'Timeouts are not supported on this stream.' So I'm guessing that the message object uses an internal stream to read the data from the server.

I now want to use a memory stream to first write the data to a stream and then create a BsonDocument from that stream.

I read here that the best way to do this is with BsonDocument.ReadFrom(ms); but the compiler cannot find the method 'ReadFrom' in BsonDocument.

Was this method replaced in newer versions? With what? Or what is the best way to create a BsonDocument from a stream?

Thanks

Based on the documentation at http://api.mongodb.com/csharp/2.0/html/Methods_T_MongoDB_Bson_BsonDocument.htm , it seems you need to convert the stream into a string and then use the Parse() method:

string bsonText;
using (var reader = new StreamReader (ms))
    bsonText = reader.ReadToEnd ();

var bsonDocument = BsonDocument.Parse (bsonText);

That said, I don't think that you can just write the MimeMessage to a stream and then expect to parse that into a BsonDocument . When you write a MimeMessage to a stream, it will be in MIME format, not BSON format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM