How can I use the MongoDB driver with the C # ASP.NET Core API?

I am trying to create a web api application that will retrieve the results from an existing mongo database, but I get this error while doing this

Could not load type 'System.Runtime.Remoting.Messaging.CallContext' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

An unhandled exception occurred while processing the request.
TypeLoadException: Could not load type 'System.Runtime.Remoting.Messaging.CallContext' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
MongoDB.Driver.Core.Events.EventContext+AsyncLocal.get_Value()

MongoDB.Driver.Core.Events.EventContext+AsyncLocal.get_Value()
MongoDB.Driver.Core.Events.EventContext.BeginOperation(Nullable<long> operationId)
MongoDB.Driver.Core.Operations.FindCommandOperation.Execute(IReadBinding binding, CancellationToken cancellationToken)
MongoDB.Driver.Core.Operations.FindOperation.Execute(IReadBinding binding, CancellationToken cancellationToken)
MongoDB.Driver.OperationExecutor.ExecuteReadOperation<TResult>(IReadBinding binding, IReadOperation<TResult> operation, CancellationToken cancellationToken)
MongoDB.Driver.MongoCollectionImpl.ExecuteReadOperation<TResult>(IClientSessionHandle session, IReadOperation<TResult> operation, ReadPreference readPreference, CancellationToken cancellationToken)
MongoDB.Driver.MongoCollectionImpl.ExecuteReadOperation<TResult>(IClientSessionHandle session, IReadOperation<TResult> operation, CancellationToken cancellationToken)
MongoDB.Driver.MongoCollectionImpl.FindSync<TProjection>(IClientSessionHandle session, FilterDefinition<TDocument> filter, FindOptions<TDocument, TProjection> options, CancellationToken cancellationToken)
MongoDB.Driver.MongoCollectionImpl+<>c__DisplayClass35_0.<FindSync>b__0(IClientSessionHandle session)
MongoDB.Driver.MongoCollectionImpl.UsingImplicitSession<TResult>(Func<IClientSessionHandle, TResult> func, CancellationToken cancellationToken)
MongoDB.Driver.MongoCollectionImpl.FindSync<TProjection>(FilterDefinition<TDocument> filter, FindOptions<TDocument, TProjection> options, CancellationToken cancellationToken)
MongoDB.Driver.FindFluent.ToCursor(CancellationToken cancellationToken)

I currently have an ASP.NET Core platform focused on .NET Core 2.0

This is selected from a method call.

var documents = collection.Find(_ => true).ToList();

Apparently, from what I read, the remote connection in .NET Core is not yet supported, but the MongoDB website ( https://docs.mongodb.com/ecosystem/drivers/csharp/ ) says that the driver version 2.4 is supported by .NET Core 2.0. I installed the driver version 2.4, but I can’t get anything out of the database using .Find.

Does anyone have a solution for this since I really like the application that I can use on Linux and not Windows?

+4
1

.NET Core 2.0, , . , .NET Standard 1.5, NuGet:

https://www.nuget.org/packages/MongoDB.Driver

, 2.5, , -, MongoDB 3.6.

, : http://www.qappdesign.com/using-mongodb-with-net-core-webapi/

+2

Source: https://habr.com/ru/post/1690990/


All Articles