从 WebAPI 将 BSON 反序列化为客户端中的对象

Deserlize BSON into objects in client from WebAPI

我有一个网络 API,目前正在从 POST 返回 BSON 响应。

通常我有 WebAPI 返回 JSON,我只是在客户端中执行此操作以将其反序列化为我的一个或多个 C# 对象。

var result = await GetApiClient().PostAsJsonAsync("api/Test/GetImages", args);

if (result.IsSuccessStatusCode)
     return await result.Content.ReadAsAsync<T>();

我现在这样做是为了获得 BSON 响应:

MediaTypeFormatter bsonFormatter = new BsonMediaTypeFormatter();
var result = await GetApiClient().PostAsync("api/Test/GetImages", args, bsonFormatter);

    if (result.IsSuccessStatusCode)
         return await result.Content.ReadAsAsync<T>();

JSON没问题,但是读取内容时出现BSON错误:

No MediaTypeFormatter is available to read an object of type IEnumerable1 from content with media type 'application/bson'

我 100% 确定我会恢复 BSON,我只是不确定现在如何将它反序列化为对象?

我刚刚弄明白了,我需要在 ReadAsAsync 调用中包含格式化程序。比我预想的简单多了

ReadAsAsync<T>(new List<MediaTypeFormatter>() {bsonFormatter})