Mongo.Migration
Mongo.Migration copied to clipboard
Value class Mongo.Migration.Documents.DocumentVersion cannot be deserialized.
System.AggregateException: One or more errors occurred. (An error occurred while deserializing the Version property of class GoArcticApi.Models.Db.BaseCollectionObject: Value class Mongo.Migration.Documents.DocumentVersion cannot be deserialized.)
---> System.FormatException: An error occurred while deserializing the Version property of class GoArcticApi.Models.Db.BaseCollectionObject: Value class Mongo.Migration.Documents.DocumentVersion cannot be deserialized.
---> MongoDB.Bson.BsonSerializationException: Value class Mongo.Migration.Documents.DocumentVersion cannot be deserialized.
at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.Serializers.SerializerBase`1.MongoDB.Bson.Serialization.IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize(IBsonSerializer serializer, BsonDeserializationContext context)
at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.DeserializeMemberValue(BsonDeserializationContext context, BsonMemberMap memberMap)
This happened because of BackgroundService that tried to fetch db on start. After I put delay it works. But I think there should be a note in readme at least.
@festelo I have the same issue here, could you explain what you mean by "delay"?
Still looking for help with this issue. Found some interesting information, so the c# mongodb driver is not able to deserialize struct. DocumentVersion is a struct here.
Sorry for late response. I will have a look at it.
Thank you for the feedback.
Cheers, Sean
Please fix this asp.
This issue is still happening, on our end we had a background service accessing Mongo too early. We've found that awaiting on IHostApplicationLifetime.ApplicationStarted.WaitHandle
in our background service solves the concurrency issue.
Any movement on this issue?
We are also getting it intermittently on .NET Core 6 application
ning, on our end we had a background service accessing Mongo too early.
Are you able to elaborate on how you achieved this?
We're doing something like this:
using Microsoft.Extensions.Hosting;
internal static class WaitHandleExtensions
{
public static Task WaitOneAsync(this WaitHandle waitHandle, CancellationToken ct)
=> WaitOneAsync(waitHandle, TimeSpan.FromMilliseconds(-1), ct);
public static Task WaitOneAsync(this WaitHandle waitHandle, TimeSpan timeout, CancellationToken ct)
{
var tcs = new TaskCompletionSource<bool>();
var cancelRegistration = ct.Register(() => tcs.SetCanceled());
var waitRegistration = ThreadPool.RegisterWaitForSingleObject(waitHandle, (_, _) => tcs.TrySetResult(true), null, timeout, true);
var task = tcs.Task;
task.ContinueWith(_ =>
{
waitRegistration.Unregister(null);
cancelRegistration.Dispose();
}, CancellationToken.None);
return task;
}
}
internal class EventConsumer : BackgroundService
{
private readonly IHostApplicationLifetime m_hostLifetime;
public EventConsumer(IHostApplicationLifetime hostLifetime)
{
m_hostLifetime = hostLifetime;
}
protected override async Task ExecuteAsync(CancellationToken ct = default)
{
// Waits for the migration service to be ready
await m_hostLifetime.ApplicationStarted.WaitHandle.WaitOneAsync(ct)
.ConfigureAwait(false);
// ...
}
}
@SRoddis any ideas what is causing this issue? Happy to contribute with a fix if pointed in the right direction.
We're doing something like this:
using Microsoft.Extensions.Hosting; internal static class WaitHandleExtensions { public static Task WaitOneAsync(this WaitHandle waitHandle, CancellationToken ct) => WaitOneAsync(waitHandle, TimeSpan.FromMilliseconds(-1), ct); public static Task WaitOneAsync(this WaitHandle waitHandle, TimeSpan timeout, CancellationToken ct) { var tcs = new TaskCompletionSource<bool>(); var cancelRegistration = ct.Register(() => tcs.SetCanceled()); var waitRegistration = ThreadPool.RegisterWaitForSingleObject(waitHandle, (_, _) => tcs.TrySetResult(true), null, timeout, true); var task = tcs.Task; task.ContinueWith(_ => { waitRegistration.Unregister(null); cancelRegistration.Dispose(); }, CancellationToken.None); return task; } } internal class EventConsumer : BackgroundService { private readonly IHostApplicationLifetime m_hostLifetime; public EventConsumer(IHostApplicationLifetime hostLifetime) { m_hostLifetime = hostLifetime; } protected override async Task ExecuteAsync(CancellationToken ct = default) { // Waits for the migration service to be ready await m_hostLifetime.ApplicationStarted.WaitHandle.WaitOneAsync(ct) .ConfigureAwait(false); // ... } }
Thanks so much. The problem we've got is that we do not have direct control of the hosted service as it's a MassTransit service which coordinates the processing of messages off of queues.
There is no real way for us to tie into Mass Transit's lifecycle to delay the start up.
System.AggregateException: One or more errors occurred. (An error occurred while deserializing the Version property of class GoArcticApi.Models.Db.BaseCollectionObject: Value class Mongo.Migration.Documents.DocumentVersion cannot be deserialized.) ---> System.FormatException: An error occurred while deserializing the Version property of class GoArcticApi.Models.Db.BaseCollectionObject: Value class Mongo.Migration.Documents.DocumentVersion cannot be deserialized. ---> MongoDB.Bson.BsonSerializationException: Value class Mongo.Migration.Documents.DocumentVersion cannot be deserialized. at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) at MongoDB.Bson.Serialization.Serializers.SerializerBase`1.MongoDB.Bson.Serialization.IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize(IBsonSerializer serializer, BsonDeserializationContext context) at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.DeserializeMemberValue(BsonDeserializationContext context, BsonMemberMap memberMap)
We had this issue when we were not calling
MongoMigrationClient.Initialize(client);