在 .NET Core 应用程序中引用 DLL 时出现 FileNotFoundException [英] FileNotFoundException when referencing DLL in .NET Core Application

查看:75
本文介绍了在 .NET Core 应用程序中引用 DLL 时出现 FileNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .NET Core 控制台应用程序和一个 .NET Core 类库.两者都是非常简单的单类项目.两者都是全新构建的,全新安装了最新的 .NET Core.两者都针对 .NET Core 1.1.

I have a .NET Core console application and a .NET Core class library. Both are extremely simple, single class projects. Both are freshly built, with a fresh install of the latest .NET Core. Both target .NET Core 1.1.

每当我在应用程序中包含 .NET Core 类库时,都会在运行时发生此错误:

This error occurs at runtime whenever I include a .NET Core class library in an application:

System.IO.FileNotFoundException: '无法加载文件或程序集'NAME,版本 = 1.0.0.0,文化 = 中性,PublicKeyToken = null'.这系统找不到指定的文件.

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

构建任一项目都可以,并且 Intellisense 在包含 using... 语句后向我显示类库的内容.参考和编写的代码在编译时没有问题.

Building either projects are fine, and Intellisense shows me the contents of the class library after including a using... statement. With the reference and code written there are no issues at compile time.

我已将控制台应用程序中引用的程序集的 Copy Local 设置为 Yes.运行时引用的 DLL 存在于控制台应用程序的 bin 文件夹中.

I've set the Copy Local to Yes for the referenced assembly in my console application. The referenced DLL exists in the bin folder of the console application during run time.

这里是 csproj 参考:

<Reference Include="NAME">
  <HintPath>pathinDebug
etcoreapp1.1NAME.dll</HintPath>
  <Private>true</Private>
  <SpecificVersion>false</SpecificVersion>
</Reference>

这只发生在 .NET Core DLL 中,我对 .NET Framework 4.5.* 及更高版本绝对没有问题.

This only happens with .NET Core DLLs, I have absolutely no issues with .NET Framework 4.5.* and up.

有人能解释一下这个问题吗?我发现的任何与此相关的 SO/MSDN 页面都是特定问题,例如针对 DLL 的错误版本,这无济于事.

Could anybody shed some light on this issue? Any SO/MSDN pages I've found regarding this have been specific problems like targeted the incorrect version of a DLL, which doesn't help.

推荐答案

使用 2.0 之前的工具不支持在 .NET Core 应用程序中引用 DLL 文件.

Referencing DLL files in a .NET Core application is not supported using the pre-2.0 tools.

原因是依赖关系图(deps.json 文件)生成不包括这些文件,并且很可能无论如何都不会工作,因为它无论如何都无法合并引用的 DLL 的引用/依赖项.

The reason is that the dependency graph (deps.json file) generation does not include these files and most likely wouldn't work anyway since it cannot consolidate references / dependencies of the referenced DLL anyway.

对于即将发布的 2.0 版本,只要您还引用原始程序包正在使用的所有 DLL/程序包,此方案就应该有效.语法是:

For the upcoming 2.0 release, this scenario should work as long as you also reference all DLLs / packages that the original package is using. The syntax would be:

<Reference Include="path/to/my.dll" />

.NET Core 2.0 还将支持以这种方式引用为 .NET 4.6.1 构建的程序集,但如果 DLL 使用不受支持的 API 调用,它可能会在运行时失败.

.NET Core 2.0 will also support referencing assemblies that have been built for .NET 4.6.1 this way, but it may fail at runtime if the DLL uses unsupported API calls.

这篇关于在 .NET Core 应用程序中引用 DLL 时出现 FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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