【问题标题】:How can I use the MongoDB Driver with C# ASP.NET Core API framework?如何将 MongoDB 驱动程序与 C# ASP.NET Core API 框架一起使用?
【发布时间】:2017-12-19 05:26:36
【问题描述】:

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

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 框架

这是从方法调用中抛出的

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

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

是否有人对此有解决方案,因为我真的很想要一个可以在 Linux 而不是 Windows 上使用的应用程序?

【问题讨论】:

  • 您提到了 MongoDB 驱动程序 2.4,但您能否更具体地说明您的意思是哪个版本,2.4.0、2.4.1 等?

标签: c# asp.net .net mongodb


【解决方案1】:

在 .NET Core 2.0 上使用驱动程序绝对是可能的。您只需确保引用作为以下 NuGet 包的一部分提供的 .NET Standard 1.5 程序集:

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

另外,新版本 2.5 几天前刚刚发布,它可能对 MongoDB 3.6 最有效。

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

【讨论】:

  • 我重新阅读了该教程,谢谢。结果我遇到了驱动冲突,我的项目使用了不受支持的版本。
  • 我花了几个小时才到达那里。你添加的很棒的教程。谢谢。
猜你喜欢
  • 2018-01-13
  • 2015-04-16
  • 2021-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-29
  • 2017-05-17
相关资源
最近更新 更多