Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I am trying to create a web api application and that will fetch results from my already existing mongo database, but I am receiving this error when doing so

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 framework targeting .NET Core 2.0

This is thrown from the method call

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

Apparently from what I have read remoting is not supported in .NET Core as of yet, but on the MongoDB website (https://docs.mongodb.com/ecosystem/drivers/csharp/) it says that the driver version 2.4 is supported with .NET Core 2.0. I have installed the 2.4 driver version but I cannot seem to pull anything out of the DB using .Find.

Does anyone have a solution to this as I'd really like an application I can use on Linux rather than windows?

like image 848
jjmcc Avatar asked Dec 18 '17 19:12

jjmcc


1 Answers

Using the driver against .NET Core 2.0 is definitely possible. You just need to make sure that you reference the .NET Standard 1.5 assembly that comes as part of the following NuGet package:

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

Also, the new version 2.5 has just been released a few days ago which presumably works best against MongoDB 3.6.

You may want to read this tutorial, too: http://www.qappdesign.com/using-mongodb-with-net-core-webapi/

like image 69
dnickless Avatar answered Sep 20 '22 13:09

dnickless