Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not load type `A` from assembly `Not.Containing.Type.A`

EDIT: I haven't mentioned one important thing - application which loads my assemblies are actually not in same directory (as other dlls). After fiddling with Fusion log, I noticed that loading of dll are behaving differently than I previously thought. (Yup, I should first RTFM, shame on me)

  • C:\Test\appLoadingStuff.exe
  • C:\Lib\Acme.Application.dll
  • C:\Lib\Acme.Data.dll
  • ...

.NET is probing application base (besides GAC and stuff; directory where loading app is - C:\Test\), and does not care about location where loaded dll are stored (other directory).


While using the .NET framework I found myself getting a ReflectionTypeLoadException exception:

System.TypeLoadException

Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

Could not load type 'Acme.Data.Foo' from assembly 'Acme.Data.Dao, Version=1.1.0.4, Culture=neutral, PublicKeyToken=null'.":"Acme.Data.Foo

I have, for simplicity, 3 assemblies:

  • Acme.Application my main assembly
  • Acme.Data my data objects (referenced by 1st one)
  • Acme.Data.Dao my data access objects (referenced by 1st one)

And there's another application, which actually loads my main assembly. All .dll files are in same directory.

As you could expect, type Acme.Data.Foo is living in assembly Acme.Data. Despite this, .NET is trying to find it in another assembly Acme.Data.Dao - which fails, as the type is not there.

I can't figure out what is making .NET looking for that particular type in wrong assembly. Exception is thrown immediately when accessing types on assembly:

System.Reflection.RuntimeAssembly assembly = Assembly.LoadFile("C:\Lib\Acme.Application.dll")
var types = assembly.GetTypes(); // -> explodes

When I tried to check referenced assemblies using:

assembly.GetReferencedAssemblies()

I can clearly see my desired assembly in list.

There's no assembly redirect (and as far I know, this affects only version). Versions of assemblies are correct.

What else should I look for?

like image 941
Zdeněk Avatar asked May 09 '16 16:05

Zdeněk


People also ask

Can not load file or assembly?

In summary if you get the "Could not load file or assembly error", this means that either your projects or their references were built with a reference to a specific version of an assembly which is missing from your bin directory or GAC.

Could not load type from assembly because the parent does not exist?

This means you have done something that caused a different type or assembly to be used. Make sure that the types you are using and the assemblies being loaded are really what you need.

Could not load Type from assembly GAC?

This usually happens when you have one version of your assembly deployed in the GAC but has not been updated with new classes that you might have added on the assembly on your IDE. Therefore make sure that the assembly on the GAC is updated with changes you might have made in your project.

Could not load file or assembly Publickeytoken null or one of its dependencies?

This error usually means that the assembly was not found. Try verifying that the file exists in the directory where your application is running.


1 Answers

As I stated on edited question, I missed to mention very important property of what I was trying to do. My assembly was loaded by another application which was not in same path as loaded assemblies.

.NET is probing directory of running application, not directories where loaded dlls are. If one needs to handle such case, it is necessary to implement event handler AppDomain.AssemblyResolve to take care of dependencies of loaded dll.

More reading:

  • How the Runtime Locates Assemblies (notice section Locating the Assembly through Probing)
  • AppDomain.AssemblyResolve Event

Thanks to anyone who participated on this question and sorry I didn't share such crucial detail (tbh, I didn't expect this little detail matters - now i know).

like image 196
Zdeněk Avatar answered Oct 06 '22 23:10

Zdeněk