c# - .NetStandard 2.0,MongoDB 驱动程序 : FileNotFoundException for MongoDB. Bson

标签 c# mongodb

我使用 .Net Standard 2.0 创建了一个具有以下依赖项的新项目:

  • MongoDb.Driver
  • MongoDb.Driver.Core
  • MongoDb.Bson

但是,当我运行我的程序时,出现以下错误:

Unhandled Exception: System.IO.FileNotFoundException: Coult not load file or assembly "MongoDB.Bson, Version=2.7.2.0, Culture=neutral, PublicKeyToken=null" or one of its dependencies. The system cannot find the file specified.

我来自 Java 世界,这让我今天一整天都感到困惑。我尝试添加指定的所有依赖项 here包括驱动程序本身的依赖关系,没有任何运气。

  • System.ComponentModel.TypeConverter (>= 4.1.0)
  • System.Linq.Queryable (>= 4.0.1)

我做错了什么?

最佳答案

我创建了一个面向 .NET Standard 2.0 的库,它使用了 mongocsharpdriver 包,另一个 .NET Framework 4.6.1 项目又使用了该库(控制台应用程序)。

构建正常,但是在运行时出现了 FileNotFoundException。

这两个线程 https://github.com/dotnet/standard/issues/410这篇文章阐明了这个问题https://www.hanselman.com/blog/ReferencingNETStandardAssembliesFromBothNETCoreAndNETFramework.aspx

基本上解决方案是在控制台应用程序对应的 csproj 中添加一行:

I can open up the .csproj file for the Framework projects and add this line within the first like this to change the restore style:

<RestoreProjectStyle>PackageReference</RestoreProjectStyle>

再次构建后,MongoDB.*.dll文件被复制到输出文件夹。

希望这有助于并节省其他人的时间!

关于c# - .NetStandard 2.0,MongoDB 驱动程序 : FileNotFoundException for MongoDB. Bson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53565680/

相关文章:

node.js - MongoDB $text 搜索是如何工作的?

mongodb - Debezium MongoDB 连接器错误 : org. apache.kafka.connect.errors.ConnectException:错误处理程序中超出了容差

javascript - 在一个查询中为每个属性分组不同的值和计数

ios - 如何使用 Objective-C 初始化嵌套的 Node.js 模式

c# - 枚举案例处理 - 更好地使用开关或字典?

c# - 用 Linq 拼接 IEnumerable

c# - 使用 c# .net 将两个视频合二为一

mongodb - mongodb中索引和搜索数字字段

c# - 自动化单元测试 - 为什么?什么?哪个?

c# - MVC4忽略参数