Questions tagged [mef]

The Managed Extensibility Framework (MEF) simplifies the design of extensible and modular applications, and is a standard component of Microsoft .NET 4.0 and Silverlight 4.0

MEF provides mechanisms for discovery and composition of modular component parts allowing a simple extension mechanism for .NET 4.0/Silverlight 4.0 applications. Through the use of the ExportProvider abstraction, MEF has the ability to easily discover and manage instances of your Exported types which easily allows you to bind to various extension points within your application.

Hello World Sample Using the default Attributed Programming Model, MEF allows you to decorate your target types with attributes which define contract definitions for how the part is composed. E.g.

public interface IMessage
{
  void Display();
}

[Export(typeof(IMessage))]
public class HelloWorldMessage : IMessage
{
  public void Display()
  {
    Console.WriteLine("Hello World");
  }
}

With that simple implementation, I am providing a an export definition of my HelloWorldMessage part, which I can use to compose my consumer:

public class Display
{
  [Import]
  public IMessage Message { get; set; }

  public void Display()
  {
    Message.Display();
  }
}

Using interfaces (contracts) this way allows me to maintain a decoupled implementation.

Make sure you visit the MEF CodePlex site for more detailed information works, and ensure you search Stack Overflow for possible answers before you post questions.

2226 questions
14
votes
2 answers

Does MEF require .NET 4?

I am using Visual Studio 2010, try to create a MEF application. Does this require .NET 4.0 or can I target .NET 2.0?
Alan
  • 141
  • 1
  • 3
14
votes
3 answers

MEF vs Mono.AddIn

I'm developing a .NET 3.5 C# desktop application. It should be extensible via plug-ins. Are there any articles etc. discussing the differences between MEF and Mono.AddIn to help me make an informed decision? Or even better have you got experience…
Patrick Wolf
  • 2,530
  • 2
  • 28
  • 27
13
votes
1 answer

What is the best way to marshall data in/out of the plug-ins?

I am building my workstation Agent application using MEF and EntityFramework 4. The application is a simple agent that runs on a computer with a plug-in architecture (and many plugins in the form of .dll files). Each plug-in will query their own…
s0ftimage
  • 175
  • 8
13
votes
4 answers

Prism/MVVM (MEF/WPF): Exposing navigation [Menu's for example] from modules

I am starting my first foray into the world of Prism v4/MVVM with MEF & WPF. I have sucessfully built a shell and, using MEF, I am able to discover and initialise modules. I am however unsure as to the correct way to provide navigation to the views…
Martin Robins
  • 6,033
  • 10
  • 58
  • 95
13
votes
1 answer

What is a suitable pattern for injecting loggers within dynamically-discovered .NET Core class libraries called from ASP.NET Core web apps?

Overview I'm trying to port a number of projects based on the .NET Framework to .NET Core. This involves porting a number of class libraries as well as top-level console/web applications that consume these libraries. Part of my requirements is that…
Tagc
  • 8,736
  • 7
  • 61
  • 114
13
votes
5 answers

Help getting started with MEF

I was reading somewhere that with MEF I can simply drop a dll into a directory and my application (with some MEF magic) will be able to read it and execute the code in it? Hopefully only classes that implement an interface that I define?? Can…
SteveCl
  • 4,529
  • 6
  • 29
  • 38
13
votes
1 answer

MEF not detecting plugin dependencies

I have a problem with MEF and using a plugins folder. I have a main app that supports plugins via MEF. The main app does not reference the assemblies containing the .NET Task type for multithreading but one or more of the plugins do. The plugins are…
Dean Lunz
  • 968
  • 7
  • 28
13
votes
2 answers

MEF: What if I have multiple exports but need only one import?

I'm trying to wrap my mind around MEF. There is one thing I don't understand. Assume that I have an interface, named ISomething, which is a contract, and I have more than one assemblies in a folder that contains my application, and I have no idea…
Venemo
  • 18,515
  • 13
  • 84
  • 125
13
votes
1 answer

Caliburn.Micro and MEF on wpf

I am just learning WPF and Caliburn.Micro. I am following the code that presented here: http://caliburnmicro.codeplex.com/wikipage?title=Customizing%20The%20Bootstrapper&referringTitle=Documentation Apparently this code is for Silverlight but my…
mans
  • 17,104
  • 45
  • 172
  • 321
12
votes
2 answers

C# MEF usage with static classes

I have a static class in my solution which is used to work with various assemblies. I want to link them through MEF, so I made a field in a class. [Import(typeof(A))] static private A _a1; Then I have a method to which I pass assembly name as…
noaRAVE
  • 432
  • 1
  • 5
  • 19
12
votes
2 answers

MEF and exporting based on Metadata

OK I'm pretty sure this is something blindingly obvious but I'm not finding it. I'm trying to export a object from MEF container based on it's Metadata. I've seen this done in tutorials like this…
Ingó Vals
  • 4,788
  • 14
  • 65
  • 113
12
votes
1 answer

How to discover new MEF parts while the application is running?

I'm using MEF to load plugins in my app. Everything works, but I want new parts to be discovered when they are dropped into my app folder. Is this possible? DirectoryCatalog has a Changed event but I'm not sure how it works. This is my code right…
David Moreno García
  • 4,423
  • 8
  • 49
  • 82
12
votes
5 answers

Replace assembly at runtime with .NET

Is there a way with a plugin system (I would use with an IoC container) to load one version of an assembly at runtime and then replace that DLL while the AppDomain is running? I don't want to restart the application. Does MEF do something like this?
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
12
votes
2 answers

ASP.NET MVC: Views using a model type that is loaded by MEF can't be found by the view engine

I'm attempting to create a framework for allowing controllers and views to be dynamically imported into an MVC application. Here's how it works so far: I'm using .NET 4, ASP.NET MVC 3 RC and the Razor ViewEngine Controllers are exported and…
Daniel Schaffer
  • 56,753
  • 31
  • 116
  • 165
12
votes
4 answers

Why am I unable to debug a dynamically loaded assembly?

I am working on a Web API project that uses an in-house mocking framework that allows to intercept and modify the responses from the controllers. It uses MEF to loading an assembly that contains code that is executed if some preconditions are…
Antoine Aubry
  • 12,203
  • 10
  • 45
  • 74