.NET Core-无法加载文件或程序集 [英] .NET Core - Could not load file or assembly

查看:1043
本文介绍了.NET Core-无法加载文件或程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在aspnet核心1.1应用程序中使用自定义DLL(4.5框架)。

I'm trying to use a custom DLL (4.5 framework) in a aspnet core 1.1 app.

我正在使用 Microsoft。 NETCore.Portable.Compatibility

当我运行该项目并尝试在某个类的库项目中调用时,vs2017抛出以下内容

When I run the project and try to make a call at some class of the library project, vs2017 throw the following exception.

Could not load file or assembly 'ApiHelperSock, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. O sistema não pode encontrar o arquivo especificado.

如果我检查 bin\Debug\netcoreapp1.1 我可以在这里看到我的dll。

If I check the bin\Debug\netcoreapp1.1 I can see my dll here.

这是我的project.json文件

This is my project.json file

{
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5"
  },
  "exclude": [
    "node_modules",
    "wwwroot"
  ]
}

任何想法吗?

---编辑---

试图用我的dll设置nuget包,尝试导入时出现以下错误:

Tried to setup a nuget package with my dll, getting the following error when trying to import:

The package ApHelperSock 1.0.0 isn't compatible with netcoreapp1.1 (.NETCoreApp,Version=v1.1). The package ApiHelperSock 1.0.0 supports: net (.NETFramework,Version=v0.0)               

创建一个.NETFramework库项目,导入此处,然后从我的aspnetcore应用程序调用该库,但没有成功。

Tried to create a .NETFramework library project, import here and then call the library from my aspnetcore app, without success.

推荐答案

您可以'不能在.NET Core应用程序中自动使用.NET 4.5库!您的库必须定位 netstandard1.0 或更高。

You can't auto-magically use .NET 4.5 libraries in .NET Core applications! Your library has to target netstandard1.0 or higher.

将您的库转换为 netstandard1.0 (4.5.2)或 netstandard1。 2 (.NET 4.5.2)项目。如果仅使用.NET 4.5 Api,则还需要添加 net45 / net452 目标并使用 #ifdef NET45 预处理程序指令,使其中的代码仅针对 net452 进行编译,而不能针对其他平台进行编译。

Convert your library to a netstandard1.0 (4.5.2) or netstandard1.2 (.NET 4.5.2) project. If you use .NET 4.5 only Api, you also need to add net45/net452 target and use #ifdef NET45 preprocessor directives to make your code inside it only compile for net452, but not for other platforms.

在.NET Core 2.0中,您可以引用普通的.NET 4.5库,但是存在相同的限制,使用时必须非常小心: 只有这样才能在.NET Core上工作,当引用的库仅使用.NET Standard 2.0上可用的API时
使用任何其他API 都会在运行时炸毁您的程序!此选项仅适用于需要引用.NET 4.5库但尚未移植为.NET Standard 2.0(即孤立的nuget包)的人。

In .NET Core 2.0 you'll get the ability to reference normal .NET 4.5 libraries, but the same limitations apply and you must be extremely carefully when using it: It will only then work on .NET Core, when the referenced library only uses API which is available on .NET Standard 2.0!!! Using any other API will blow up your program at runtime! This option is only there for people who need to reference a library which is .NET 4.5 but not yet ported for .NET Standard 2.0 (i.e. orphaned nuget packages).

这篇关于.NET Core-无法加载文件或程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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