Yoav
Yoav

Reputation: 2077

Plugin architecture - Error when loading assemblies in runtime

I've written an application which has an abstract class. I want to support implementations of this class as plugins.

This is the abstract class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;

namespace Stub.Logic {
    public abstract class Connection {
        …stuff
    }
}

This is an implementation:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;
using Stub.Logic;

namespace MqConnection {
    public class MqConnection : Connection {
        …stuff
    }
}

The plugin is dropped in a folder where it is read and loaded.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace Stub.Logic {
    public class PluginManager {
        private static List<Type> connectionTypes = new List<Type>();

        public static void LoadConnectionTypes(string path) {
            DirectoryInfo dllDirectory = new DirectoryInfo(path);
            FileInfo[] dlls = dllDirectory.GetFiles("*.dll");
                foreach (FileInfo dllFileInfo in dlls) {
                    try {
                        Assembly assembly = Assembly.LoadFrom(dllFileInfo.FullName);
                        connectionTypes.AddRange(**assembly.GetTypes()**);
                    } catch (Exception ex) {
                        if (ex is System.Reflection.ReflectionTypeLoadException) {
                        var typeLoadException = ex as ReflectionTypeLoadException;
                        var loaderExceptions = typeLoadException.LoaderExceptions;
                        TextWriter writer = new StreamWriter(@"C:\Users\yoav.benzvi\Desktop\error.txt");
                        writer.WriteLine(loaderExceptions[0].ToString());
                        writer.Close();
                    }
                }
            }
        }
    }
}

Its failing on assembly.GetTypes() call. This is the error I’m getting:

System.IO.FileNotFoundException: Could not load file or assembly 'StubLogic, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified. File name: 'StubLogic, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'

Do I need to have StubLogic.dll in the plugin folder? Seems redundant since its already part of the application, so what am I doing wrong?

Edit: Adding the reference properties:
enter image description here

Edit2: Apologies to all. The code is working. Issue was with my bad testing. Again, apologies to anyone who wasted their time here.

Upvotes: 0

Views: 354

Answers (3)

Yoav
Yoav

Reputation: 2077

Apologies to all. The code is working. issue was with my bad testing. Again, apologies to anyone who wasted their time here.

Upvotes: 0

Aghilas Yakoub
Aghilas Yakoub

Reputation: 29000

I suggest you this code

I modified your path with

DirectoryInfo(Path.Combine(Environment.CurrentDirectory, "YourDirectoryRepository"));

So

 DirectoryInfo dInfo = new DirectoryInfo(Path.Combine(Environment.CurrentDirectory, "YourDirectoryRepository"));
 FileInfo[] files = dInfo.GetFiles("*.dll");
 List<Assembly> plugInAssemblyList = new List<Assembly>();

 if (null != files)
 {
      foreach (FileInfo file in files)
      {
           plugInAssemblyList.Add(Assembly.LoadFrom(file.FullName));
      }
 }

Upvotes: 1

alex
alex

Reputation: 12654

Make sure that plugin references the same version of StubLogic assembly that you have, or that "Use specific version" is false for StubLogic reference in a plugin assembly project.

Upvotes: 1

Related Questions