Reputation: 56
I met System.IO.FileNotFoundException when using DLLs indirectly referenced.
A .NET framework 4.8 project references a .NET standard 2.0 project, and the .NET standard project references a NuGet package (Newtonsoft.Json). A .NET standard method uses a class in the NuGet package, and the method is called by the .NET framework project. When calling the method, System.IO.FileNotFoundException is thrown saying it can't load assembly Newtonsoft.Json.
Why does this happen and how can I fix it?
I added the NuGet package in the main .NET framework project and it worked, but I think this is not an elegant way to fix this problem.
In .NET standard:
public class Class1
{
public static void Run()
{
var x = new JsonSerializer();
}
}
In .NET framework:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Class1.Run();
}
}
Error message: System.IO.FileNotFoundException:“未能加载文件或程序集“Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed”或它的某一个依赖项。系统找不到指定的文件。”
(Translation: System.IO.FileNotFoundException:"Can't load file or assembly "Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed" or one of its dependencies. The system can't find the specified file."
Upvotes: 0
Views: 580
Reputation: 2047
If your projects are in the same solutions, and the reference from net 4.8 project to netstandard project is a ProjectReference , you can add this to a PropertyGroup inside your netstandard csproj file to enforce copying NuGet assemblies to the built output:
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
This will cause the dlls derived from a nuget package are copied in the output folder.
Building both net 4.8 and netstandard project, all dlls coming from net standard nuget package should be copied in the output folder.
Upvotes: 3