How to resolve compilation issue CS1703 error in a Xamarin.iOS project

I have a Xamarin cross platform (iOS and Android) solution using .NET Standard 2.0. The solution is being built by Visual Studio 2017 (I've tried both the current and preview versions). The Forms and Android projects build fine. However, the iOS fails to build, with the following error:

1>CSC : error CS1703: Multiple assemblies with equivalent identity have been imported: 'C:\Users\newuser.nuget\packages\system.reflection.emit\4.3.0\ref\netstandard1.1\System.Reflection.Emit.dll' and 'C:\Program Files (x86)\Microsoft Visual Studio\Preview\Enterprise\Common7\IDE\ReferenceAssemblies\Microsoft\Framework\Xamarin.iOS\v1.0\Facades\System.Reflection.Emit.dll'. Remove one of the duplicate references.

I tried many work arounds as described here:

Unfortunately none of these suggestions work. I had tried to add the System.Reflection.Emit nuget package directly to the Forms and/or to the iOS projects directly. This in itself didn't work either. Note that originally, the System.Reflection.Emit package wasn't referenced directly by anything in the solution. It is being pulled in as a dependency by the following nuget packages which where themselves are being explicitly referenced:

  • Acr.DeviceInfo (5.0.5)
  • akavache (6.0.0-alpha0038)
  • AutoMapper (6.2.2)
  • Microsoft.Identity.Client (1.1.1-preview0040)
  • Newtonsoft.Json (10.0.3)

Finally, I have tried the update-package -reinstall command in the package console to reinstall all packages for the iOS project. This doesn't work either. :-(

I had confirmed all of the dependencies referenced by Nuget, use the same version. See these screenshots:

Collapsed references

Expanded references

*Referenced Emit dependencies from Visual Studio Expanded referenced Emit dependencies
*

It seemed to me the problem (as the error says) is that there is an old version of System.Reflection.Emit that was installed by Visual Studio. However, I couldn't find a way of ignoring it and using the nuget version of System.Reflection.Emit instead.

Finally I discovered the way to resolve this issue is to add this to the iOS project file.

<PackageReference Include="System.Reflection.Emit">
  <Version>4.3.0</Version>
  <ExcludeAssets>all</ExcludeAssets>
</PackageReference>

Have a look at this for documentation about nuget packagereference.

It works by excluding the package from the compilation process. Of course in order to get this to work, I had to first explicitly add the System.Reflection.Emit nuget package to the iOS project.

Hope this helps.