8

I have a c# program that uses BouncyCastle. If I insert a reference to iTextSharp, the compiler generates many errors of classes already existing in both references. I have specified all the namespaces but it does not change anything. Example:

 Org.BouncyCastle.X509.X509Certificate certCopy = DotNetUtilities.FromX509Certificate(oCertificato);

How can I use BouncyCastle and iTextSharp together?

  • You should read [this](https://stackoverflow.com/questions/3672920/two-different-dll-with-same-namespace) SO question and have a look at [this GitHub source](https://github.com/itext/itextsharp/blob/9d04600a8c79fdfdf0cfc4ad395e6818b21de0a4/src/core/srcbc/security/DotNetUtilities.cs) – Jeroen Heier Jan 04 '19 at 18:54
  • 2
    I solved by changing only the alias in the properties of the iTextSharp dll. Thank you! – Franco De Giorgi Jan 05 '19 at 10:17
  • 2
    Welcome to Stackoverflow! Did you know that you can answer your own question if you found a solution for yourself? This will help others as well... – Lonzak Jan 07 '19 at 07:26
  • 2
    For anyone else: if you change the alias in the reference properties for iTextSharp from "global" to, say "iText", then you using statement will be `extern alias iText;` `using iText::iTextSharp.text.pdf;` – ProgrammierTier Apr 19 '19 at 19:14
  • if you're having issues adding the `alias`, you might need to manually edit the csproj. see https://stackoverflow.com/a/55986973/426315 for more details – itsho May 04 '19 at 21:34

2 Answers2

6

Just like @franco-de-giorgi said. Add an Alias to the library.

I'm just writing a full answer because I had to learn what is an Alias and how to add and Alias

Go to your references and go to properties on BouncyCastle, then change global to your personal Alias:

enter image description here

Then in your class use an external alias to your references like this (instead of using)

//using Org.BouncyCastle.Crypto.Parameters;
extern alias Merged;

In your classes add your alias

new Merged::Org.BouncyCastle.OpenSsl.PemReader
Augusto
  • 81
  • 1
  • 3
0

TL;DR: Use the PackageReference's Aliases attribute in *.csproj and add the alias to the affected *.cs files:

<PackageReference Include="PackageAffectedByConflict" Aliases="AltGlobalNamespace" />
extern alias AltGlobalNamespace;

using AltGlobalNamespace.ConflictedName;

According to the NuGet documentation from Microsoft:

In some rare instances different packages will contain classes in the same namespace. Starting with NuGet 5.7 & Visual Studio 2019 Update 7, equivalent to ProjectReference, PackageReference supports Aliases. By default no aliases are provided. When an alias is specified, all assemblies coming from the annotated package with need to be referenced with an alias.

The documentation also links an example on GitHub showing how to use the Aliases attribute:

PackageReferenceAliasesExample.csproj:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net5.0</TargetFramework>
    </PropertyGroup>
  
    <ItemGroup>
        <PackageReference Include="NuGet.Versioning" Version="5.8.0" Aliases="ExampleAlias" />
    </ItemGroup>

</Project>

Program.cs:

extern alias ExampleAlias;
using System;

namespace PackageReferenceAliasesExample
{
    class Program
    {
        static void Main(string[] args)
        {
            var version = ExampleAlias.NuGet.Versioning.NuGetVersion.Parse("5.0.0");
            Console.WriteLine($"Version : {version}");
        }
    }
}
psqli
  • 588
  • 6
  • 11
  • I kept the example generic, so use `BouncyCastle` as the PackageAffectedByConflict, and choose any name (including `BouncyCastle`) for AltGlobalNamespace. – psqli Oct 28 '21 at 15:38