SolvedHome extern alias support for NuGet package references

From @fubar-coder on April 5, 2017 15:59

Currently, when a NuGet package reference is added, there is no way to set the alias from the project system for the new style csproj projects.

This feature is needed, because NuGet package references don't result in direct assembly references any more and only those can have an alias.

My proposal is to add the alias(es) to all assemblies referenced for the NuGet package, but not the indirectly referenced NuGet packages.

Copied from original issue: dotnet/project-system#1930

notes

dotnet/sdk#10947 The build tasks on (.NET Core SDK side)
dotnet/NuGet.BuildTasks#70 The build tasks for the non-SDK based PackageReference
dotnet/project-system#6011 Nomination updates on project-system side.

33 Answers

✔️Accepted Answer

After some experimentation and got it to work.

Placed the below snippet into my csproj file where I had both references of StackExchange.Redis and StackExchange.Redis.StrongName nuget dependencies.

<Target Name="ChangeAliasesOfStrongNameAssemblies" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
    <ItemGroup>
      <ReferencePath Condition="'%(FileName)' == 'StackExchange.Redis.StrongName'">
        <Aliases>signed</Aliases>
      </ReferencePath>
    </ItemGroup>
  </Target>

Other Answers:

@fubar-coder the target name is just a random name same goes for the alias signed, the snippet above was all I added to my csproj file.

<Project Sdk="Microsoft.NET.Sdk">
  
  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
  </PropertyGroup>
  
  <ItemGroup>
    <PackageReference Include="Autofac" Version="4.6.0" />
    <PackageReference Include="AutofacSerilogIntegration" Version="2.0.0" />
    <PackageReference Include="AWSSDK.DynamoDBv2" Version="3.1.5" />
    <PackageReference Include="AWSSDK.S3" Version="3.1.7.2" />
    <PackageReference Include="ConfigInjector" Version="2.2.1175" />
    <PackageReference Include="Linq2DynamoDb.DataContext" Version="2.0.0" />
    <PackageReference Include="Linq2DynamoDb.DataContext.Caching.Redis" Version="2.0.0" />
    <PackageReference Include="MassTransit" Version="3.5.7" />
    <PackageReference Include="Microsoft.AspNet.SignalR.Core" Version="2.2.2" />
    <PackageReference Include="Microsoft.AspNet.SignalR.Redis" Version="2.2.2" />
    <PackageReference Include="Microsoft.Owin" Version="3.1.0" />
    <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
    <PackageReference Include="Owin" Version="1.0.0" />
    <PackageReference Include="Serilog" Version="2.5.0" />
    <PackageReference Include="ThirdDrawer" Version="1.1.9" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="Microsoft.CSharp" />
  </ItemGroup>
  
  <Target Name="ChangeAliasesOfStrongNameAssemblies" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
    <ItemGroup>
      <ReferencePath Condition="'%(FileName)' == 'StackExchange.Redis.StrongName'">
        <Aliases>signed</Aliases>
      </ReferencePath>
    </ItemGroup>
  </Target>
  
</Project>

With dotnet/sdk#11612 and dotnet/sdk#11954, finally all the legs of this feature are completed.

The feature will be available in 16.7 and all the matching tooling versions (3.1.400 of the SDK, 5.0 of the SDK) & 5.7 of NuGet.exe.

If you are using NuGet.exe, keep in mind that you need VIsual Studio/ MSBuild to be 16.7

Related Issues:

66
Home extern alias support for NuGet package references
After some experimentation and got it to work Design document From @fubar-coder on April 5 2017 15:5...
59
Home Package 'NameOfPackage' is incompatible with 'all' frameworks in project
I was surprised by this error as well Clearing NuGet cache fixed the problem. From @bsmayer on April...
38
Home Package Manager Error: Unable to load the service index for source (bad proxy settings)
For me removing the .nuget folder located in C:\Users\YourNameHere fixed the problem. ...
32
Home Unhelpful error message when incorrectly using nuspec version tag of nuget pack
This is definitely an error message which could do with some improvement It seems there are two prob...
27
Home Framework assemblies are no longer added to .nuspec when using the new .csproj project system
Is there any hope of getting this fixed soon? We're ready to merge in .csproj ports on several major...
27
Home error NU1403: The package Microsoft.CSharp.4.0.1 sha512 validation failed.
My mistake was not locking down my NuGet sources with a NuGet.Config which had this one enabled: Mic...
24
Home Add --prerelease option to dotnet add package
At the very least the error message needs to change This is the current experience: The message impl...
20
Home contentFiles (cs, compile) not working in NetStandard projects.
@emgarten Thanks for your fantastic support Two suggestions Please provide docs! NuGet is essential ...
13
Home dotnet.exe nuget push command now supports -skipDuplicate like nuget.exe did.
To clarify my expectation: #1630 should have included the required changes to dotnet Without that th...
12
Home Invoke must be called on a background thread
The error is generated by NuGet attempting to execute init.ps1 scripts for packages installed in a p...
11
Home dotnet restore on 2.1 preview1 throws error instead of warning when project files are "missing"
I have fixed that by changing Docker file from to From @christiansparre on March 8 ...
7
Home nuget.exe does not work with msbuild 12 as of 3.5.0 [fixed in 4.0.0]
.. Before NuGet CLI version 3.5.0.1938 We were using the NuGet CLI version 3.4.4.1321 When using ver...
3
Home Please provide a way to reference package ID and version in .target/.props files
I have the same problem I want to have way to specify package ID and version at my .targets file For...