Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
Have you seen this? stackoverflow.com/questions/1057853/… –  Halvard Apr 15 at 12:43
    
@Halvard Yes, i do. But i don't have different versions, i have trouble with loadfrom, that loads me similar assembly twice –  Alex Voskresenskiy Apr 15 at 12:52
    
Does it load the same assembly twice or does it load a similar assembly twice? –  Halvard Apr 15 at 12:58
1  
Take a look at this –  Sriram Sakthivel Apr 15 at 12:59
    
@Halvard forgot to mention, it loads similar assembly twice from different place: first it loads it when starts server, and the second cope is located in plugin folder and autoloaded via LoadFrom –  Alex Voskresenskiy Apr 15 at 13:02
show 2 more comments

2 Answers 2

up vote 0 down vote accepted

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);
share|improve this answer
add comment

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.

share|improve this answer
    
Nope... All my projects are build with .Net 4.5. I use Simplee Injector after i get instances of plugins. What also can MEF provide me? I always though that MEF is kind of IoC-container, am i right? –  Alex Voskresenskiy Apr 15 at 12:55
2  
MEF is specifically for plugins - it does the loading of assemblies and discovery of interface implementations with support for using attributes to configure the plugins. It is worth reading the front page at least. For your specific issue, use ndepend or ILSpy or .NET Reflector to examine your DLL and application and verify the ServiceCore.dll that is being pulled in for each. The error is because the dll is being loaded 2x from different locations. –  Brandon Apr 15 at 15:14
1  
@AlexVoskresenskiy common misconception. MEF is NOT IOC. IOC is NOT a plug in tool. For example you should look at the Lazy<T, TMetadata> class that comes with MEF. –  Aron Apr 15 at 16:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.