gpt4 book ai didi

DotNet Core App 2.1 - Can't add DLL reference(DotNet Core App 2.1 -无法添加DLL引用)

转载 作者:bug小助手 更新时间:2023-10-25 17:07:53 32 4
gpt4 key购买 nike



I have a DotNetCore 2.1 Web App (just created it today from the VS 2017 scaffold). This solution also includes a DotNet Core 2.1 Library DLL project. The solution builds ok.

我有一个DotNetCore2.1 Web App(今天刚从VS 2017脚手架上创建)。该解决方案还包括一个DotNet Core 2.1库DLL项目。解决方案构建得很好。



I have another (brand new) DotNetCore 2.1 Web App that wants to use the Library DLL. But when I try to add a new reference (on the Browse tab), it complains:

我还有另一个(全新的)DotNetCore2.1Web应用程序想要使用库DLL。但当我尝试添加新引用时(在浏览选项卡上),它抱怨:




One or more errors occurred.

The reference is invalid or unsupported.




Any suggestions?

有什么建议吗?



(Curiously, the DLL doesn't show up directly under the Bin\Debug folder like it does in classic .Net; It's under Bin\Debug\netcoreapp2.1)

(奇怪的是,DLL不像在经典的.Net中那样直接显示在Bin\Debug文件夹下;它在Bin\Debug\netcoreapp2.1下)


更多回答

Is the dll marked as None or Content build action? is there an entry in the csproj file related to the dll?

DLL是标记为无还是内容构建操作?Csproj文件中是否有与DLL相关的条目?

No and no. The DLL doesn't exist in the referencing project. I'm trying to add an external DLL to my project references, and it just won't do it. A little Googling showed that this flavor of problem seems to infect DotNet Core libraries...

不是也不是。引用项目中不存在该DLL。我试图将外部DLL添加到我的项目引用中,但它不能做到这一点。稍微搜索一下,就会发现这个问题似乎感染了DotNet Core库……

优秀答案推荐

I got this error after I moved some of my code repos around and suddenly my .NET Core 2.1 console app wouldn't recognize a .NET 4.5 DLL anymore when I added it.

在我移动了一些代码报告后,我得到了这个错误,当我添加它时,我的.NET Core2.1控制台应用程序突然不能识别.NET 4.5DLL了。



Turned out that although the broken reference did not show in the solution explorer, the project file still contained it, along with the now broken HintPath to the DLL's old location. Manually removing this reference from the project file solved it.

原来,尽管解决方案资源管理器中没有显示损坏的引用,但项目文件仍然包含它,以及指向DLL旧位置的现已损坏的HintPath。手动从项目文件中删除此引用已解决此问题。



Go into your .csproj and make sure the <Reference Include=... is pointing to the correct directory.

进入您的.csproj并确保



For example, my references were pointing to the bin\Debug path of another project when the only build that was actually there was the bin\Release

例如,我的引用指向另一个项目的bin\Debug路径,而实际存在的唯一版本是bin\Release



When you create a new project from the Visual Studio 2017 Class Library (.NET Core) template, the csproj file contains:

当您从Visual Studio 2017类库(.NET Core)模板创建新项目时,csproj文件包含:



<TargetFramework>netcoreapp2.1</TargetFramework>


Since this is a library, it (apparently) needs to target netstandard2.0. (BTW, creating a library project from the dotnet command line command correctly targets netstandard2.0. Go figure.)

由于这是一个库,它(显然)需要以netStandard2.0为目标。(顺便说一句,从DotNet命令行命令创建库项目正确地针对netStandard2.0。想一想吧。)



So changing the line in the library's csproj file to

因此,将库的csproj文件中的行更改为



<TargetFramework>netstandard2.0</TargetFramework>


and rebuilding the library now allows me to reference the library from the DotNet Core 2.1 (netcoreapp2.1) application.

现在,重新构建库允许我从DotNet Core 2.1(netcoreapp2.1)应用程序引用该库。



I am converting lots of projects to .net6, and one of the projects suddenly refused to let me add .dll files. The solution was to remove the old references first, then add the new references. (The project had references to old .dll files with the same name as the new converted ones.)

我正在将许多项目转换为.net6,其中一个项目突然拒绝让我添加.dll文件。解决方案是首先删除旧引用,然后添加新引用。(该项目引用了与新转换的文件同名的旧.dll文件。)


Normally Visual Studio lets me add two dlls with the same name but different path, so I later can erase the old one, but in this particular case this did not work. A screenshot and Paint to the rescue for my memory!

通常,Visual Studio允许我添加两个同名但路径不同的dll,这样我以后就可以删除旧的dll,但在这种情况下,这不起作用。一张截图和画作来拯救我的记忆!



In my case, I had a reference to duplicated .dll files. After removing duplication and changing references to different folders got the error above. Cleaning obj, bin and removing the .vs folder from the root directory helped

在我的例子中,我引用了复制的.dll文件。删除重复项并更改对不同文件夹的引用后,出现上述错误。清除obj、bin并从根目录中删除.vs文件夹有帮助



For me I had to add a nuget package instead of manually adding a reference. I was using .net 6 and needed System.Drawing so I added the System.Drawing.Common package.

对于我来说,我必须添加一个Nuget包,而不是手动添加引用。我使用的是.NET6,需要System.Drawing,所以我添加了System.Drawing.Common包。


https://www.nuget.org/packages/System.Drawing.Common/

Https://www.nuget.org/packages/System.Drawing.Common/


更多回答

This was the problem. I edited the project, removed the section and added the reference back in with no problems.

这就是问题所在。我编辑了项目,删除了部分,并毫无问题地重新添加了引用。

But overall it is unnecessary to add a reference like that. A .NET Core Class Library project can generate a NuGet package directly, and then you can add package reference (which resolves all the dependencies).

但总体而言,没有必要添加这样的引用。.NET核心类库项目可以直接生成NuGet包,然后您可以添加包引用(它解析所有依赖项)。

But what can you do if this is a third-party library and you have no control of how it compiles?

但是,如果这是一个第三方库,并且您无法控制它的编译方式,您能做什么呢?

The whole point of the OP was that I have a solution containing (1) my application project, and (2) a general-purpose library project. In the context of this solution, both projects are what I'm working on. Note that the OP says I'm using a project reference, not a DLL reference. The whole point of this is to develop both the app and the library in one VS solution. The underlying problem was that the VS template code set up the csproj file for the library incorrectly.

OP的全部意义在于我有一个包含(1)我的应用程序项目和(2)通用库项目的解决方案。在这个解决方案的上下文中,这两个项目都是我正在从事的工作。请注意,OP说我使用的是项目引用,而不是DLL引用。这样做的目的是在一个VS解决方案中同时开发应用程序和库。根本问题是VS模板代码不正确地设置库的csproj文件。

@AnKing If you've been given a 3rd-party library, and it's not built correctly, my guess is that you are stuck unless you can get the library's author to fix it. I imagine there are ways to work around an incompatibility like that, but that would be a subject for a different SO question of its own. Good luck!

@AnKing如果你得到了一个第三方库,但它没有正确构建,我猜你会陷入困境,除非你能让库的作者来修复它。我想有办法解决这样的不兼容性问题,但这将是一个不同的问题本身的主题。祝好运!

in my case it worked after changing the target framework from netcore6.0 to net6.0 the name was wrong in the newer versions.

在我的例子中,在将目标框架从netcore6.0更改为net6.0之后,它工作了,在较新的版本中,名称是错误的。

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com