'PackageReference Aliases are ignored on parent projects
Actually I'm trying to use the two libraries Microsoft.Graph and Microsoft.Graph.Beta in parallel within my solution.
To get this to work, you can use aliases within references. For this purpose you first have to write within your .csproj file something like this:
<PackageReference Include="Microsoft.Graph" Version="4.25.0" />
<PackageReference Include="Microsoft.Graph.Beta" Version="4.40.0-preview">
<Aliases>GraphBeta</Aliases>
</PackageReference>
After that within your .cs file at the top you can write something like this:
extern alias GraphBeta;
using Beta = GraphBeta.Microsoft.Graph;
using Microsoft.Graph;
And within your code you can access both parts individually like this:
var client = new GraphServiceClient(new HttpClient());
var betaClient = new Beta.GraphServiceClient(new HttpClient());
So far so good.
Unfortunately within my solution I'm using the above code within a library project. This library project is referenced by my application project and within my application project I also need to make some calls to Microsoft.Graph only. After adding the beta package to my library project I'm getting compiler errors from my application project like this:
error CS0433: The type 'IMessageAttachmentsCollectionPage' exists in both 'Microsoft.Graph.Beta, Version=4.40.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' and 'Microsoft.Graph, Version=4.25.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
So it seems, the given alias to the NuGet package within my library project doesn't flow upwards to my application project. My application project doesn't have any direct reference to any of the both Graph libraries, it just consumes it through the indirect dependency from the library project.
Also trying to add the extern alias line to the file within my application project results in this error message:
error CS0430: The extern alias 'GraphBeta' was not specified in a /reference option
Any solution available to either make the alias flowing upwards to my application project or to make the Beta dependency internal to the library project?
Solution 1:[1]
After some further try-and-error, my current solution is to add a reference to the Graph.Beta assembly (including the alias) explicitly to the application project. After that the error message is gone.
But this is just a simplified example. In my real project I have a deeper structure of multiple library projects and now every project that needs to use the Microsoft.Graph namespace needs to get an explicit package reference with the alias to the Beta package. For example if you have a test project for your library project, it must explicitly reference the Beta package including the alias if it tries to use any type from the Microsoft.Graph namespace within a test.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Oliver |
