Search code examples
.netnugettarget-framework

How to smoothly upgrade the targetFramework for a NuGet package


I have a NuGet package which currently targets .NET 4.5. The next version will instead target .NET 4.5.2. I have no interest in supporting both framework versions (these packages are only consumed internally), but I do want to make it obvious to consumers that they need to target 4.5.2. Right now, it seems that NuGet will allow a 4.5 project to install my package but it will be a no-op (since there are no DLLs to install for net45).

My question is, what can I do to improve this experience? Ideally I'd like to have the install fail when the targetFramework is wrong. Barring that, I'd settle for something like an error message.


Solution

  • To do this, you need to make sure that everything in your package is targeted at the new version. Then, NuGet will fail the install with this message:

    Could not install package 'XXX'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.5', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author

    My problem was that even thought my DLL targeted net452, I had other elements in my nuspec file (e. g. content files) that still targeted net45. NuGet interprets this as my package being multi-targeted, and thus allows the install to proceed in a .NET 4.5 project (silently skipping the step of adding the DLL reference).