(System.IO.FileNotFoundException) 通过“添加引用"添加 DLL 时,.Net 核心 Web API 无法找到子依赖项(依赖项的依赖项); [英] (System.IO.FileNotFoundException) .Net core Web API cannot find child dependency (Dependencies of dependency) when added DLL by "Add Reference"

查看:38
本文介绍了(System.IO.FileNotFoundException) 通过“添加引用"添加 DLL 时,.Net 核心 Web API 无法找到子依赖项(依赖项的依赖项);的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .Net Core Web API 中遇到 System.IO.FileNotFoundException.所以我建立了下面的项目来演示这个问题.

我创建了一个名为 DemoLibrary 的 .Net Standard 库,并通过 NuGet 添加了 QRCoder 依赖项.

免责声明:选择 QRCoder 的原因是 Web API 默认不使用它.我没有在我的项目中使用它.事实上,我收到了 EntityFrameworkCore 的这个异常.

我创建了一个没有其他依赖项的新 .Net Core Web API DemoWebAPI.

然后通过添加引用 -> 浏览 -> DemoLibrary.dll 将 DemoLibrary 添加到 DemoWebAPI.

这是我的解决方案:

Calculate 类中的DemoMethod 方法只是创建了QRCodeGenerator 的对象.

公共类计算{公共静态字符串 DemoMethod(){QRCodeGenerator qrGenerator = new QRCodeGenerator();返回 "";}}

而我在 DemoWebAPI 中的 ValuesController 只是调用了该方法:

[HttpGet]公共 ActionResult>得到(){return new string[] { "value1", "value2", DemoLibrary.Calculate.DemoMethod() };}

现在,当我运行 DemoWebAPI 项目时,我在调用 DemoMethod 时遇到以下异常:

<块引用>

System.IO.FileNotFoundException: '无法加载文件或程序集'QRCoder,版本=1.3.5.0,Culture=neutral,PublicKeyToken=null'.系统找不到指定的文件.'

我了解我必须将 QRCoder.dll 文件复制到某处的事实.但我不明白把它放在哪里.我已经尝试将它放在 DemoWebAPI 的 "bin/debug/netcoreapp2.2" 和 DemoLibrary 的 "bin/debug/netstandard2.0" 中.>

但我无法让它工作.

<块引用>

请求:请尽可能详细地发布您的答案,因为我是 .Net Core 的新手.

编辑:我知道 NuGet 服务器.我已阅读诸如在 IIS 和 Azure 中托管 NuGet 服务器之类的主题.DLL 引用背后的原因是我想在两个项目中使用我的 DLL,其中一个是 .net 核心 API,另一个是由 NMAKE 编译的 .net 框架类库.我找不到任何方法来恢复 .MAK 文件中的 NuGet 包.

解决方案

看起来您只是将 DemoLibrary 的 DLL 添加到您的 DemoWebApi 项目中.这不是您应该添加引用的方式.由于它们在同一个解决方案中,您应该添加一个项目引用.这将解决您的问题.

现在,让我解释一下这里到底发生了什么.您的 DemoLibrary 依赖于 QRCoder.这是一个 NuGet 引用,这意味着包将被恢复(即下载)并包含在您的 DemoLibrary 构建输出中.但是,它将作为一个或多个 DLL 包含在您的 DemoLibrary.dll 旁边.然后,当您只引用 DemoLibrary.dll 时,您会遗漏属于 DemoLibrary 的所有其他 DLL,因此,事情无法正常工作.

现在,当涉及到项目参考时,事情稍微复杂一些.项目引用实质上是将引用的项目包装到您的其他项目中.您可以将其视为一种子项目.出于所有意图和目的,就像子项目的任何依赖项都变成了主项目的依赖项.这意味着 DemoWebAPI 现在在技术上具有对 QRCoder 的 NuGet 包引用,即使其项目文件中没有显式包引用.依赖项来自您的 DemoLibary 项目.因此,通过项目引用,将包含所有必要的依赖项,因为就好像主项目通过子项目包含这些本身一样.

就其价值而言,您实际上永远不应该直接包含 DLL 作为参考.这曾经是必需的,但 NuGet 包的概念几乎消除了这种做法.即使 DemoLibraryDemoWebAPI 不在同一个解决方案中(意味着你不能再做项目引用),正确的使用方法是将 DemoLibary 到 NuGet 包中,然后通过包引用在 DemoWebAPI 中引用它,就像任何其他 NuGet 包一样.您不是简单地添加 DLL.

I am getting System.IO.FileNotFoundException in my .Net Core Web API. So I've set up the below project to demonstrate the problem.

I created a.Net Standard library named DemoLibrary and added QRCoder dependency via NuGet.

Disclaimer: Reason for choosing the QRCoder is that the Web API doesn't use it by default. I don't use it in my project. In fact, I'm getting this exception for EntityFrameworkCore.

I created a new .Net Core Web API DemoWebAPI which has no other dependencies.

Then added the DemoLibrary to DemoWebAPI via Add Reference -> Browse -> DemoLibrary.dll.

This is my solution:

The DemoMethod method in Calculate class just creates the object of QRCodeGenerator.

public class Calculate
{
    public static string DemoMethod()
    {
        QRCodeGenerator qrGenerator = new QRCodeGenerator();
        return "";
    }
}

And my ValuesController in DemoWebAPI just calls the method:

[HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
        return new string[] { "value1", "value2", DemoLibrary.Calculate.DemoMethod()  };
    }

Now, when I run the DemoWebAPI project I get below exception upon the call to the DemoMethod:

System.IO.FileNotFoundException: 'Could not load file or assembly 'QRCoder, Version=1.3.5.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.'

I understand the fact that I have to copy the QRCoder.dll file somewhere. But I fail to understand where to put it. I've already tried putting it in "bin/debug/netcoreapp2.2" of the DemoWebAPI and "bin/debug/netstandard2.0" of the DemoLibrary.

But I couldn't get it working.

Request: Please post your answer as descriptive as you can because I am new to .Net Core.

Edit: I am aware of the NuGet servers. I have read topics like hosting a NuGet server in IIS and Azure. The reason behind DLL reference is I want to use my DLLs in two projects one of them is a .net core API and the other is .net framework class library which is compiled by NMAKE. I couldn't find any way to restore NuGet packages in the .MAK files.

解决方案

It looks like you've merely added the DLL for DemoLibrary to your DemoWebApi project. That's not how you should be adding references. Since these are in the same solution, you should add a project reference. That will fix your issue.

Now, let me explain what's actually going on here. Your DemoLibrary has a dependency on QRCoder. It's a NuGet reference, which means that package will be restored (i.e. downloaded) and included in your DemoLibrary build output. However, it will be included as one or more DLLs along side your DemoLibrary.dll. When you then just reference DemoLibrary.dll, you're missing all these other DLLs that are part of DemoLibrary and thus, things don't work properly.

Now, when it comes to a project reference, things are little more complex. A project reference essentially wraps the referenced project into your other project. You can think of it as sort of a sub project. For all intents and purposes, it's like any dependency of the sub project becomes a dependency of the main project. That means that DemoWebAPI now technically has a NuGet package reference to QRCoder even though there's no explicit package reference in its project file. The dependency comes from your DemoLibary project. As such, with a project reference, all the necessary dependencies will be included, because it's as if the main project included those itself, by way of the sub project.

For what it's worth, you should virtually never include a DLL as a reference directly. That used to be required, but the concept of NuGet packages has all but eliminated the practice. Even if DemoLibrary was not in the same solution as DemoWebAPI (meaning you could no longer do a project reference), the correct way to use it would be to turn DemoLibary into a NuGet package, and then reference it in DemoWebAPI via a package reference, like any other NuGet package. You do not simply add the DLL.

这篇关于(System.IO.FileNotFoundException) 通过“添加引用"添加 DLL 时,.Net 核心 Web API 无法找到子依赖项(依赖项的依赖项);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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