【问题标题】:NuGet selecting wrong DLL (32-bits vs 64-bits)NuGet 选择错误的 DLL(32 位与 64 位)
【发布时间】:2021-08-28 17:35:22
【问题描述】:

我正在使用一个 .net NuGet,它包含一个编译为本机代码的 DLL。该原生 DLL 有两个版本,一个 32 位版本和一个 64 位版本。

nuget 使用其“build”文件夹中的“.targets”文件来确定要复制的 DLL。目标文件看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <!-- Ensure that for Framework projects the correct DLL is copied to the build directory -->
  
  <ItemGroup Condition=" '$(Platform)' == 'x64' OR '$(Platform)' == 'AnyCPU' ">
    <Content Include="$(MSBuildThisFileDirectory)..\runtimes\win-x64\native\FiftyOne.DeviceDetection.Hash.Engine.OnPremise.Native.dll">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <Link>FiftyOne.DeviceDetection.Hash.Engine.OnPremise.Native.dll</Link>
    </Content>
  </ItemGroup>

  <ItemGroup Condition=" '$(Platform)' == 'x86' ">
    <Content Include="$(MSBuildThisFileDirectory)..\runtimes\win-x86\native\FiftyOne.DeviceDetection.Hash.Engine.OnPremise.Native.dll">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <Link>FiftyOne.DeviceDetection.Hash.Engine.OnPremise.Native.dll</Link>
    </Content>
  </ItemGroup>

</Project>

但是,即使我使用的是 64 位计算机并使用 64 位构建目标,最终在我的构建的“Bin”文件夹中的始终是 32 位版本。

有谁知道这个过程是如何工作的,以及什么决定了 NuGet 将决定以哪个平台为目标?如何让 nuget-restore-and-build 进程选择 64 位 DLL? (使用 Visual Studio 或 msbuild 执行构建)

【问题讨论】:

    标签: .net .net-core nuget


    【解决方案1】:

    您发布的目标文件与FiftyOne.DeviceDetection.Hash.Engine.OnPremise NuGet 包的最新版本(4.2.4)的目标文件不匹配,其中AnyCPU 匹配x86 平台而不是x64 平台:

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    
      <!-- Ensure that for Framework projects the correct DLL is copied to the build directory -->
      
      <ItemGroup Condition=" '$(Platform)' == 'x64' ">
        <Content Include="$(MSBuildThisFileDirectory)..\runtimes\win-x64\native\FiftyOne.DeviceDetection.Hash.Engine.OnPremise.Native.dll">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <Link>FiftyOne.DeviceDetection.Hash.Engine.OnPremise.Native.dll</Link>
        </Content>
      </ItemGroup>
    
      <ItemGroup Condition=" '$(Platform)' == 'x86' OR '$(Platform)' == 'AnyCPU' ">
        <Content Include="$(MSBuildThisFileDirectory)..\runtimes\win-x86\native\FiftyOne.DeviceDetection.Hash.Engine.OnPremise.Native.dll">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <Link>FiftyOne.DeviceDetection.Hash.Engine.OnPremise.Native.dll</Link>
        </Content>
      </ItemGroup>
    
    </Project>
    

    因此,为了让正确的原生 dll 最终出现在您的输出目录中,您应该在您的 csproj 文件中添加 &lt;Platform&gt;x64&lt;/Platform&gt; 或通过指定 Platform 属性来构建您的项目,如下所示:

    dotnet build /p:Platform=x64 MyApp.csproj
    

    【讨论】:

    • 修改后的文件是我试图解决问题的一种尝试,我也尝试使用未修改的原始文件,但无济于事。不幸的是,传递 Platform=x64 似乎也无济于事
    猜你喜欢
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2011-05-12
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    相关资源
    最近更新 更多