Reputation: 3
I would like to write a test case that checks the new behavior of a service against each previously-published client SDK. The service produces JSON output, and the SDK deserializes that and provides an object model. I want to ensure that as the service evolves, each older SDK can still successfully deserialize the output.
All the SDKs versions are available in nuget.org and in our package feed. Once restored, I can work out how to load the different assembly versions from a file path and call them via reflection, but I'm having trouble with the restore step. We are using .NET 8 with Central Package Management.
Ideally I'd like to have a test project contain something like this:
<ItemGroup>
<PackageReference Include="Foo.Bar.Sdk" VersionOverride="1.0.0" ExcludeAssets="all" />
<PackageReference Include="Foo.Bar.Sdk" VersionOverride="2.0.0" ExcludeAssets="all"/>
</ItemGroup>
But this fails with
Error NU1504: Warning As Error: Duplicate 'PackageReference' items found.
If I suppress the warning, only the latest version is downloaded from the feed. Is there a way to circumvent the normal behavior and deliberately fetch multiple versions of the same dependency?
Upvotes: 0
Views: 51
Reputation: 39
In .NET 8, you can restore and use multiple versions of the same package by using package aliasing in your .csproj file.
Example:
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3">
<Aliases>json12</Aliases>
</PackageReference>
</ItemGroup>
Usage in Code:
extern alias json12;
using Newtonsoft.Json; // Latest version
using json12::Newtonsoft.Json; // Aliased version
Upvotes: 1