3

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.

wondra
  • 3,271
  • 3
  • 29
  • 48
  • To add a bit meta information: the dependency in question used by different packages is a MySql adapter and the exception in question is MySql.Data.MySqlClient.MySqlException, but the question is about this dependency issue in general. – wondra Mar 16 '20 at 11:50
  • You can try setting `all` for the `PackageReference` of the `UnrelatedPackage2` in `ProjectB`. Doing so, you will prevent the references of that package (and its dependencies) from being visible up-stream (`ProjectB` and `ProjectC`). – dymanoid Mar 16 '20 at 12:35
  • @dymanoid Sadly, `all` has no effect what so ever. All packages are still visible up-stream and the error is still there. That however brought me to a [question](https://stackoverflow.com/a/52214728/3096657) with similar problem under completely different name, where it was also suggest and likewise did not work. Luckily the other answer did. – wondra Mar 16 '20 at 12:46

1 Answers1

1

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.

wondra
  • 3,271
  • 3
  • 29
  • 48