Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not load type from assembly

Tags:

c#

.net

Before you say anything, I have read the previously asked questions about this issue. The answers there did not fix my problem.

It's pretty simple, I guess, if you know the answer. Here's my problem:

I've got a solution with several projects, I'm creating a plugin-based application where I use Reflection to load all assemblies. This part goes fine, I load all my assemblies like so

        var filePaths = Directory.GetFiles(@"C:\CustomerServiceModule\", "*.dll", SearchOption.AllDirectories).Where(n => n.Contains("bin"));
        foreach (var f in filePaths)
        {
            Assembly.LoadFile(f);
        }

Now I want to create an instance of a type, so I can work with it:

            var assembly = AppDomain.CurrentDomain.GetAssemblies().Where(a => a.ManifestModule.Name == "Kayako.dll").SingleOrDefault();
            var name = assembly.GetTypes();
            var type = assembly.GetType("Kayako.KayakoData");
            var lol = Activator.CreateInstance(type);

This goes badly because inside KayakoData I have this:

KayakoService _service = new KayakoService("xxx", "yyy", "zzz");

This service is an assembly that works, I've used it before. Version number is fine, there's nothing in the GAC that overrides it, I can't see any errors using Assembly Binding Log Viewer. I still get this error:

[System.LoadTypeException]{"Could not load type 'KayakoRestAPI.KayakoService' from assembly 'KayakoRestAPI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.":"KayakoRestAPI.KayakoService"}

Anyone have any bright ideas? I've been staring myself blind at this. If I remove the service part from KayakoData the whole thing works, but I really need to run the service.

like image 817
MartinNielsen Avatar asked Feb 23 '23 21:02

MartinNielsen


2 Answers

Quote from the documentation of the LoadFile method:

Use the LoadFile method to load and examine assemblies that have the same identity, but are located in different paths. LoadFile does not load files into the LoadFrom context, and does not resolve dependencies using the load path, as the LoadFrom method does. LoadFile is useful in this limited scenario because LoadFrom cannot be used to load assemblies that have the same identities but different paths; it will load only the first such assembly.

Conclusion: try LoadFrom in order to load dependent assemblies as well.

like image 77
Darin Dimitrov Avatar answered Mar 07 '23 00:03

Darin Dimitrov


You just need to change the version of .dll from assembly info of that project. Then rebuild your solution.

like image 27
Yatin Nanda Avatar answered Mar 07 '23 01:03

Yatin Nanda