Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load assembly correctly

I develop a system with plugins, which loads assemblies at runtime. I have a common interface library, which i share between server and its plugins. But, when i perform LoadFrom for plugin folder and try to find all types, which implement common interface IServerModule i get runtime exception:

The type 'ServerCore.IServerModule' exists in both 'ServerCore.dll' and 'ServerCore.dll'

I load plugins like this:

foreach (var dll in dlls)
{
            var assembly = Assembly.LoadFrom(dll);
            var modules = assembly.GetExportedTypes().Where(
                type => (typeof (IServerModule)).IsAssignableFrom(type)
                && !type.IsAbstract &&
                !type.IsGenericTypeDefinition)
                .Select(type => (IServerModule)Activator.CreateInstance(type));
            result.AddRange(modules);
}

How can i deal with this trouble?

I'll be gratefull for any help

like image 563
Alex Voskresenskiy Avatar asked Apr 15 '14 12:04

Alex Voskresenskiy


1 Answers

Inspect the problem DLL and its dependencies. Chances are good that it is pulling in ServerCore.dll from a different version of .NET than your main application.

I recommend you use MEF if you want to do plugins.

like image 178
Brandon Avatar answered Oct 26 '22 14:10

Brandon