Question

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

Was it helpful?

Solution 2

Well, my solution is ugly, but works and i'll go forward for MEF in future (maybe). For now, i added such thing:

if(Path.GetFileNameWithoutExtension(dll)==Assembly.GetCallingAssembly().GetName().Name)
    continue;

Thanks everybody for awesome replies

EDIT: I came up to more elegant solution, here it is:

var frameworkAssemblies =
                from file in new DirectoryInfo(frameworkDirectory).GetFiles()
                where (file.Extension.ToLower() == ".dll" || file.Extension.ToLower() == ".exe")
                && !AppDomain.CurrentDomain.GetAssemblies().Select(a => a.GetName().Name).Contains(file.GetFileNameWithoutExtension())
                select Assembly.LoadFrom(file.FullName);

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top