trying to Insert a document to a collection inside multithread function

private async Task Foo(Bar bar)
{
 Console.WriteLine("Inside Foo before insert");
_collection.Insert(session, bar);
Console.Writeline("Inside Foo after Insert");
}

this method is executed by several threads concurrently and i get to see the first log but not the one after Insert and Execution stops.
Same works if i try synchrounously.

Hi, @vishwa_nathan,

Welcome to the MongoDB Community Forums. I understand that you’re having an issue using the MongoDB .NET/C# Driver from async tasks.

You are calling _collection.Insert(session, bar) however the driver does not have an Insert method. The driver provides sync methods InsertOne and InsertMany as well as async methods InsertOneAsync and InsertManyAsync. If you are writing async code, you should be using InsertOneAsync and awaiting the result.

If you are still having problems, it would be helpful to know the driver version that you are using.

Sincerely,
James

3 Likes