drs9222
drs9222

Reputation: 4508

"An assembly with the same simple name has already been imported" error without duplicate reference

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.

Upvotes: 25

Views: 34979

Answers (11)

Mmm
Mmm

Reputation: 951

None of the above answers worked for me.

I'm using an old MVC web application that compiles just fine with the "offending" dll.

The error would come up when IIS/ASP.Net would compile the "temporary internet files", and at first glance didn't tell me much. The dll in question was only referenced in place in the project, and it compiled fine, so why was IIS/ASP.Net complaining?

After a few hours I figured out the IIS/ASP.Net error did indeed have the info I required to solve it. The CSC compile line scrolls far to the right, and it's not obvious in your browser you can scroll and scroll and scroll...

Copied that multi-page CSC compile line out to notepad with wordwrap on and searched on the dll in question, did a search, and found there's a plugin in this application that uses a much much older version of the offending dll. IIS/ASP.Net knew that, and complained. Visual Studio had no clue.

Hopefully this helps someone down the road in Stackoverflow land.

Upvotes: 0

forever_learner
forever_learner

Reputation: 86

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.

Upvotes: 1

Renesis
Renesis

Reputation: 899

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.

Upvotes: 0

Chris Schaller
Chris Schaller

Reputation: 16584

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 :)

Upvotes: 2

bikeman868
bikeman868

Reputation: 2637

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.

Upvotes: 5

Umar Khan
Umar Khan

Reputation: 11

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

Upvotes: 1

BobbyA
BobbyA

Reputation: 2260

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.

Upvotes: 4

Veverke
Veverke

Reputation: 11358

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.

Upvotes: 1

Eugenio Mir&#243;
Eugenio Mir&#243;

Reputation: 2428

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.

Upvotes: 2

drs9222
drs9222

Reputation: 4508

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.

Upvotes: 8

Robert Seder
Robert Seder

Reputation: 1420

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

Upvotes: 2

Related Questions