25

I'm getting the following error:

error CS1704: An assembly with the same simple name 'Interop.xxx.dll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null has already been imported. Try removing one of the references or sign them to enable side-by-side.

Everything I've seen says that I am referencing two assemblies with the same name and I need to remove one of them. However, I've checked and I'm only referencing it once.

This also only happens when I'm using msbuild to build from the command line on my dev box. If I build through Visual Studio or do a clean build on our CI server I don't see this error.

I've tried completely removing all of my source and building from scratch to more closely resemble the build machine but no luck.

Rai Vu
  • 1,595
  • 1
  • 20
  • 30
drs9222
  • 4,448
  • 3
  • 33
  • 41

10 Answers10

8

So it looks like I can't read today!
The project had a reference to the Interop and a COM reference that generated the "same" interop. So there were two and I just didn't search very well. I still don't understand why it worked in other places but this did fix it.

drs9222
  • 4,448
  • 3
  • 33
  • 41
5

In my case the duplicate entry was caused by a NuGet package reference and a direct file reference to the same assembly in the packages folder. I am not sure how the project got into this state, but unloading the project and searching the XML file for the offending assembly name resolved the issue for me.

Note that in my case this started happening after updating a NuGet package to a newer version with no other changes to the project, so this maybe caused by a bug in NuGet.

bikeman868
  • 2,236
  • 23
  • 30
  • My case was similar: a dll, that previously was referenced by NuGet, was added to the solution as project (and updated there to a newer version). Afterwards, CS1704 occurred for no apparent reason. But there still was a transitive dependency to the old version of the dll which always pulled it and provoked the error! – Nicolas Feb 20 '20 at 16:20
4

In the Error List window, the project that was triggering this error was listed in the Project column. I got around the error by doing the following:

  • I unloaded the listed project (right-click => Unload Project)
  • Opened the XML for edit (right-click the unloaded project => Edit {ProjectName.csproj}).
  • Searched for the offending .dll, and noticed it was listed multiple times in the XML
  • Removed the entire Reference tag related to the offending dll, and did so for every copy of the reference except the first one listed

The reason it was listed multiple times was because several referenced libraries used that dll. This shouldn't be a problem, in and of itself, so I'm not sure what caused this error to suddenly pop up for me. I'll update this answer if I figure that out.

BobbyA
  • 2,090
  • 23
  • 41
2

If this is a web project, are there any strong-named references to the other version there? Those won't show up as a project dependency, but will cause a run-time error like you describe. Hope that helps

Robert Seder
  • 1,390
  • 1
  • 9
  • 19
2

I had this problem but in my case, I had an old copy placed in the current folder for the EXE loading my component, that was loaded together with the current one, that was loaded by hand from my projects folder. Deleting that old copy solved my problem.
I used Debug > Windows > Modules window to see which modules were loaded at that time and that solved my problem.

Eugenio Miró
  • 2,398
  • 2
  • 28
  • 38
2

For those developing UWP projects that have project references that include specifically the Microsoft.Windows.SDK.Contracts nuget package (or other dependencies that reference it), this is a common error when the version of the SDK contracts is targeting a different version of the runtime to how your project is configured.

For instance, when targeting Windows 10, version 1903: UWP Runtime Targeting

Any dependencies or reference projects should target or at least support the same runtime version.

it is common thought process to update all NuGet packages when a new stable version is available, but this is not always a helpful practise on its own. Just because a new stable version of a package is available does not mean that you should or that you can easily use that version.

Even though this package for SDK contracts has a stable update, it is not compatible with my main project configuration, Nuget does not know this so it allows the update.

Windows SDK Contracts

This package is specifically designed to provide windows dlls for project types that DO NOT have windows platform targeting support, it copies the same dlls that are included by the UWP targeting config. By installing later versions of the package the references from the satellite project will be included in the output along with those provided due to platform targeting, ultimately causing OPs error.

There are similar SDK and targeting packs for Windows IoT Device Runtimes, this information should help you identify and resolve those issues if you get stuck on this issue as my team often does :)

Chris Schaller
  • 13,704
  • 3
  • 43
  • 81
1

For others facing the same as me: if building via command line using property AssemblyName, it will overwrite all assemblies generated by all solution projects - in other words, you will end up with (N -1) assemblies named the same where N is the no. of projects - the startup one (which generally will generate an exe).

This happens because all build command line properties are global and overwrite any project-specific setting. See this and this.

From the msdn link mentioned above:

Global properties are properties that are set by using the /property switch on the command line, or properties that are set by the integrated development environment (IDE) before a project is built. These global properties are applied to all projects that are built by using this Engine.

In my specific case, where Jenkins is the CI tool, I ended up adding a windows batch command at the end to rename the .exe only to what I originally intended when passing the AssemblyName parameter.

Veverke
  • 9,208
  • 4
  • 51
  • 95
1

In my case, the issue was on wrong characters in the ProjectReference section of my csproj file.

Background

I have a project that references another library I maintain, which I publish as a NuGet package.

Whenever I make changes to my library, I usually reference the local dll in my project to test and make sure everything looks good before I publish the library as a NuGet package.

When testing, I just comment out the PackageReference line and uncomment the ProjectReference one so it references my local dll, like so:

<ProjectReference Include="..\..\my-class-library\MyClassLibrary.csproj" />
<!--<PackageReference="MyClassLibrary" Version="2.0.1"/>-->

Root cause

I had the slashes inverted, so I was using / rather than \ in the path, like so:

<ProjectReference Include="../../my-class-library/MyClassLibrary.csproj" />

Once corrected, the issue went away.

0

Try this instead: remove Interop.xx.dll from the reference section in Solution Explorer and Rebuild the project

Umar Khan
  • 1
  • 1
0

In our case this error was shown when we had a duplicate reference inside the .csproj file (although I have no idea how this happened).
The difference to an already posted answer is that, in our case, one was a project reference and another one was direct binary reference to a dll.
Once we removed one of those, project correctly compiled.

Renesis
  • 881
  • 2
  • 9
  • 16