Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
festelo opened this issue Dec 27, 2020 · 12 comments

Comments

@festelo
Copy link

festelo commented Dec 27, 2020

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
Copy link
Author

festelo commented Dec 27, 2020

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.

@kavatari
Copy link

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

@kavatari
Copy link

kavatari commented Sep 7, 2021

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.

@SRoddis
Copy link
Owner

SRoddis commented Sep 28, 2021

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

Thank you for the feedback.

Cheers, Sean

@Sacharkin
Copy link

Please fix this asp.

@samuelcadieux
Copy link

samuelcadieux commented May 24, 2022

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.

@Lukejkw
Copy link

Lukejkw commented Sep 28, 2022

Any movement on this issue?

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

@Lukejkw
Copy link

Lukejkw commented Sep 28, 2022

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

Are you able to elaborate on how you achieved this?

@samuelcadieux
Copy link

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

		// ...
	}
}

@Lukejkw
Copy link

Lukejkw commented Oct 17, 2022

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

@Lukejkw
Copy link

Lukejkw commented Oct 24, 2022

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.

@char1es31
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants