【问题标题】:Fetch untyped data from MongoDB gives "Unknown discriminator value"从 MongoDB 获取无类型数据给出“未知鉴别器值”
【发布时间】:2018-05-16 21:38:48
【问题描述】:

我们正在使用log4mongo-net 将 log4net 问题保存在 mongo db 中。 现在我们制作了一个简单的页面,我们可以通过从 mongo db 中读取未键入的日志,直接从浏览器中浏览日志。

但是,当日志中包含例如包含对象的 POST 时,此操作将失败,并显示“Unknown discriminator value 'SomeModel'”。

我们能否以某种方式告诉 Mongo 忽略 routedata->ActionArguments->model 的鉴别器,并将其反序列化为无类型?

我们正在使用mongocsharpdriver 2.4.3

这是我们如何获取数据的概念:

var _collection = MongoDbFactory.GetDatabase(_connectionString).GetCollection<object>(_collectionName);
var result = _mongoLogsRepository.Collection.Find(filter).ToList();

json数据:

{"_id":"5afbf3ec6f21aa0544647ea0","timestamp":"2018-05-16T09:03:40.492Z","level":"ERROR","thread":"19","logger":"X.Intranet.Common.Logging.Logger","message":"WebApi action failed","exceptiondata":{"type":"System.Data.Entity.Validation.DbEntityValidationException","message":"Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.","source":"EntityFramework","stackTrace":"   at System.Data.Entity.Internal.InternalContext.SaveChanges()\r\n   at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()\r\n   at System.Data.Entity.DbContext.SaveChanges()\r\n   at X.Intranet.Data.Contexts.XContext.SaveChanges()\r\n   at X.Intranet.Data.Handlers.ScheduleHandler.AddChange(ScheduleChange change)\r\n   at X.Intranet.Business.Managers.ScheduleManager.LogChange(Nullable`1 scheduleExceptionId, Nullable`1 scheduleExtraId, User currentUser, ScheduleChangeReason reason, Assistant fromAssistant, Assistant toAssistant, DateTimePeriodNullable oldTime, DateTimePeriodNullable newTime, Boolean vacant, Boolean noAssistance, String note)\r\n   at X.Intranet.Business.Managers.ScheduleManager.AddVacantAnswer(WorkingPeriod period, Boolean answer, String comment, User currentUser)\r\n   at X.Intranet.Ui.Areas.App.Controllers.Api.Assistant.AppScheduleVacanyController.Answer(Int32 clientId, AppScheduleVacantShiftRequest model)\r\n   at lambda_method(Closure , Object , Object[] )\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__0.MoveNext()"},"userdata":{"userName":"test.assistent"},"routedata":{"Method":"POST","RequestUrl":"https://xxx.azurewebsites.net:443/app/api/v1/assistant/appschedulevacany/answer?clientId=1786","Controller":"X.Intranet.Ui.Areas.App.Controllers.Api.Assistant.AppScheduleVacanyController","Action":"Answer","ActionArguments":{"clientId":1786,"model":{"_t":"AppScheduleVacantShiftRequest","ShiftItemId":"O:182270:2018-06-13 15:00","Answer":false,"Comment":null}},"ReferrerUrl":null},"data":null}

使用 BsonDocument 代替对象时的 Stacktrace:

在 MongoDB.Bson.BsonValue.System.IConvertible.ToType(Type conversionType, IFormatProvider provider 在 Newtonsoft.Json.JsonWriter.WriteValue(JsonWriter writer, PrimitiveTypeCode typeCode, Object value 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializePrimitive(JsonWriter 编写器,对象值,JsonPrimitiveContract 合同,JsonProperty 成员,JsonContainerContract containerContract,JsonProperty containerProperty 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,对象值,JsonContract valueContract,JsonProperty 成员,JsonContainerContract containerContract,JsonProperty containerProperty 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,对象值,JsonContract valueContract,JsonProperty 成员,JsonContainerContract containerContract,JsonProperty containerProperty 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter 编写器,IEnumerable 值,JsonArrayContract 合同,JsonProperty 成员,JsonContainerContract 集合合同,JsonProperty containerProperty 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,对象值,JsonContract valueContract,JsonProperty 成员,JsonContainerContract containerContract,JsonProperty containerProperty 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,对象值,JsonContract valueContract,JsonProperty 成员,JsonContainerContract containerContract,JsonProperty containerProperty 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter 编写器,IEnumerable 值,JsonArrayContract 合同,JsonProperty 成员,JsonContainerContract 集合合同,JsonProperty containerProperty 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,对象值,JsonContract valueContract,JsonProperty 成员,JsonContainerContract containerContract,JsonProperty containerProperty 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter 编写器,IEnumerable 值,JsonArrayContract 合同,JsonProperty 成员,JsonContainerContract 集合合同,JsonProperty containerProperty 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,对象值,JsonContract valueContract,JsonProperty 成员,JsonContainerContract containerContract,JsonProperty containerProperty 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter,对象值,类型 objectType 在 Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter,对象值,类型 objectType 在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding EffectiveEncoding 在 System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding EffectiveEncoding 在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(类型类型,对象值,流 writeStream,HttpContent 内容 在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(类型类型,对象值,流 writeStream,HttpContent 内容,TransportContext transportContext,CancellationToken cancelToken)---在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync --- 在 System. Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务 在 System.Web.Http.WebHost.HttpControllerHandler.d__1b.MoveNext()

提前致谢!

【问题讨论】:

    标签: c# mongodb mongodb-.net-driver mongodb-csharp-2.0


    【解决方案1】:

    以下应该有效:

    var _collection = MongoDbFactory.GetDatabase(_connectionString).GetCollection<BsonDocument>(_collectionName);
    

    【讨论】:

    • 我也尝试过使用 BsonDocument,但它似乎无法反序列化实体。获取“System.InvalidCastException”。 :/ 也许 log4mongo-net appender 对实体做了一些(不)有趣的事情。
    • 您能否将示例数据发布为 JSON 字符串(不是图像)?
    • 另外,你从哪里得到 InvalidCastException?你有 StackTrace 吗?
    • 这可能是由这个问题引起的:stackoverflow.com/questions/16651776/…
    • 感谢您的帮助,但仍然无法解决此问题。有空的时候会努力解决的。
    猜你喜欢
    • 1970-01-01
    • 2022-01-16
    • 2011-11-19
    • 1970-01-01
    • 2018-08-10
    • 2019-03-05
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    相关资源
    最近更新 更多