如何将Microsoft.data.sqlclient.sni本机二进制文件复制到针对.NET Framework 4.7.2的Web应用程序的BIN目录?

发布于 2025-01-26 00:07:35 字数 4001 浏览 1 评论 0原文

如何< import> a .targets文件包含在< packageReference> d nuget软件包中针对.NET 4.7.2的Web应用程序项目?

背景

Web应用程序项目不是SDK风格的项目。它在同一解决方案中引用了类库项目。此类图书馆项目 SDK式项目。它的软件包引用对 microsoft.data.data.data.sqlclient版本4.1.1.0.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.0.1.1.0 <<<< /a>,取决于 microsoft.data.data.sqlclient.sni。运行时4.0.0 。后者包含自动复制到项目的bin目录的天然二进制文件。

本机二进制文件未复制到Web应用程序项目的BIN目录。这可能是因为这样:

sni是本机C ++库,在Windows上运行时,SQLClient依赖于各种网络操作。在使用MSBuild Project SDK构建的.NET框架应用程序中,本机DLL并未使用还原命令管理。因此,“ microsoft.data.sqlclient.sni” nuget软件包中包含一个“ .targets”文件,该软件包定义了必要的“复制”操作。

当对“ Microsoft.data.sqlclient”库进行直接依赖时,随身引用了随附的“ .targets”文件。在进行传递(间接)引用的方案中,应手动引用此“ .targets”文件以确保“复制”操作可以在必要时执行。

建议的解决方案:确保在应用程序的“ .csproj”文件中引用“ .targets”文件以确保执行“复制”操作。

source

不确定MSBuild Project SDK构建了什么是什么意思正好,但我认为我的场景有资格。

现在,我正在尝试实现推荐的解决方案,但我无法正常工作。

我做了

步骤1 :将软件包引用添加到 Microsoft.data.sqlclient.sni版本4.0.0

<PackageReference Include="Microsoft.Data.SqlClient.SNI">
  <GeneratePathProperty>true</GeneratePathProperty>
  <Version>4.0.0</Version>
</PackageReference>

我添加了&lt; generatePathProperty&gt;,因为以下步骤2。根据 A>这应该使可用的属性可用于参考软件包中的文件:

有时希望从MSBUILD目标中引用软件包中的文件。在基于packages.config的项目中,将软件包安装在文件夹中,相对于项目文件。但是,在packageReference中,包装是从全球包装文件夹中消费的,这些文件夹可能因机器而异。

为了弥合差距,Nuget引入了一个属性,该属性指向将消耗包装的位置。

步骤2 :导入目标文件:

<Import Project="$(PkgMicrosoft_Data_SqlClient_SNI)\build\net46\Microsoft.Data.SqlClient.SNI.targets" Condition="Exists('$(PkgMicrosoft_Data_SqlClient_SNI)\build\net46\Microsoft.Data.SqlClient.SNI.targets')" />

您可以看到该软件包使用Nuget软件包Explorer包含此目标文件( link )。 pkgmicrosoft_data_sqlclient_sni属性是根据文档(上面链接)命名的,并基于其示例:

MSBUILD属性和软件包身份没有相同的限制,因此需要将包装身份更改为MSBuild友好名称,该名称由pkg。

步骤3 :验证我们的代理商上的工具版本:

  • MSBUILD 16.11.2.50704(安装在代理c:\ program Files(x86)\ Microsoft Visual Studio \ 2019 \ Enterprise \ Enterprise \ MSBUILD \ MSBUILD \ Current \ Current \ Current \ Current \ Current \ Current \ bin
  • nuget 6.1.0.106(通过管道任务安装)

,根据上面链接的文档,这些内容满足了最低要求(MSBUILD 16和NUGET 5)。

步骤4 :运行Nuget Restore在我们的构建代理上,然后使用Azure DevOps msbuild@1使用MSBUILDARGUMENTS构建项目: '/t:构建'

步骤4 :收集输出并以工件的形式发布。

结果

输出中不存在SNI文件。使用MSBUILD 17.0.0.52104在本地构建时(安装在'C:\ Program Files(X86)\ Microsoft Visual Studio \ 2022 \ 2022 \ 2022 \ buildTools \ MSBUILD \ MSBUILD \ Current \ bin'中)我确实看到了SNI文件。

How can I <Import> a .targets file that is included in <PackageReference>d NuGet package in a non-SDK-style web application project that targets .NET 4.7.2?

Background

The web application project is not an SDK-style project. It references a class library project in the same solution. This class library project is an SDK-style project. It has a package reference to Microsoft.Data.SqlClient version 4.1.0, which depends on Microsoft.Data.SqlClient.SNI.runtime 4.0.0. The latter contains native binaries which are automatically copied to the bin directory of the project.

The native binaries are not copied to the bin directory of the web application project. This is probably because of this:

SNI is the native C++ library that SqlClient depends on for various network operations when running on Windows. In .NET Framework applications that are built with the MSBuild Project SDK, native DLLs aren't managed with restore commands. So a ".targets" file is included in the "Microsoft.Data.SqlClient.SNI" NuGet package that defines the necessary "Copy" operations.

The included ".targets" file is auto-referenced when a direct dependency is made to the "Microsoft.Data.SqlClient" library. In scenarios where a transitive (indirect) reference is made, this ".targets" file should be manually referenced to ensure "Copy" operations can execute when necessary.

Recommended Solution: Make sure the ".targets" file is referenced in the application's ".csproj" file to ensure "Copy" operations are executed.

Source

Not sure what "built with the MSBuild Project SDK" means exactly, but I think my scenario qualifies.

Now I'm trying to implement the recommended solution, but I cannot get it to work.

What I did

Step 1: add package reference to Microsoft.Data.SqlClient.SNI version 4.0.0:

<PackageReference Include="Microsoft.Data.SqlClient.SNI">
  <GeneratePathProperty>true</GeneratePathProperty>
  <Version>4.0.0</Version>
</PackageReference>

I added <GeneratePathProperty> because of step 2 below. According to the documentation this should make a property available that can be used to refer to files in the package:

Sometimes it is desirable to reference files in a package from an MSBuild target. In packages.config based projects, the packages are installed in a folder relative to the project file. However in PackageReference, the packages are consumed from the global-packages folder, which can vary from machine to machine.

To bridge that gap, NuGet introduced a property that points to the location from which the package will be consumed.

Step 2: import the targets file:

<Import Project="$(PkgMicrosoft_Data_SqlClient_SNI)\build\net46\Microsoft.Data.SqlClient.SNI.targets" Condition="Exists('$(PkgMicrosoft_Data_SqlClient_SNI)\build\net46\Microsoft.Data.SqlClient.SNI.targets')" />

You can see that the package contains this targets file using the NuGet package explorer (link). The PkgMicrosoft_Data_SqlClient_SNI property is named according to the documentation (linked above) and based on their example:

MSBuild properties and package identities do not have the same restrictions so the package identity needs to be changed to an MSBuild friendly name, prefixed by the word Pkg.

Step 3: verify the tool versions on our agent:

  • MSBuild 16.11.2.50704 (installed on the agent C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\bin)
  • NuGet 6.1.0.106 (installed via a pipeline task)

According to the documentation linked above, these fulfil the minimum requirements (MSBuild 16 and NuGet 5).

Step 4: run nuget restore on our build agent and then build the project using the Azure DevOps MSBuild@1 task with msbuildArguments: '/t:Build'.

Step 4: collect output and publish as an artifact.

Outcome:

The SNI files are not present in the output. When building locally with MSBuild 17.0.0.52104 (installed in 'C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\MSBuild\Current\bin') I do see the SNI files.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文