Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An assembly with the same identity 'XXX' has already been imported. Try removing one of the duplicate references

I have a MVC web application that I publish to our UAT server with msbuild command line. It is hosted on a windows 2016 server, IIS 10.

Sometimes, not always, I have a the following error when I try to start up the website:

Server Error in '/' Application. Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1703: An assembly with the same identity 'System.Xml.ReaderWriter, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' has already been imported. Try removing one of the duplicate references.

Source Error: [No relevant source lines] Source File: Line: 0 Show Detailed Compiler Output: C:\WINDOWS\SysWOW64\inetsrv> "C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe" /t:library /utf8output /R: "C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Text.Encoding.Extensions\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Text.Encoding.Extensions.dll" /R:

...

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\4a1a5bcc\84ed54d3\assembly\dl3\ab6a43c3\00602620_2137d201\System.Xml.ReaderWriter.dll" /R:

...

"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xml.ReaderWriter\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Xml.ReaderWriter.dll" /R:

...

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\4a1a5bcc\84ed54d3\App_Web_home.cshtml.65a2d1ee.ef3qnjdl.1.cs" Microsoft (R) Visual C# Compiler version 4.6.1586.0

for C# 5 Copyright (C) Microsoft Corporation. All rights reserved.

This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240

c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\4a1a5bcc\84ed54d3\assembly\dl3\ab6a43c3\00602620_2137d201\System.Xml.ReaderWriter.DLL: error CS1703: An assembly with the same identity 'System.Xml.ReaderWriter, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' has already been imported. Try removing one of the duplicate references. c:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Xml.ReaderWriter\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Xml.ReaderWriter.dll: (Location of symbol related to previous error)

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.3163.0

I have tried to look for references, but I can see it only once in the web.config.

Also, the error usually goes away after I run an iisreset or restart the web server. My fear is this could happen on the live server too if we release it.

I would appreciate any help to point me to the right direction to fix this.

UPDATE:

I have checked all the references in the whole project. All I can find is.

Web.config:

<compilation defaultLanguage="c#" debug="true" batch="false" targetFramework="4.5" numRecompilesBeforeAppRestart="50">
        <assemblies>
            <remove assembly="System.Xml.ReaderWriter" />
            <add assembly="System.Xml.ReaderWriter, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

and Packages.config:

<package id="System.Xml.ReaderWriter" version="4.3.0" targetFramework="net461" />

UPDATE 2:

I have removed the "add assembly" from the web.config as suggested by Péter Csajtai. Since then it is working fine (probably it is not used anywhere).

I have tried to remove the other assemblies, but those are used, so I would need multiple redirects. On the other hand it is working, so I am reluctant a bit to "fix it". I have added a redirect to see if it would work, but without an error it is hard to tell if it is doing the right thing.

Also found this that could be useful for other's for troubleshooting in the future.

like image 808
Crick3t Avatar asked Nov 08 '18 15:11

Crick3t


1 Answers

As the output states, your projects are referencing the System.Xml.ReaderWriter.dll from two different places:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\4a1a5bcc\84ed54d3\assembly\dl3\ab6a43c3\00602620_2137d201\

And from the GAC:

C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xml.ReaderWriter\v4.0_4.0.0.0__b03f5f7f11d50a3a\

You should check all your references to that dll and unify the reference paths where they are different.

like image 158
Péter Csajtai Avatar answered Sep 24 '22 03:09

Péter Csajtai