简体   繁体   English

在.NET Standard项目中使用Package References后,如何恢复在packages.config中引用Nuget包?

[英]How can I revert to referencing Nuget packages in packages.config after using Package References in a .NET Standard project?

Background 背景

I have several C# code libraries which I recently converted to target .NET Standard 1.4, for use in a Xamarin Forms project as well as many legacy .NET Framework 4.6.1 projects. 我有几个C#代码库,我最近转换为目标.NET Standard 1.4,用于Xamarin Forms项目以及许多遗留的.NET Framework 4.6.1项目。

I initially added Nuget packages to the .NET Standard class libraries using the Package Reference in Project Files style. 我最初使用Project Files样式中的Package Reference将Nuget包添加到.NET Standard类库

After learning of this bug where transitive package references from .NET Standard projects are not resolved in legacy .NET Framework projects , I tried the workaround of adding <RestoreProjectStyle>PackageReference</RestoreProjectStyle> to the legacy project's csproj. 在了解了传统.NET Framework项目中未解析.NET Standard项目中的传递包引用此错误之后,我尝试了将<RestoreProjectStyle>PackageReference</RestoreProjectStyle>到旧项目的csproj中的解决方法。 The legacy projects continued to not resolve the transitive references- that is, the workaround didn't work. 遗留项目仍然没有解决传递性引用 - 也就是说,解决方法不起作用。

So, I would like to revert to the old style of referencing Nuget packages in a packages.config file in my .NET Standard project. 所以,我想恢复我的.NET Standard项目中的packages.config文件中引用Nuget包的旧样式。

Steps I followed: 我遵循的步骤:

  • Uninstalling all Nuget packages in the .NET Standard project, except NETStandard.Library which cannot be uninstalled 卸载.NET Standard项目中的所有Nuget软件包,但NETStandard.Library除外,无法卸载
    • My VS default setting for Nuget is to add the first package in a solution to packages.config Nuget的我的VS默认设置是将解决方案中的第一个包添加到packages.config中
  • Reinstalling a Nuget package from the Manage Nuget Packages tool window Manage Nuget Packages工具窗口重新安装Nuget包
    • This package is placed in the csproj as a PackageReference , not in a packages.config file. 此包作为PackageReference放在csproj中,而不是在packages.config文件中。

Does anyone know how I can force new Nuget packages in my .NET Standard 1.4 project to be listed in packages.config instead of as Package References? 有谁知道我如何强制我的.NET Standard 1.4项目中的新Nuget包列在packages.config而不是Package References中?

In addition to removing the PackageReferences from the project file, I also had to remove the following files from the $ProjectDir\\obj directory: 除了从项目文件中删除PackageReferences之外,我还必须从$ProjectDir\\obj目录中删除以下文件:

  • Myproject.csproj.nuget.cache Myproject.csproj.nuget.cache
  • Myproject.csproj.nuget.g.props Myproject.csproj.nuget.g.props
  • Myproject.csproj.nuget.g.targets Myproject.csproj.nuget.g.targets
  • project.assets.json project.assets.json

How to roll back to packages.config 如何回滚到packages.config

  1. Close the migrated project. 关闭迁移的项目。

  2. Copy the project file and packages.config from the backup (typically \\MigrationBackup\\\\) to the project folder. 将项目文件和packages.config从备份(通常为\\ MigrationBackup \\\\)复制到项目文件夹。 Delete the obj folder if it exists in the project root directory. 删除项目根目录中存在的obj文件夹。

  3. Open the project. 打开项目。

  4. Open the Package Manager Console using the Tools > NuGet Package Manager > Package Manager Console menu command. 使用工具> NuGet包管理器>包管理器控制台菜单命令打开包管理器控制台。

  5. Run the following command in the Console: 在控制台中运行以下命令:

    update-package -reinstall update-package -reinstall

Source: https://docs.microsoft.com/en-us/nuget/reference/migrate-packages-config-to-package-reference#how-to-roll-back-to-packagesconfig 来源: https//docs.microsoft.com/en-us/nuget/reference/migrate-packages-config-to-package-reference#how-to-roll-back-to-packagesconfig

  1. remove all PackageReference in the project file. 删除项目文件中的所有PackageReference。
  2. deleting all the caches in $ProjectDir\\obj. 删除$ ProjectDir \\ obj中的所有缓存。
  3. add an empty packages.config file(with the root tags) back to the project. 将一个空的packages.config文件(带有根标签)添加回项目。
  4. rebuild the project. 重建项目。 Visual studio then will prompt you what is missing. Visual Studio然后会提示你缺少什么。
  5. then add package of the version you select by using nuget manager. 然后使用nuget manager添加您选择的版本的包。
  6. repeat step 4 and 5, VS will add your reference one by one back to the packages.config file. 重复步骤4和5,VS会将您的引用逐个添加回packages.config文件。

If you haven't already you need to remove this property: <RestoreProjectStyle>PackageReference</RestoreProjectStyle> 如果您还不需要删除此属性: <RestoreProjectStyle>PackageReference</RestoreProjectStyle>

And then remove any PackageReferences in your project. 然后删除项目中的任何PackageReferences。 Then when you add nuget package references they should go into the package.config file. 然后,当您添加nuget包引用时,它们应该进入package.config文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM