Mongo.Migration icon indicating copy to clipboard operation
Mongo.Migration copied to clipboard

Value class Mongo.Migration.Documents.DocumentVersion cannot be deserialized.

Open festelo opened this issue 4 years ago • 12 comments

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)

festelo avatar Dec 27 '20 22:12 festelo

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 avatar Dec 27 '20 22:12 festelo

@festelo I have the same issue here, could you explain what you mean by "delay"?

kavatari avatar Aug 31 '21 14:08 kavatari

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.

kavatari avatar Sep 07 '21 11:09 kavatari

Sorry for late response. I will have a look at it.

Thank you for the feedback.

Cheers, Sean

SRoddis avatar Sep 28 '21 14:09 SRoddis

Please fix this asp.

Sacharkin avatar Dec 09 '21 10:12 Sacharkin

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.

samuelcadieux avatar May 24 '22 20:05 samuelcadieux

Any movement on this issue?

We are also getting it intermittently on .NET Core 6 application

Lukejkw avatar Sep 28 '22 11:09 Lukejkw

ning, on our end we had a background service accessing Mongo too early.

Are you able to elaborate on how you achieved this?

Lukejkw avatar Sep 28 '22 11:09 Lukejkw

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);

		// ...
	}
}

samuelcadieux avatar Oct 03 '22 14:10 samuelcadieux

@SRoddis any ideas what is causing this issue? Happy to contribute with a fix if pointed in the right direction.

Lukejkw avatar Oct 17 '22 12:10 Lukejkw

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.

Lukejkw avatar Oct 24 '22 07:10 Lukejkw

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);

char1es31 avatar Nov 30 '22 13:11 char1es31