C
C#2y ago
n8ta

Specifying framework version of dependency manually in SDK style project

I am updating an old project to the SDK style and I have a reference that looked like
<Reference Include="Package, Version=1.2.3.4, Culture=neutral>
<HintPath>$(EnlistmentRoot)\packages\Package\lib\net45\Package.dll</HintPath>
</Reference>
<Reference Include="Package, Version=1.2.3.4, Culture=neutral>
<HintPath>$(EnlistmentRoot)\packages\Package\lib\net45\Package.dll</HintPath>
</Reference>
After moving to SDK style I had
<PackageReference Include="Package" Version="1.2.3.4" />
<PackageReference Include="Package" Version="1.2.3.4" />
Which caused the build system to select the net46 version of the package not the net45 which turns out to be important. How can I override that behavior? Going back to the <Reference seems to work but it'd be nice to use PackageReference if possible.
6 Replies
333fred
333fred2y ago
You'll need a combo of PackageRef with ExcludeAssets="all" and GeneratePathProperty="true", then a Reference element pointing at <package path>\lib\net45\package.dll
333fred
333fred2y ago
NuGet PackageReference in project files
Details on NuGet PackageReference in project files as supported by NuGet 4.0+ and VS2017 and .NET Core 2.0
333fred
333fred2y ago
Which, IMO, is better than just a raw Reference
n8ta
n8taOP2y ago
Okay so what that gets me is that the version isn't hardcoded in my reference anymore correct? So it can be found by dotnet build and such. Easily restored globally etc.
333fred
333fred2y ago
Correct The SDK generates where the package path is, so you no longer need to care if it's at EnlistmentRoot, ~/.nuget, or wherever else You only need to care about the path within the package
n8ta
n8taOP2y ago
Okay that sounds like a reasonable compromise to me, thanks! Right now we've got a single packages.config and 100% hardcoded <References so it'll be nice to let that go

Did you find this page helpful?