简体   繁体   English

从WCF数据服务返回BSonDocuments

[英]Returning BSonDocuments from a WCF Data Service

I am using the official MongoDB C# driver and I am trying to find a way to deserialize a BSonDocument into a class. 我正在使用官方的MongoDB C#驱动程序,并且试图找到一种将BSonDocument反序列化为类的方法。

The thing is that I want this to be generic (without having to create manually beforehand). 问题是我希望它是通用的(不必事先手动创建)。 I need to do this because I am building a WCF data service with a ServiceOperation that will return documents in my mongo database. 我需要这样做,因为我正在使用ServiceOperation构建WCF数据服务,该服务将返回mongo数据库中的文档。 The documents returned can be anything so they won't necessarily fit a certain class structure. 返回的文档可以是任何东西,因此它们不一定适合特定的类结构。

Unfortunately, I cannot just return a list of BSonDocuments as my service complains that 不幸的是,我不能仅仅返回BSonDocuments列表,因为我的服务抱怨

The type 'MongoDB.Bson.BsonDocument' is not a complex type or an entity type.

Any of you faced a similar problem? 你们中有人遇到过类似的问题吗? If so what do you recommend? 如果是这样,您有什么建议?

You could convert your BsonDocument to a JSON string and return that and then parse it back at the receiving end. 您可以将BsonDocument转换为JSON字符串,然后将其返回,然后在接收端将其解析回去。

At the server: 在服务器上:

var json = document.ToJson();
return json;

At the client: 在客户处:

var document = BsonSerializer.Deserialize<BsonDocument>(json);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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