FileNotFoundException 与间接(.net 到 .net 标准到 NuGet)引用的 DLL [英] FileNotFoundException with indirectly (.net to .net standard to NuGet) referenced DLL

查看:24
本文介绍了FileNotFoundException 与间接(.net 到 .net 标准到 NuGet)引用的 DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用间接引用的 DLL 时遇到了 System.IO.FileNotFoundException.

I met System.IO.FileNotFoundException when using DLLs indirectly referenced.

.NET 框架 4.8 项目引用了 .NET 标准 2.0 项目,.NET 标准项目引用了 NuGet 包 (Newtonsoft.Json)..NET 标准方法使用 NuGet 包中的一个类,该方法由 .NET 框架项目调用.调用该方法时,抛出 System.IO.FileNotFoundException ,表示无法加载程序集 Newtonsoft.Json.

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?

我在主 .NET 框架项目中添加了 NuGet 包并且它起作用了,但我认为这不是解决此问题的优雅方法.

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.

在 .NET 标准中:

In .NET standard:

    public class Class1
    {
        public static void Run()
        {
            var x = new JsonSerializer();
        }
    }

在 .NET 框架中:

In .NET framework:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            Class1.Run();
        }
    }

IO.FileNotFoundException:NewError 加载文件或程序集"NewErrortonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed"或者它的某个依赖项.系统查找不到指定的文件."

Error message: System.IO.FileNotFoundException:"未能加载文件或程序集"Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed"或它的某一个依赖项。系统找不到指定的文件。"

(翻译:System.IO.FileNotFoundException:无法加载文件或程序集Newtonsoft.Json,版本=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."

推荐答案

如果您的项目在相同的解决方案中,并且从 net 4.8 项目到 netstandard 项目的引用是 ProjectReference ,您可以将其添加到您的 netstandard 中的 PropertyGroupcsproj 文件以强制将 NuGet 程序集复制到构建的输出:

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>

这将导致从 nuget 包派生的 dll 被复制到输出文件夹中.

This will cause the dlls derived from a nuget package are copied in the output folder.

同时构建 net 4.8 和 netstandard 项目,所有来自 net standard nuget 包的 dll 都应复制到输出文件夹中.

Building both net 4.8 and netstandard project, all dlls coming from net standard nuget package should be copied in the output folder.

这篇关于FileNotFoundException 与间接(.net 到 .net 标准到 NuGet)引用的 DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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