我正在嘗試從我的 MongoDB 資料庫中檢索物件串列。我必須使用 BsonDocument 在記錄上存盤額外的動態資料,但是當我嘗試使用最少的 API 將串列回傳到瀏覽器時,我收到以下錯誤。
InvalidCastException: Unable to cast object of type 'MongoDB.Bson.BsonString' to type 'MongoDB.Bson.BsonBoolean'.
AsBooleanGetter(object )
System.Text.Json.Serialization.Metadata.JsonPropertyInfo<T>.GetMemberAndWriteJson(object obj, ref WriteStack state, Utf8JsonWriter writer)
System.Text.Json.Serialization.Converters.ObjectDefaultConverter<T>.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.JsonConverter<T>.TryWrite(Utf8JsonWriter writer, ref T value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.Metadata.JsonPropertyInfo<T>.GetMemberAndWriteJson(object obj, ref WriteStack state, Utf8JsonWriter writer)
System.Text.Json.Serialization.Converters.ObjectDefaultConverter<T>.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.JsonConverter<T>.TryWrite(Utf8JsonWriter writer, ref T value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.Converters.IEnumerableDefaultConverter<TCollection, TElement>.OnWriteResume(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.JsonCollectionConverter<TCollection, TElement>.OnTryWrite(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.JsonConverter<T>.TryWrite(Utf8JsonWriter writer, ref T value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.Metadata.JsonPropertyInfo<T>.GetMemberAndWriteJson(object obj, ref WriteStack state, Utf8JsonWriter writer)
System.Text.Json.Serialization.Converters.ObjectDefaultConverter<T>.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.JsonConverter<T>.TryWrite(Utf8JsonWriter writer, ref T value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.Converters.ListOfTConverter<TCollection, TElement>.OnWriteResume(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.JsonCollectionConverter<TCollection, TElement>.OnTryWrite(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.JsonConverter<T>.TryWrite(Utf8JsonWriter writer, ref T value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.JsonConverter<T>.WriteCore(Utf8JsonWriter writer, ref T value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.JsonConverter<T>.WriteCoreAsObject(Utf8JsonWriter writer, object value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.JsonSerializer.WriteCore<TValue>(JsonConverter jsonConverter, Utf8JsonWriter writer, ref TValue value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.JsonSerializer.WriteStreamAsync<TValue>(Stream utf8Json, TValue value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken)
System.Text.Json.JsonSerializer.WriteStreamAsync<TValue>(Stream utf8Json, TValue value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken)
System.Text.Json.JsonSerializer.WriteStreamAsync<TValue>(Stream utf8Json, TValue value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken)
Microsoft.AspNetCore.Http.HttpResponseJsonExtensions.WriteAsJsonAsyncSlow<TValue>(Stream body, TValue value, JsonSerializerOptions options, CancellationToken cancellationToken)
Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
我已經嘗試在物件上使用 BsonExtraElements 屬性(如docs)并得到相同的結果。
我已經把一個最小的樣品在Github這里。我的基礎物件如下所示:
public class BaseCollection
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public ObjectId Id { get; set; }
public BsonDocument Metadata { get; set; }
}
我在檔案上的唯一記錄是:
{"_id":{"$oid":"61cb03e65b7cd42eedf7a36b"},"Metadata":{"name":"Mauricio","lastname":"Sipmann"}}
我正在查詢以下內容
context.GetCollection("teste").Find(new BsonDocument()).ToList<BaseCollection>()
如果我描述 BaseCollection 類中的每個欄位,則輸出有效。
其他詳細資訊。.net 6 OSx MongoDB 云
uj5u.com熱心網友回復:
你想回傳什么?如果你改變你的“去”路線
app.MapGet("/go", async (IMongoDbContext context) => {
var result = await context.GetCollection("teste").FindAsync(new BsonDocument());
var list = await result.ToListAsync();
return list.ToJson();
});
你可能不會得到這個錯誤。FindAsync 回傳一些 IEnumerable,因此您必須從中獲取串列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/396453.html
上一篇:記一次有手就行的getshell
