18

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.

I initially added Nuget packages to the .NET Standard class libraries using the Package Reference in Project Files style.

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. 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.

Steps I followed:

  • Uninstalling all Nuget packages in the .NET Standard project, except NETStandard.Library which cannot be uninstalled
    • My VS default setting for Nuget is to add the first package in a solution to packages.config
  • Reinstalling a Nuget package from the Manage Nuget Packages tool window
    • This package is placed in the csproj as a PackageReference, not in a packages.config file.

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?

Cass
  • 870
  • 8
  • 21
  • What sort of .NET Standard project is it? Is it an Sdk style project? An Sdk style project will always use PackageReferences. – Matt Ward Jul 22 '17 at 12:08
  • It's a class library project- is that the same as Sdk style? – Cass Jul 24 '17 at 15:24
  • If that means it's not possible @MattWard, add that as an answer & I'll accept. – Cass Jul 24 '17 at 15:26
  • It depends on what sort of project it is. A class library could be a project that uses a packages.config file or a project.json file or it could be an MSBuild sdk style project that uses PackageReferences. Does your project have an Sdk attribute in the Project element? That indicates it is an MSBuild sdk style project. – Matt Ward Jul 24 '17 at 15:35
  • It does have an Sdk attribute. Is it possible to convert from that to one with packages.config? – Cass Jul 24 '17 at 19:15
  • I do not think there are any tools you can use to convert it for you. You could either go back to a PCL project or a .NET Standard project that uses project.json. Either would involve some manual editing of the .csproj file. Or creating a new PCL project and adding the files back into the new project. Not ideal. – Matt Ward Jul 24 '17 at 20:52

4 Answers4

19

In addition to removing the PackageReferences from the project file, I also had to remove the following files from the $ProjectDir\obj directory:

  • Myproject.csproj.nuget.cache
  • Myproject.csproj.nuget.g.props
  • Myproject.csproj.nuget.g.targets
  • project.assets.json
BenV
  • 12,052
  • 13
  • 64
  • 92
  • 2
    This was the only thing that worked for me. I had to checkout a different branch that hadn't converted and I couldn't get anything to work. I even went so far as to run `devenv /resetuserdata` and that didn't work (wish I'd seen this first!) – crgolden Aug 20 '18 at 21:51
  • 2
    Thank you! Reverting to a previous commit did not help me, but this did! – Kristoffer Lerbæk Pedersen Mar 28 '19 at 11:50
  • 2
    Deleting the entire obj directory periodically can resolve or avoid odd bugs that VS will create for you. Its a temporary compilation folder so its contents are always expected to be regenerated. – StingyJack Apr 29 '19 at 11:28
7

How to roll back to packages.config

  1. Close the migrated project.

  2. Copy the project file and packages.config from the backup (typically \MigrationBackup\\) to the project folder. Delete the obj folder if it exists in the project root directory.

  3. Open the project.

  4. Open the Package Manager Console using the Tools > NuGet Package Manager > Package Manager Console menu command.

  5. Run the following command in the Console:

    update-package -reinstall

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

Paulo Pozeti
  • 359
  • 4
  • 4
2
  1. remove all PackageReference in the project file.
  2. deleting all the caches in $ProjectDir\obj.
  3. add an empty packages.config file(with the root tags) back to the project.
  4. rebuild the project. Visual studio then will prompt you what is missing.
  5. then add package of the version you select by using nuget manager.
  6. repeat step 4 and 5, VS will add your reference one by one back to the packages.config file.
yww325
  • 315
  • 4
  • 12
-1

If you haven't already you need to remove this property: <RestoreProjectStyle>PackageReference</RestoreProjectStyle>

And then remove any PackageReferences in your project. Then when you add nuget package references they should go into the package.config file.

Wes Haggard
  • 4,284
  • 1
  • 22
  • 21