Problem loading a Plugin that implements an interface

A

auad

hi,

I'm having a problem with plugins....as follows:
I have 3 projects: 2 class libraries and 1 Windows App

(Project 1: ClassLibrary)
I have a plugin interface:
--------------------------------------------------------------------
namespace API
{
public interface IPlugin
{
string GetPluginName(); // returns the plugin name
void Setup(); // displays the configuration
form
}
}

(Project 2: ClassLibrary)
a plugin class that implements the interface:
a plugin configuration form:
--------------------------------------------------------------------
namespace Plugins
{
public class Plugin1: IPlugin
{
string GetPluginName()
{
//...
}

void Setup()
{
frmSetup form = new frmSetup();
form.ShowDialog();
}
}

partial class frmSetup : Form
{
public frmSetup()
{
InitializeComponent();
}
}
}

(Project 3: WinApp)
load a plugin:
--------------------------------------------------------------------
namespace WinApp
{
public partial class frmPrincipal: Form
{
public frmPrincipal()
{
InitializeComponent();
Assembly assembly = Assembly.LoadFile("My_Plugin.dll");
Type[] types = assembly.GetTypes(); // This line throws an
error
}
}
}

Error: Unable to load one or more of the requested types. Retrieve the
LoaderExceptions property for more information.

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

I think that when I run the GetTypes method, the compiler looks in my
plugin assembly and finds that it references my plugin Interface, which
is in another assembly, but it doesn't find it, so it throws this
error.
I tried to solve it by using the GetType method passing the full path
to my plugin class as parameter, but it always returns null...On the
other hand, if I pass the full path for the configuration form class
(which is in the same project, and isn't my objective) it works
fine....

Can someone help me?

Tks
 
M

Markus Stoeger

auad said:
Assembly assembly = Assembly.LoadFile("My_Plugin.dll");

{System.IO.FileNotFoundException: Could not load file or assembly 'API,

It's looking for a file called API.dll instead of My_Plugin.dll. Could
it be that you have renamend the API.dll to My_Plugin.dll after compiling?

I think I remember a similar problem.. when you compile an A.dll, rename
it to B.dll and try to load it dynamically, it will say it cannot find
A.dll. I have never tried to find out why, but it looks like the
assembly name is stored within the assembly.dll and you shouldnt rename it.

I could be all wrong, no time to test this right now, but see if
renaming your my_plugin.dll back to its original name (api.dll) helps.

hth,
Max
 
A

auad

sorry Max, I think I wrote something wrong...
When I load the plugin assembly which contains a class that implements
the interface that is defined in API.dll, .Net also looks for the
API.dll because it is referenced in the plugin assembly and then for
some reason it doesn't find it....I've already tried to embed the
API.dll assembly into the plgin assembly, but I got the same error...
Can you help me?

Tks,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top