C
C#7mo ago
_read_only_

MEF No exports were found that match the constraint

Hi, i'm trying to implement MEF in my WPF app, however i'm getting this error, not sure why anymore. Everything looks correct for me This is my plugin:
public interface IPlugin
{
void Process();
string Name { get; }
string Description { get; }
}

[Export(typeof(IPlugin))]
public class Plugin : IPlugin
{
public string Name => "Plugin";
public string Description => "Plugin";

public void Process()
{
Console.WriteLine("Plugin");
}
}
public interface IPlugin
{
void Process();
string Name { get; }
string Description { get; }
}

[Export(typeof(IPlugin))]
public class Plugin : IPlugin
{
public string Name => "Plugin";
public string Description => "Plugin";

public void Process()
{
Console.WriteLine("Plugin");
}
}
and this is part of my WPF App.xaml.cs
public interface IPlugin
{
void Process();
string Name { get; }
string Description { get; }
}

public partial class App : Application
{
[Import(typeof(IPlugin))]
public IPlugin plugin;

protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

string plugins = System.IO.Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"WPFAPP", "plugins");

if (!Directory.Exists(plugins))
{
Directory.CreateDirectory(plugins);
}

var catalog = new DirectoryCatalog(plugins);

try
{
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
catch (Exception ex)
{
Console.WriteLine($"ex: {ex}");
}
}
}
public interface IPlugin
{
void Process();
string Name { get; }
string Description { get; }
}

public partial class App : Application
{
[Import(typeof(IPlugin))]
public IPlugin plugin;

protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

string plugins = System.IO.Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"WPFAPP", "plugins");

if (!Directory.Exists(plugins))
{
Directory.CreateDirectory(plugins);
}

var catalog = new DirectoryCatalog(plugins);

try
{
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
catch (Exception ex)
{
Console.WriteLine($"ex: {ex}");
}
}
}
is there something wrong?
6 Replies
canton7
canton77mo ago
Note that your two IPlugin interfaces are different types So the one which your plugin claims that it implements is a different interface to the one which your WPF app is trying to find implementations of
_read_only_
_read_only_OP7mo ago
looks like its the same when I leave IPlugin interface in wpf app, and add project reference to plugin to use same interface
canton7
canton77mo ago
So your plugin references your WPF app, but your WPF app is trying to load the plugin? That's a circular dependency Normally you need to have another assembly, which contains the common types that both your app and your plugin rely on
_read_only_
_read_only_OP7mo ago
just as a quick test, also tried with creating another shared dll this is what I have currently
namespace Wpf.Shared
{
public interface IPlugin
{
void Process();
string Name { get; }
string Description { get; }
}
}
namespace Wpf.Shared
{
public interface IPlugin
{
void Process();
string Name { get; }
string Description { get; }
}
}
namespace Wpf.Private;

[Export(typeof(IPlugin))]
public class Plugin : IPlugin
{
public string Name => "Plugin";
public string Description => "Plugin";

public void Process()
{
Console.WriteLine("Plugin");
}
}
namespace Wpf.Private;

[Export(typeof(IPlugin))]
public class Plugin : IPlugin
{
public string Name => "Plugin";
public string Description => "Plugin";

public void Process()
{
Console.WriteLine("Plugin");
}
}
public partial class App : Application
{
[Import(typeof(IPlugin))]
public IPlugin plugin;

protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

string plugins = System.IO.Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"WPFAPP", "plugins");

if (!Directory.Exists(plugins))
{
Directory.CreateDirectory(plugins);
}

var catalog = new DirectoryCatalog(plugins);

try
{
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
catch (Exception ex)
{
Console.WriteLine($"ex: {ex}");
}
}
}
public partial class App : Application
{
[Import(typeof(IPlugin))]
public IPlugin plugin;

protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

string plugins = System.IO.Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"WPFAPP", "plugins");

if (!Directory.Exists(plugins))
{
Directory.CreateDirectory(plugins);
}

var catalog = new DirectoryCatalog(plugins);

try
{
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
catch (Exception ex)
{
Console.WriteLine($"ex: {ex}");
}
}
}
both reference .Shared hi @Lex Li you recommended me MEF yesterday, do you have any idea whats wrong here
Lex Li
Lex Li7mo ago
I will stick to working samples like https://github.com/bezzad/MEF-Sample instead of rolling out my own.
GitHub
GitHub - bezzad/MEF-Sample: Sample of Managed Extensibility Framewo...
Sample of Managed Extensibility Framework (MEF). Contribute to bezzad/MEF-Sample development by creating an account on GitHub.
_read_only_
_read_only_OP7mo ago
chatgpt for the win lol, idk how this happened but its working now
However, I noticed that the Plugin class is using the [Export] attribute from the System.Composition namespace, while the App class is using the [Import] attribute from the System.ComponentModel.Composition namespace. These are two different versions of MEF and they're not compatible with each other.
However, I noticed that the Plugin class is using the [Export] attribute from the System.Composition namespace, while the App class is using the [Import] attribute from the System.ComponentModel.Composition namespace. These are two different versions of MEF and they're not compatible with each other.
Want results from more Discord servers?
Add your server