ChatGPT解决这个技术问题 Extra ChatGPT

Could not load file or assembly or one of its dependencies

I'm having another of these "Could not load file or assembly or one of its dependencies" problems.

Additional information: Could not load file or assembly 'Microsoft.Practices.Unity, Version=1.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

I have no idea what is causing this or how I could debug it to find the cause.

I've done a search in my solution catalogs .csproj files, and every where I have Unity I have:

Reference Include="Microsoft.Practices.Unity, Version=2.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"

Can't find any reference anywhere which goes against 1.2.0.0 in any of my projects.

Any ideas how I should go about solving this?

Could any of your referenced assemblies be using some stuff in old Unity library?
Probably... but how can I find which assemblies? I have a lot of projects in my solution and a lot of potential suspects... trial and error bruteforce seems a bit hopeless...
It's not the assembly reference, you reference version 2.0. But at runtime, the CLR is finding 1.2, an old version. If you don't see that old DLL in your build directory then use Fuslogvw.exe to find out how the CLR found this old copy.
Look at your project's bin folder and see if your project's dll has a conflict in it's name. Just delete that one and then Rebuild your solution. That worked for me.
"or one of its dependencies" is the part that really annoys me. If it can't load "one of its dependencies" the error should say which "one of its dependencies" can't be loaded. The current form is useless, it might as well say can't load thinggy

S
Slime recipe

Check if you are referencing an assembly which in turn referencing an old version of unity. For example let's say you have an assembly called ServiceLocator.dll which needs an old version of Unity assembly, now when you reference the ServiceLocator you should provide it with the old version of Unity, and that makes the problem. May be the output folder where all projects build their assemblies, has an old version of unity.

You can use FusLogVw to find out who is loading the old assemblies, just define a path for the log, and run your solution, then check (in FusLogvw) the first line where the Unity assembly is loaded, double click it and see the calling assembly, and here you go.


Where's the log file of FuseLogVw
To avoid having to find the log file, you can specify a custom log path: Settings, check the Enable custom log path checkbox, enter a custom log path, refresh.
k
kranthi

Open IIS Manager

Select Application Pools

then select the pool you are using

go to advanced settings (at right side)

Change the flag of Enable 32-bit application false to true.


IIS -> select each ApplicationPool -> Basic Settings -> check if the latest framework is selected under the ".NET Framework version" dropdown
You can also right click your project in VS . and remove the prefer 32 bit checkmark
Thanks. It worked. Well it was already True in my case , just for try. I made it False and it worked.
When I merged a project from 1 server to another, this flag was indeed False again, thanks for the solution!
R
Robotnik

For me, none of the other solutions worked (including the clean/rebuild strategy). I found another workaround solution which is to close and re-open Visual Studio.

I guess this forces Visual Studio to re-load the solution and all the projects, rechecking the dependencies in the process.


Yeah, same here, combined with doing a Clean Solution. After I did that, VS highlighted a build error that wasn't showing up before. Previously it said Rebuild All Succeeded after rebuilding - I was referencing the wrong namespace for a class.
B
BlaM

Try to clean Debug and Release folders in your solution. Then remove and add unity again.


This issue can be caused by a lot of things ... your solution solved my problems, and might solve others' as well.
@ScottRippey This worked for me to. I first deleted all .pdb files and then reloaded my project and rebuild it.
Worked for me, I just deleted the bin/ folder and built the solution again.
r
radbyx

At 99% the Could not load file or assembly or one of its dependencies problem is caused by dependencies! I suggest you follow this steps:

Download Dependency Walker from http://www.dependencywalker.com/ Launch Dependency Walker and open the dll (in my case NativeInterfaces.dll) You can see one or more dll with the error in red Error opening file...

https://i.stack.imgur.com/9xmc8.png

It means that this dll is missing in your system; in my case the dll name is MSVCR71.DLL You can download missings dll from google and copy in right path (in my case c:\windows\system32) At this point, you must register the new dll in the GAC (Global Assembly Cache): open a DOS terminal and write: cd \Windows\System32 regsvr32 /i msvcr71.dll Restart your application


Dependency walker is great, but copying random DLLs from the internet to Windows is ... less great. It's better to try to find the installer that provides those dlls.
I got a few files (API-MS-WIN-CORE-KERNEL32-PRIVATE-L1-1-1.DLL) not found and led me to this stackoverflow question. Basically keep in mind could be looking at false positive for some files, link provides more detail.
J
Jeremy Caney

Despite the original question being posted five years ago, the problem still persists and is rather annoying.

The general solution is thorough analysis of all referenced assemblies to understand what's going wrong. To make this task easier I made a tool (a Visual Studio extension) which allows selecting a .NET assembly (a .dll or .exe file) to get a graph of all the referenced assemblies while highlighting conflicting or missing references.

The tool is available in Visual Studio Gallery: https://marketplace.visualstudio.com/vsgallery/051172f3-4b30-4bbc-8da6-d55f70402734

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


Does not work with Community editions of Visual Studio
I believe there should be another issue, not related to Visual Studio edition. I tested the extension on VS 2017 and VS 2015 Community editions. Actually it was developed by means of VS 2017 Community edition.
Aha. Do you have any other extensions installed? This page says that DGML is not supported in VS Community: msdn.microsoft.com/en-us/library/hh871439.aspx#VersionSupport
There are no architecture tools in the Community edition but the DGML editor itself it available. You can install it selecting "Install DGML editor" under "Individual Components" -> "Code Tools" via Visual Studio Installer -> Modify
R
Riddhi M.

Following worked for me.

Remove Temporary Files C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files

Close VSTS and Open Again

Remove and Add the same DLLs (Note: you add the same matching versions)


R
Rebecca

Microsoft Enterprise Library (referenced by .NetTiers) was our problem, which was in turn referencing an older version of Unity. In order to solve the problem we used the following binding redirection in the web.config:

<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="Microsoft.Practices.Unity" publicKeyToken="31bf3856ad364e35" culture="neutral" />
                <bindingRedirect oldVersion="1.0.0.0-2.0.414.0" newVersion="2.1.505.0" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="Microsoft.Practices.Unity.Configuration" publicKeyToken="31bf3856ad364e35" culture="neutral" />
                <bindingRedirect oldVersion="1.0.0.0-2.0.414.0" newVersion="2.1.505.0" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

Alternatively, you may want to just update the Enterprise Library to the latest version.


J
Jaseem Abbas

Check the Web.config/App.config file in your project. See if the version numbers are correct.

<bindingRedirect oldVersion="X.X.X.X-X.X.X.X" newVersion="X.X.X.X" />

This worked for me.


This worked for me although it was web.config, not app.config
E
Engin Aydogdu

https://i.stack.imgur.com/7sQHj.jpg


After checking application pool, "Enable 32-Bit Applications" was set to False, but my platform target was x86. Changing it to Any CPU OR x64 fixed my issue.
I
Ievgen

Juntos answer is correct but you should also consider:

For the unity v2.1.505.2 different AssemblyVersion and AssemblyFileVersion attributes are specified:

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

AssemblyFileVersion is used by the NuGet but CLR does not care about it! CLR is going to use only AssemblyVersion!

So your redirects should be applied to a version that specified in AssemblyVersion attribute. So 2.1.505.0 should be used

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
 <assemblyIdentity name="Microsoft.Practices.Unity" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.1.505.0" newVersion="2.1.505.0" />
</dependentAssembly>
</assemblyBinding>

See also: What are differences between AssemblyVersion, AssemblyFileVersion and AssemblyInformationalVersion?


R
Roshana Pitigala

I also got this terrible error and found a solution for this...

Right Click on the Solution name Click Clean Solution Restart Visual Studio Goto project Properties >> Build Change Configuration to Release Start Debugging (F5)

1) , 2)

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

4) , 5)

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

Hope this will help you also.


C
Community

Goto :Solution -> Package

Click on Advanced Tab (Find below the page)

Add your dll to additional assemblies(this way we can add external dlls in sharepoint).


I dont have "Solution -> Package" in my VS2010 project
z
zero323

Not sure if this might help.

Check that the Assembly name and the Default namespace in the Properies in your asemblies match. This resolved my issue which yielded the same error.


Excellent! My dll file name and the namespace were different, I copied the namespace and renamed my dll.
T
Totodile

In my case in the bin folder was a non reference dll called Unity.MVC3 , i tried to search any reference to this in visual studio without success, so my solution was so easy as delete that dll from the bin folder.


M
Mohammad Almasi

I had the same problem i solved it via the instructions below:

open tools menu and select option in options, window go to Projects and Solutions/Web Projects check use the 64bit version of IIS ...

https://i.stack.imgur.com/2kMUv.jpg


S
Sridhar Kommana

Thanks Riddhi M. Following worked for me.

Remove Temporary Files C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files Close VSTS and Open Again Remove and Add the same DLLs (Note: you add the same matching versions)


Spent so long on this and I cannot believe this was the answer. It's usually good go-to solution when you're seeing some odd behavior within VS. Thank you.
J
Joel Martinez

You say you have a lot of projects in your solution ... well, start with one near the top of the build order. Get that one to build and once you figure it out you can apply the same fix to the rest of them.

Honestly, you probably just need to refresh your reference. It sounds like you either updated your version and didn't update the references, or it's a relative path issue if you keep your solution in source control. Just verify your assumptions, and re-add the reference.


b
basit durrani

if you are getting this error message by opening an application on you windows xp it mean first you have installed that app due to its not working without net framework 4 and service pack 3 . you installed both and again you are getting this error so you should reinstall that app again but first uninstall from add and remove

if this not work please dont abuse me . i am also a junior


o
onlyme

Following worked for me.

Remove Temporary Files C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files then right click on Temporary Asp.net Files>properties>security and give total control access to IIS and to all user runing my project

then right click on Temporary Asp.net Files>properties>security and give total control access to IIS and to all user runing my project


C
Creamstout10

This issue happened to me where one of my dependent libraries was compiling a DLL with "Any CPU" when the parent library was expecting a compilation of "x64".


g
gucci

You have to delete Your appname.dll file from your output folder. Cleanup Debug and Release folders. Rebuild and copy to output folder regenerated dll file.


n
nirav

I "Set as Startup Project" the unloaded/unfound library/project.

Then deployed it.

It worked!

I think it couldn't found the .dll because it was not in the assembly at first.


n
nathanchere

Another possible cause: make sure you haven't accidentally given both of the projects the same assembly name in project properties.


This took me hours to figure out.... I accidentally named my unit test project the same name as the main project, so the unit test project dll must have been overwriting the project dll
M
MacGyver

My solution for .NET 4.0, using Enterprise Library 5, was to add a reference to:

Microsoft.Practices.Unity.Interception.dll


u
user3791372

Look out for conflicting references. Even after a clean and rebuild, conflicting references will still cause a problem. My problem was between AForge and Accord. I removed both of the references, and re-added the references re-choosing the particular reference (particular to my case, just Accord).


N
Nicolas Raoul

In my case, none of the proposed answer worked.

Here is what worked for me:

Remove the reference Rename the DLL Import the reference again

The second step was important apparently as it did not work without it.


S
Srinivas Somasundaram

Try checking if the "Copy to Local" property for the reference is set to true and the specific version is set to true. This is relevant for applications in Visual Studio.


g
garryp

I had this today, and in my case the issue was very odd:

  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Owin.Host.SystemWeb" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.1.0" newVersion="3.1.0.0" />
  </dependentAssembly>0.

Note the stray characters at the end of the XML - somehow those had been moved from the version number to the end of this block of XML!

  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Owin.Host.SystemWeb" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" />
  </dependentAssembly>

Changed to the above and voila! Everything worked again.


n
nPcomp

Clean the solution and then right click on the project and select Package

Here increment the Assembly and Assembly file version and rebuild.

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

If that does not work,

1 - Open the solution in file Explorer.

2 - Close Visual Studio.

3 - Remove all bin and obj folders.

4 - Reopen the project and build it.