ChatGPT解决这个技术问题 Extra ChatGPT

The located assembly's manifest definition does not match the assembly reference

I am trying to run some unit tests in a C# Windows Forms application (Visual Studio 2005), and I get the following error:

System.IO.FileLoadException: Could not load file or assembly 'Utility, Version=1.2.0.200, Culture=neutral, PublicKeyToken=764d581291d764f7' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)** at x.Foo.FooGO() at x.Foo.Foo2(String groupName_) in Foo.cs:line 123 at x.Foo.UnitTests.FooTests.TestFoo() in FooTests.cs:line 98** System.IO.FileLoadException: Could not load file or assembly 'Utility, Version=1.2.0.203, Culture=neutral, PublicKeyToken=764d581291d764f7' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

I look in my references, and I only have a reference to Utility version 1.2.0.203 (the other one is old).

Any suggestions on how I figure out what is trying to reference this old version of this DLL file?

Besides, I don't think I even have this old assembly on my hard drive. Is there any tool to search for this old versioned assembly?

In my case, this happened because I had two projects loading the same DLL with different versions. (hope this helps someone!)

C
Callum Watkins

The .NET Assembly loader:

is unable to find 1.2.0.203

but did find a 1.2.0.200

This assembly does not match what was requested and therefore you get this error.

In simple words, it can't find the assembly that was referenced. Make sure it can find the right assembly by putting it in the GAC or in the application path. Also see https://docs.microsoft.com/archive/blogs/junfeng/the-located-assemblys-manifest-definition-with-name-xxx-dll-does-not-match-the-assembly-reference.


but when i look at references of the project, it is pointing to 1.2.0.203 .. . nothing seems to be pointing to 1.2.0.200 anymore
Exactly - it's looking for 1.2.0.203, but it found 1.2.0.200. Find out where that file is and replace it with the right version.
I asked a similar question here and got a working solution: stackoverflow.com/questions/4187907/…
Check the references version, and then look if its the same in packages.config and Web.config
This message confuses me every time. It seems to be written backwards. I'd expect it to complain about the version you've asked to load, not the version it found. Glad I'm not the only one who gets it wrong!
C
Community

You can do a couple of things to troubleshoot this issue. First, use Windows file search to search your hard drive for your assembly (.dll). Once you have a list of results, do View->Choose Details... and then check "File Version". This will display the version number in the list of results, so you can see where the old version might be coming from.

Also, like Lars said, check your GAC to see what version is listed there. This Microsoft article states that assemblies found in the GAC are not copied locally during a build, so you might need to remove the old version before doing a rebuild all. (See my answer to this question for notes on creating a batch file to do this for you)

If you still can't figure out where the old version is coming from, you can use the fuslogvw.exe application that ships with Visual Studio to get more information about the binding failures. Microsoft has information about this tool here. Note that you'll have to enable logging by setting the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Fusion\EnableLog registry key to 1.


Don't forget that the File Version is not part of the assemblies identity. The Assembly Version is, but does not have to be the same as the File Version!
If using fuslogvw for services read blogs.msdn.com/b/junfeng/archive/2004/02/14/72912.aspx
Searching for the filename solved my problem. I had an old version of the dll in my Temporary ASP.Net folder and InstallShield was using that instead of the up to date version! Clean solution, rebuild, restart PC did nothing. Worked fine locally and blew up every time it was deployed.
Immediately after building, my site works fine, but a little while later, this problem crops up.
I edited this answer to say assembly version instead.
N
Nathan Bedford

I just ran into this problem myself, and I found that the issue was something different than what the others have run into.

I had two DLLs that my main project was referencing: CompanyClasses.dll and CompanyControls.dll. I was getting a run-time error saying:

Could not load file or assembly 'CompanyClasses, Version=1.4.1.0, Culture=neutral, PublicKeyToken=045746ba8544160c' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference

Trouble was, I didn't have any CompanyClasses.dll files on my system with a version number of 1.4.1. None in the GAC, none in the app folders...none anywhere. I searched my entire hard drive. All the CompanyClasses.dll files I had were 1.4.2.

The real problem, I found, was that CompanyControls.dll referenced version 1.4.1 of CompanyClasses.dll. I just recompiled CompanyControls.dll (after having it reference CompanyClasses.dll 1.4.2) and this error went away for me.


+1 Something similar happened to me when I have one of my DLLs reference an older version of Caliburn Micro.
me too, your experience sparked me where I need to look and I got my issue fixed.
Another option would be to open the CompanyControls project, right-click the CompanyClasses.dll reference --> properties --> SpecificVersion = false
this is very often case on xamarin applications. I had xamarin.forms project different than xamarin.droid project. I just saw your post and I recognized it.
If CompanyClasses.dll is signed, then SpecificVersion = false alone won't cut it. You'll need a bindingredirect.
P
Peter Mortensen

The following redirects any assembly version to version 3.1.0.0. We have a script that will always update this reference in the App.config so we never have to deal with this issue again.

Through reflection you can get the assembly publicKeyToken and generate this block from the .dll file itself.

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
 <dependentAssembly>
    <assemblyIdentity name="Castle.Core" publicKeyToken="407dd0808d44fbdc" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-65535.65535.65535.65535" newVersion="3.1.0.0" />
  </dependentAssembly>
</assemblyBinding>

Note that without an XML namespace attribute (xmlns) this will not work.


This worked for me. I changed the 'newVersion=3.3.3' to 'newVersion=3.1.0'
Best not to go down the rebinding route if you can avoid it though. When that bug comes to bite you it'll bite hard.
My issue was the fact that the redirects were pointing to non-existent assemblies.The App.Config held assembly info from the latest NuGet packages I'd installed. When I downgraded these packages later, it did NOT clean this up. This was a .NET standard class library being hit by a 4.7.2 framework unit test project. The unit test project's issue presented at runtime..
@D-Sect is right. If your source control indicates that web.config has changes (because you've been playing with your NuGets) you might be wise to back those bindingRedirects out of there. Downgrading NuGets will not clean up the binding redirects
T
Tad

If you are using Visual Studio, try "clean solution" and then rebuild your project.


This is usually the solution for me. Often, deleting bin and obj do it. Basically, something I used to reference is still sitting there trying to satisfy the same requirement. For example, an old version being something I referenced directly and the new version being on NuGet.
Encountered this problem for several DLLs after pulling from TFS. This solution fixed it for me.
Worked for me. Deleted out the bin amd obj folder and resolved the issue.
Thanks, worked for me also, just deleting the bin folder.
"Clean solution" didn't do anything for me. (Microsoft????) But deleting the bin and obj folders fixed it. Thanks!
R
RayLoveless

https://i.stack.imgur.com/SUDUK.png


That setting has only effect at compile time. After it is compiled, it will need the exact same assembly version with which you compiled it. See stackoverflow.com/questions/24022134/…
c
codeMonkey

I am going to blow everyone's mind right now . . .

Delete all the <assemblyBinding> references from your .config file, and then run this command from the NuGet Package Manager console:

Get-Project -All | Add-BindingRedirect

Worked for me as well. Thanks
you saved my time 👌👌
this works only when package management format is packages.config, if you are using 2017 csproj without packages.config, it doenst work :(
Mind officially blown
I had updated this product over time... sometimes just flushing the artifacts and having a do over is needed. I think the only thing different I could have done was create a new project and hand pick the elements into it... this command saved me from having to do that... Someday I will have to pay the technical debt? Maybe, but not today evil.
L
Levi Fuller

In my case, this error occurred while running an ASP.NET application. The solution was to:

Delete the obj and bin folders in the project folder

Clean didn't work, rebuild didn't work, all references were fine, but it wasn't writing one of the libraries. After deleting those directories, everything worked perfectly.


Thanks, Levi Fuller.This answer should be higher up; for my situation it was spot on! For me this error started when I made a backup copy of my web.config and Visual Studio kept loading this config file instead of the actual config, even after I deleted the duplicate copy. This solved it. Thanks.
Works for me too. Still unsure why this works though :(
Thank you so much. This finally got it working after hours of troubleshooting. In my case, the problem happened after I accidentally upgraded some NuGet packages, then reverted the changes in my working copy (packages.config, .csproj).
P
Peter Mortensen

I added a NuGet package, only to realize a black-box portion of my application was referencing an older version of the library.

I removed the package and referenced the older version's static DLL file, but the web.config file was never updated from:

<dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
    <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="6.0.0.0" />
</dependentAssembly>

to what it should have reverted to when I uninstalled the package:

<dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.5.0.0" />
</dependentAssembly>

I've seen this where, at least for the Entity Framework module when you use NuGet, if you right-click your solution, go to Manage NuGet Packages for Solution, then Installed Packages > All, select that module, select Manage, you can usually deselect it from your project. That should clean out things like this without having to do it manually --- assuming the vendor did their due diligence. But good catch as apparently sometimes they don't, if that's how you removed it.
N
Neal Tibrewala

I just ran across this issue and the problem was I had an old copy of the .dll in my application debug directory. You might want to also check there (instead of the GAC) to see if you see it.


We just migrates to a different server had this issue cause we keep backups. Worked after deleting backup copies. Thanks :)
G
Glade Mellor

In my case it was an old version of the DLL in C:\WINDOWS\Microsoft.NET\Framework\~\Temporary ASP.NET Files\ directory. You can either delete or replace the old version, or you can remove and add back the reference to the DLL in your project. Basically, either way will create a new pointer to the temporary ASP.NET Files.


This worked for me when I closed Visual Studio, stopped IIS, and deleted all temporary ASP.NET files. Note there can be files in the Framework and Framework64 folder if on 64 bit machine, as well as in the .NET 2.0 and 4.0 folders!
I used the Windows Start Menu search function to find all DLLs created by my solution and then deleted the whole lot, wherever they were found. I could do this without fear because they should only be created during my Visual Studio debugging. As VS will rebuild such missing DLLs and nothing outside of my solution should be referencing them, this was a "safe" operation for me.
S
Sire

For us, the problem was caused by something else. The license file for the DevExpress components included two lines, one for an old version of the components that was not installed on this particular computer. Removing the older version from the license file solved the issue.

The annoying part is that the error message gave no indication to what reference was causing the problems.


In my case, after upgrading to new DevExpress version, the .resx file of a form contained references to old uninstalled library version. I had to open .resx in code view and either correct the version to new one or delete the invalid entries.
P
Peter Mortensen

I would like to just add that I was creating a basic ASP.NET MVC 4 project and added DotNetOpenAuth.AspNet via NuGet. This resulted in the same error after I referenced a mismatching DLL file for Microsoft.Web.WebPages.OAuth.

To fix it I did a Update-Package and cleaned the solution for a full rebuild.

That worked for me and is kind of a lazy way, but time is money:-P


Similar answer for me. Update-Package -reinstall re-installs all NuGet packages at the same version.
This is great; thanks for posting. I tried all of these other great suggestions, but this was the solution that did the trick. Thank goodness for people who are still willing to post an alternate solution, even when there are 80 of them available
This solved it for me. My company's library that I recently installed was using an older reference version of the library causing the mismatch. After installation, this threw the error. Update-Package -reinstall brought it back in order
G
Guy Starbuck

This exact same error is thrown if you try to late bind using reflection, if the assembly you are binding to gets strong-named or has its public-key token changed. The error is the same even though there is not actually any assembly found with the specified public key token.

You need to add the correct public key token (you can get it using sn -T on the dll) to resolve the error. Hope this helps.


Please elaborate - what is "sn -T"? And where do I add the public key token?
"sn.exe" is a tool that comes with Visual Studio, it's a command line tool that can be run from the Visual Studio command prompt. Just run the Visual Studio command prompt (from the start menu), navigate to the folder that contains your assembly, and type "sn -T " where is the full name of the dll. This gets the Assembly "Token" information. Once you have this, when you are doing late binding with reflection, enter the token info into the assembly id string (i.e., "Assembly=MyAssembly.dll, Public Key Token=)
Thanks for this answer. I was getting this error when referencing a Configuration Section in my App.ini. I had recently signed the assembly, so the PublicKeyToken=null had to be updated with the new (correct) token.
M
Marek Schwarz

Is possible you have a wrong nugget versions in assemblyBinding try:

Remove all assembly binding content in web.config / app.config:

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Extensions.Logging.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.1.3.0" newVersion="3.1.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Extensions.DependencyInjection" publicKeyToken="adb9793829ddae60" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.1.3.0" newVersion="3.1.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.2.1.0" />
  </dependentAssembly>
</assemblyBinding>

Type in Package Manager Console: Add-BindingRedirect All necessary binding redirects are generated Run your application and see if it works properly. If not, add any missing binding redirects that the package console missed.


A
AEON Blue Software

My issue was copying source code to a new machine without pulling over any of the referenced assemblies.

Nothing that I did fixed the error, so in haste, I deleted the BIN directory altogether. Rebuilt my source code, and it worked from then on out.


C
Contango

Mine was a very similar situation to the post by Nathan Bedford but with a slight twist. My project too referenced the changed dll in two ways. 1) Directly and 2) Indirectly by referencing a component (class library) that itself had a reference to the changed dll. Now my Visual studio project for the component(2) referenced the correct version of the changed dll. However the version number of the compnent itself was NOT changed. And as a result the install of the new version of the project failed to replace that component on the client machine.

End result: Direct reference (1) and Indirect reference(2) were pointing to different versions of the changed dll at the client machine. On my dev machine it worked fine.

Resolution: Remove application; Delete all the DLLS from application folder; Re-install.Simple as that in my case.


P
Peter Mortensen

I got this error while building on Team Foundation Server's build-service. It turned out I had multiple projects in my solution using different versions of the same library added with NuGet. I removed all old versions with NuGet and added the new one as reference for all.

Team Foundation Server puts all DLL files in one directory, and there can only be one DLL file of a certain name at a time of course.


Another way is to click "Manage NuGet packages for solution..." and update both your test project and the project under tests to the same (newest) version.
M
Mike Murphy

I'll let someone benefit from my shear stupidity. I have some dependencies to a completely separate application (let's call this App1). The dll's from that App1 are pulled into my new application (App2). Any time I do updates in APP1, I have to create new dll's and copy them into App2. Well. . .I got tired of copying and pasting between 2 different App1 versions, so I simply added a 'NEW_' prefix to the dll's.

Well. . . I'm guessing that the build process scans the /bin folder and when it matches something up incorrectly, it barfs with the same error message as noted above. I deleted my "new_" versions and it built just dandy.


P
Peter Mortensen

My app.config contains a

<bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.11.0"/>

for npgsql. Somehow on the user's machine, my app.exe.config went missing. I am not sure if it was a silly user, installer glitch, or wacked out anti-virus yet. Replacing the file solved the issue.


C
Cole

After trying many of the above solutions with no fix, it came down to making sure 'Auto-generate binding redirects' was turned on within your application in Visual Studio.

https://i.stack.imgur.com/bXLq6.png

More information on enabling automatic binding redirection can be found here: https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/how-to-enable-and-disable-automatic-binding-redirection


L
Ladislav Mrnka

I just found another reason why to get this error. I cleaned my GAC from all versions of a specific library and built my project with reference to specific version deployed together with the executable. When I run the project I got this exception searching for a newer version of the library.

The reason was publisher policy. When I uninstalled library's versions from GAC I forgot to uninstall publisher policy assemblies as well so instead of using my locally deployed assembly the assembly loader found publisher policy in GAC which told it to search for a newer version.


u
uli78

To me the code coverage configuration in the "Local.testtesttings" file "caused" the problem. I forgot to update the files that were referenced there.


d
dhiraj1mumbai

Just deleting contents of your project's bin folder and rebuild the solution solved my problem.


Well what do you know, you just helped me out my misery, thanks indeed
C
ChrisBeamond

A general answer to this kind of issue is to use binding redirects as in other answers. However, that's only part of the problem - you need to know the correct version of the assembly file that you're using. Windows properties is not always accurate and nuget is also not always accurate.

The only way to get correct version info is to analyse the file itself. One useful tool is dotPeek. The assembly name listed in dotPeek is always accurate in my experience.

So for example, the correct binding for this file is the following:

<dependentAssembly>
    <assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
    <bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.2.1.0"/>
</dependentAssembly>

Windows explorer says the file is 4.6.26515.06, nuget says its a 5.0.0.0 file. dotPeek says it is 4.2.1.0 and that is the version that works correctly in our software. Also note that the public key and culture are important and dotPeek also show this information.

https://i.stack.imgur.com/KcEtq.png


!!!!!!!!!!This worked instantly : Note that the binding for me that worked was related to the oldVersion -> newVersion. My assembly manifest pointed to the old version, but was never redirected to the new version of the assembly after upgrading to a later of the same DLL.
M
Mike Gledhill

Here's my method of fixing this issue.

From the exception message, get the name of the "problem" library and the "expected" version number.

https://i.stack.imgur.com/8XKgk.png

Find all copies of that .dll in your solution, right-click on them, and check which version of the .dll it is.

https://i.stack.imgur.com/QxmEp.png

Okay, so in this example, my .dll is definitely 2.0.5022.0 (so the Exception version number is wrong).

Search for the version number which was shown in the Exception message in all of the .csproj files in your solution. Replace this version number with the actual number from the dll.

So, in this example, I would replace this...

<Reference Include="DocumentFormat.OpenXml, Version=2.5.5631.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />

... with this...

<Reference Include="DocumentFormat.OpenXml, Version=2.0.5022.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />

Job done !


what if my csproj files have no version referenced?
P
Peter Mortensen

The question has already an answer, but if the problem has occurred by NuGet package in different versions in the same solution, you can try the following.

Open NuGet Package Manager, as you see my service project version is different than others.

Then update projects that contain an old version of your package.

https://i.stack.imgur.com/dYt7J.png


G
Ganesh Londhe

clean and rebuild the solution might not replace all the dll's from the output directory.

what i'll suggest is try renaming the folder from "bin" to "oldbin" or "obj" to "oldobj"

and then try build your silution again.

incase if you are using any third party dll's those you will need to copy into newly created "bin" or "obj" folder after successful build.

hope this will work for you.


m
marc_s

No solution worked for me. I tried clean project solution, remove bin, update package, downgrade package and so on... After two hours I loaded default App.config from project with assemblies and there I changed wrong reference version from:

<dependentAssembly>
    <assemblyIdentity name="Microsoft.IdentityModel.Logging" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.5.0.0" newVersion="5.5.0.0" />
</dependentAssembly>

to:

<dependentAssembly>
    <assemblyIdentity name="Microsoft.IdentityModel.Logging" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.14.0.0" newVersion="5.5.0.0" />
</dependentAssembly>

After this I cleaned project, build it again and it worked. No warning no problem.


t
toralux

This question is quite old, and I had the same error message recently with Azure DevOps Yaml pipelines and Dotnet Core 3.1. The problem was somewhat different than the other answers try to solve, so I will share my solution.

I had a solution with many projects for my own nuget packages. I had by accident added version-tag in the *.csproj files like this:

  <Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <Version>1.0.0</Version>
  </PropertyGroup>

I packed the nuget packages for all projects using Yaml with a DotnetCoreCLI@2 task:

 - task: DotNetCoreCLI@2
   displayName: 'pack'
   inputs:
     command: pack
     nobuild: true
     configurationToPack: 'Release'
     includesource: true
     includesymbols: true
     packagesToPack: 'MyNugetProject1.csproj;**/MyNugetProject2.csproj'
     versioningScheme: 'byEnvVar'
     versionEnvVar: 'GitVersion.SemVer'

The problem was that the version in the *.csproj files didn't match the version in the environment variable GitVersion.SemVer (specified by input-"versionEnvVar").

After removing all <Version>1.0.0</Version>-tags in the *.csproj files the assembly/fileversion for dll was automatically assigned by the environment-variable and both nuget and dll (assembly/fileversion) would have the same version and problem was solved.