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

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

问题描述

我的.Net Core Web API中出现 System.IO.FileNotFoundException 。因此,我设置了以下项目来演示该问题。



我创建了一个名为 DemoLibrary 的.Net标准库,并添加了通过NuGet获得QRCoder 依赖。



免责声明:选择QRCoder的原因是Web API默认不使用它。我不在项目中使用它。实际上,我正在获得EntityFrameworkCore的异常。



我创建了一个新的.Net Core Web API DemoWebAPI



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



这是我的解决方案:





 公共类Calculate类中的DemoMethod方法仅创建QRCodeGenerator的对象。公共类计算
{
public static字符串DemoMethod()
{
QRCodeGenerator qrGenerator = new QRCodeGenerator();
返回;
}
}

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

  [HttpGet] 
public ActionResult< IEnumerable< string>> Get()
{
返回新字符串[] { value1, value2,DemoLibrary.Calculate.DemoMethod()};
}

现在,当我运行DemoWebAPI项目时,在调用DemoMethod:


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


我了解必须复制 QRCoder.dll 的事实。 strong>文件在某处。但我不知道该放在哪里。我已经尝试将其放在DemoWebAPI的 bin / debug / netcoreapp2.2 和DemoLibrary的 bin / debug / netstandard2.0 中。 p>

但是我无法正常工作。


请求:请发布您的答案尽可能多地描述,因为我是.Net Core的新手。


编辑
我知道NuGet服务器。我读过一些主题,例如在IIS和Azure中托管NuGet服务器。 DLL参考背后的原因是我想在两个项目中使用我的DLL,其中一个是.net核心API,另一个是NMAKE编译的.net框架类库。我找不到任何方法来还原.MAK文件中的NuGet软件包。

解决方案

似乎您只是添加了 DemoLibrary 的DLL到您的 DemoWebApi 项目。那不是您应该添加引用的方式。由于这些解决方案在同一解决方案中,因此您应该添加项目参考。



现在,让我解释一下这里的实际情况。您的 DemoLibrary 依赖于 QRCoder 。这是一个NuGet参考,这意味着该软件包将被还原(即下载)并包含在您的 DemoLibrary 构建输出中。但是,它将作为 DemoLibrary.dll 旁边的一个或多个DLL包含在内。然后,当您仅引用 DemoLibrary.dll 时,您会丢失所有 DemoLibrary 一部分的所有其他DLL,因此,事情就无法正常工作。



现在,当涉及到项目参考时,事情要复杂得多。项目引用实际上将引用的项目包装到您的其他项目中。您可以将其视为子项目。出于所有目的和目的,就像子项目的任何依赖都变成了主项目的依赖一样。这意味着 DemoWebAPI 从技术上讲现在具有对 QRCoder 的NuGet包引用,即使在其项目文件中没有明确的包引用也是如此。 。依赖项来自您的 DemoLibary 项目。因此,对于项目参考,将包括所有必需的依赖项,因为好像主项目通过子项目本身包括了那些依赖项。



对于不管它的价值是多少,您几乎绝对不应该直接包含DLL作为参考。以前需要这样做,但是NuGet软件包的概念几乎消除了这种做法。即使 DemoLibrary DemoWebAPI 不在同一个解决方案中(这意味着您无法再进行项目引用),正确的使用方法是将 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).net核心Web API在通过“添加引用”添加DLL时找不到子依赖项(依赖项的依赖项)。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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