Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type exists in 2 assemblies (due to Package dependency)

Tags:

c#

nuget

I am facing the very same problem as in Type exists in 2 assemblies:

The type exists in both <ASSEMBLY1.dll> and <ASSEMBLY2.dll>

I have tried the most voted answer from the question however, I am still getting the error.
I believe it is because my setup is quite different from the question: I have two my projects ProjectA and ProjectB, each of those using completely different NuGet packages, that happen to use a dependencies/types with exactly same name but from different vendors (without vendor prefix!). Both of these projects are referenced in ProjectC

ProjectA
- Packages
-- UnrelatedPackage1
--- Dependency (VendorA)
ProjectB
- Packages
-- UnrelatedPackage2
--- Dependency (VendorB)
ProjectC
- Refences
-- ProjectA
-- ProjectB

Now, I would like to catch in ProjectC's code an exception raised in ProjectA's dependency :

try {
   ProjectA.SomeClass.DoStuff();
}
catch (ExceptionFromDependency ex)
{ }

but I cannot since

The type ExceptionFromDependency exists in both <Dependency (VendorA)> and <Dependency (VendorB)>

I have added an alias projectb to ProjectB's reference in ProejctC as recommended in the answer, but it did not fix the error - probably because the source is not the project itself but rather dependency of dependency.
How can I fix the error so I can catch the exception? I dont ever plan to use any types from 'Dependency (VendorB)', and I would prefer to contain its existence to the ProjectB, if that offers simpler solution.

like image 258
wondra Avatar asked Oct 25 '25 15:10

wondra


1 Answers

There is a workaround to alias the package itself, but not accessible from UI, edit&add the following Target to ProjectC's .csproj file:

<Target Name="ChangeAliasesOfAssemblies"
        BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
  <ItemGroup>
    <ReferencePath Condition="'%(FileName)' == 'Dependency.VendorB'">
      <Aliases>projectb</Aliases>
    </ReferencePath>
  </ItemGroup>
</Target>

where Dependency.VendorB is the .dll file name without extension of the 'Dependency (VendorB)' package. Clean/rebuild/restart IDE.

like image 60
wondra Avatar answered Oct 27 '25 04:10

wondra