c# - 在 .NET Core 1.1 csproj 中引用 .NET 4.5 dll?

标签 c# asp.net-core

我正在运行 VS 2017 RC4。

我在我的 .NET Core 应用程序中添加了一个对我的 .NET 4.5 dll 的引用,它编译了。当在运行时调用引用 dll 的行时,我得到:

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

此图显示要使用 4.5 引用,我需要使用 netstandard 1.1。 https://msdnshared.blob.core.windows.net/media/2016/07/172.png

假设这是我需要的,我该如何在我的 .csproj 中引用它?我只能找到有关何时使用 project.json 的旧文档。

我尝试添加以下内容,但没有帮助:

<NetStandardImplicitPackageVersion>1.1</NetStandardImplicitPackageVersion>

此外,我还需要补充:

<RuntimeFrameworkVersion>1.0.3</RuntimeFrameworkVersion>

或者我得到 FileNotFoundException:无法加载文件或程序集。系统找不到指定的文件。

这是为什么?

以下是我的 .csproj 的相关部分:

<PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
    <PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
<RuntimeFrameworkVersion>1.0.3</RuntimeFrameworkVersion>
  </PropertyGroup>
<ItemGroup>
    <Reference Include="My4.5dll">
      <HintPath>Dlls\My4.5dll.dll</HintPath>
    </Reference>
  </ItemGroup>

最佳答案

您不能(安全地)将 .NET Framework 4.5 库加载到 .NET Core 中,因为它可能使用 .NET Core 中不可用的 API。

只有当您的库面向 portable-net45+win8(.NET Framework 4.5 Windows 8 Portable Class Profile 或更高版本)时,它才可以与 .NET Core 一起使用。因为这个特定的 PCL 配置文件限制了与(以前称为 WinRT)System.Runtime 兼容的 API,这是 .NET Core 的基础。

有关兼容 PCL 配置文件的列表,请参阅 this list (底部的 PCL 兼容性)

如果您要引用的程序集不支持 netstandard1.x 或任何受支持的配置文件,您必须以 .NET Framework 4.5 而不是 .NET Core 为目标.

在你的csproj中

<TargetFramework>net45</TargetFramework>
...
<ItemGroup>
    <PackageReference Include="Net45DependencyHere" Version="4.5.0" />

或者如果你是多目标

<TargetFrameworks>net45;netcoreapp1.1</TargetFrameworks>
...
<ItemGroup>
    <PackageReference Condition="'$(TargetFramework)' == 'net45' Include="Net45DependencyHere" Version="4.5.0" />
    <PackageReference Condition="'$(TargetFramework)' == 'netcoreapp1.1' Include="NetCoreReplacementLibrary" Version="1.1.0" />

您无法在 .NET Core 项目中自动神奇地使用任何 .NET Framework 4.5 库。只有 PCL 和 netstandard1.x 的。

更新

为了完整起见:

如果您确定您的类库/包以兼容的 PCL 为目标,您也可以让 nuget 恢复此包,即使它们不以 netstandard1.x

<PropertyGroup>
    <PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
</PropertyGroup>

警告语

从不,我再说一遍,从不除了兼容的 PCL 库之外,别把任何东西放在那里。 永远不要net45 放在这里。这只会强制 NuGet 下载并安装此程序包,但不会使其正常工作并在运行时崩溃并出现与上述类似的错误!

它只是强制 nuget 安装知道在过渡期间与 .NET Core 一起工作的包,直到大多数包都以 netstandard1.x 为目标!

关于c# - 在 .NET Core 1.1 csproj 中引用 .NET 4.5 dll?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42349605/

相关文章:

c# - 从 ASP.net MVC 4 WEB API Controller 返回 byte[]

c# - WPF:在不实现 INotifyPropertyChanged 接口(interface)的情况下显示属性更改

asp.net-core - 用每个 hangfire 作业的独特值(value)丰富 Serlilogs

c# - 使用 Xunit 进行 .Net 核心测试

c# - 从 .net 核心使用 ServiceStack Redis 并连接到 Sentinel 设置

c# - 获取具有指定 ID 的行之后按字母顺序排列的行

c# - 动态对象转换为仅在运行时已知的类型

C# 和 Kinect v2 : Get RGB values that fit to depth-pixel

c# - 如何在 c#.net windows 应用程序中检查 gridview 的行是否被选中

c# - .net 核心应用程序目标 Linux 上的 .net framework 4.5.2