【问题标题】:Reference .NET 4.5 dll in .NET Core 1.1 csproj?在 .NET Core 1.1 csproj 中引用 .NET 4.5 dll?
【发布时间】:2018-08-04 04:47:38
【问题描述】:

我正在运行 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>

【问题讨论】:

    标签: c# asp.net-core


    【解决方案1】:

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

    仅当您的库以 portable-net45+win8(.NET Framework 4.5 和 Windows 8 Portable Class Profile 或更高版本)为目标时,它才能与 .NET Core 一起使用。因为这个特定的 PCL Profile 限制了与(以前称为 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 为目标!

    【讨论】:

    • 虽然这对我的特殊情况没有帮助,但我对其进行了测试并注意到以下问题:The TargetFramework value 'net45;netcoreapp2.0' is not valid. To multi-target, use the 'TargetFrameworks' property instead. 所以我猜&lt;TargetFramework&gt; 属性中需要一个额外的s。
    • @WouterVanherck:正确。复制粘贴错误。修复了
    • 感谢大家的帮助,我只需要将 .Net Framwork 版本从 45 修改为 452。在此分享详细信息 dotnetinternal.blogspot.com/2018/10/…
    【解决方案2】:

    我遇到了同样的问题,但使用的是 .NET Standard 2.0 和 .NET Framework 4.0 程序集。解决方案是在针对 .NET Standard 2.0 的 nuget 包资源管理器中使用我的 dll 创建一个 nuget,然后将包添加到项目中。

    nuget explorer 的链接:https://github.com/NuGetPackageExplorer/NuGetPackageExplorer

    【讨论】:

      猜你喜欢
      • 2017-07-26
      • 2021-04-30
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      • 2016-12-08
      • 1970-01-01
      • 2017-07-14
      • 1970-01-01
      相关资源
      最近更新 更多