18

I have a problem starting with VS2017 and the new PackageReference for NuGet dependencies.

At first I was excited that this extra packages.config file is not needed anymore. But now I am a bit disappointed:

Some of my assemblies target framework 4.0 because they must be able to run under XP also. Others do not have this limitation and target framework 4.6.1. This works fine because no 4.0 assembly have a dependency on a 4.6.1 assembly. Only the other direction.

Most assemblies uses the NLog NuGet package. But when using the new PackageReference option for specifying the NuGet packages, the assemblies targeting framework 4.6.1 will always install the NLog variant targeting .NET 4.5. From then on, other assemblies depending on a .NET 4.0 assembly cannot be build anymore:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(1987,5): warning MSB3275: The primary reference "Tools, Version=2.0.0.9180, Culture=neutral, processorArchitecture=MSIL" could not be resolved because it has an indirect dependency on the assembly "NLog, Version=3.2.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c" which was built against the ".NETFramework,Version=v4.5" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.0". [D:\Work\4.8.0_PackageReference\Test\TestApp.csproj]

I want to use NLog for .NET 4.0 for all projects. Is this really not possible without packages.config? There one could specify the desired framework with the targetFramework attribute. But I could not find any way to do this with the new PackageReference approach. That is unbelievable...

5
  • If your proj target framework 4, it will auto target. Otherwise, add the reference to right dll and set Specific Version to true. sometimes you have to do it directly in proj file as VS might revert to "corresponding version" (to selected target framework) Commented Sep 28, 2017 at 14:34
  • Thanks, but I need also .csproj targeting 4.5 or higher to use NLog using framework 4.0. Otherwise how would I handle two different NLog.dll in the bin folder at the same time? Commented Sep 28, 2017 at 14:46
  • you can, only via GAC or separate foldering. Read, how .NET selects dll. first - bin. Then folder named myassebly. Then GAC. you can also in config file, set your probing order. So, if your proj FW 4.5, you need to set reference to lower one and [probably] set "specific version". learn.microsoft.com/en-us/dotnet/framework/deployment/… Commented Sep 28, 2017 at 16:23
  • How do you use PackageReference in your framework 4.6.1 and framework 4.0? And do you reference framework 4.0 project in framework 4.6.1 project? I create a sample that framework 4.6.1 reference framework 4.0 project and add PackageReference in framework 4.6.1 project file with this code: <PackageReference Include="NLog" Version="3.2.0" Condition="'$(TargetFramework)' == 'net45'" /> It build successful in my side. Please provide more information about your project structure and relationship between projects to help reproduce this issue. Commented Sep 29, 2017 at 2:41
  • 1
    I also tested to add Condition="'$(TargetFramework)' == 'net40'" but it does not make any difference. I can specify anything as the "TargetFramework" there, it makes no difference. Always the .net45 Version of NLog is installed in the bin folder. I tested it with a complete new empty project. Commented Oct 10, 2017 at 8:41

1 Answer 1

13
<ItemGroup>
    <!-- ... -->
    <PackageReference Include="Newtonsoft.Json" Version="9.0.1" Condition="'$(TargetFramework)' == 'net452'" />
    <!-- ... -->
</ItemGroup>

MS doc link: https://learn.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files#adding-a-packagereference-condition

Sign up to request clarification or add additional context in comments.

1 Comment

This is not necessarily the same thing. In this snippet, the target framework is determined by the TargetFramework/TargetFrameworks property. It will not work if you have a package that works with net462 and netstandard2.0 and you want the netstandard2.0 version of the package rather rather than the net462, as would be the case with a net48 project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.