mardi 2 avril 2019

Assembly.CreateInstance isn't working predictably

When I try to late bind to a class implementing a common interface, I get our old friend "Object reference not set to an instance of an object".

So I have a VS solution that contains three projects. As an example, I'll name them; SERVICE (a windows service), PLUGIN (an assembly with a single class in it), and COMMON (an assembly that contains a common API in the form of an interface).

I've seen several posts, and several implementations (and people having trouble too), but I can't get this to work.

My PLUGIN class has a default constructor and implements the IPlugin interface which is referenced from the COMMON assembly. My SERVICE also references the same COMMON assembly to make use of the Interface and call the methods in the instance of my PLUGIN. I referenced the projects in VS and that didn't work.

My binaries on disk look something like this:

c:\service\service.exe
          \common.dll
c:\service\plugins\plugin.plug.dll

I have also tried referencing by browsing to the same common.dll directly, still no deal. I made local copies of the common.dll to the plugin folder as well, still no dice.

COMMON.cs

public interface IPlugin {
   void Method();
}

PLUGIN.cs

public class Plugs : IPlugin {
   public Plugs() {}

   public void Method() {}
}

SERVICE.cs

foreach (var pluginFile in Directory.GetFiles(basePath, "*.dll"))
{
    var assembly = Assembly.LoadFile(pluginFile);
    var types = assembly.GetExportedTypes().Where(t => t.GetInterfaces().Contains(typeof(IPlugin)));
    foreach (var type in types)
    {
        IPlugin plugin = (IPlugin)assembly.CreateInstance(type.FullName);
        plugin.Method();
    }
}

I don't get an error on CreateInstance, presumably because it's returning null and then calling .Method() is where it's crashing.

I have found that running the SERVICE.exe in debug with the IDE attached, it doesn't work. However if I breakpoint in the code where the SERVICE creates the instance, it does work! I don't know if that's VS doing something smart, but it will not work when running as a standalone .exe, so I must be doing something wrong.





Aucun commentaire:

Enregistrer un commentaire