7
votes

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

1
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)T.S.
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?user2452157
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". docs.microsoft.com/en-us/dotnet/framework/deployment/…T.S.
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.Weiwei
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.user2452157

1 Answers

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

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