如何将MongoDB驱动程序与C#ASP.NET Core API框架一起使用? [英] How can I use the MongoDB Driver with C# ASP.NET Core API framework?

查看:186
本文介绍了如何将MongoDB驱动程序与C#ASP.NET Core API框架一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个Web api应用程序,它将从我已经存在的mongo数据库中获取结果,但是这样做时我收到此错误

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)

我目前有一个针对.NET Core 2.0的ASP.NET Core框架

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

显然,.NET Core目前尚不支持远程读取,但是在MongoDB网站上( https://docs.mongodb.com/ecosystem/drivers/csharp/),它表示.NET Core 2.0支持驱动程序版本2.4.我已经安装了2.4驱动程序版本,但似乎无法使用.Find从数据库中提取任何内容.

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.

有人对此有解决方案吗,因为我真的很希望可以在Linux而不是Windows上使用的应用程序?

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

推荐答案

绝对可以使用针对.NET Core 2.0的驱动程序.您只需要确保引用以下NuGet软件包中随附的.NET Standard 1.5程序集即可:

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

另外,几天前刚刚发布了新版本2.5,该版本可能最适合MongoDB 3.6.

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

您可能也想阅读本教程: http: //www.qappdesign.com/using-mongodb-with-net-core-webapi/

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

这篇关于如何将MongoDB驱动程序与C#ASP.NET Core API框架一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆