我使用 .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/