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
33
votes
6 answers

Implementing MEF with ASP.NET MVC?

I am trying to find out if anyone has any experience or ideas of using MEF (Managed Extensible Framework (Microsoft's new plugin framework) with ASP.NET MVC. I need to create a standard ASP.NET MVC, which I have. But I need to offer additional…
mark smith
  • 20,637
  • 47
  • 135
  • 187
32
votes
4 answers

Is MEF a dependency injection framework?

The recently announced managed extensibility framework (MEF) of .NET 4.0 - is it a dependency injection framework? Will Microsoft Unity from Patterns and Practices be obsolete in 4.0 ? How does MEF compare to a framework like Unity?
bitbonk
  • 48,890
  • 37
  • 186
  • 278
30
votes
4 answers

Prism v4: Unity or MEF?

I downloaded Prism v4 and ran the installer. I went into the directory and ran the two following batch files: Desktop only - Open Modularity With Mef QuickStart.bat Desktop only - Open Modularity With Unity QuickStart.bat When I compile these…
myermian
  • 31,823
  • 24
  • 123
  • 215
29
votes
6 answers

How to integrate MEF with ASP.NET MVC 4 and ASP.NET Web API

How does one integrate Managed Extensibility Framework (MEF) with ASP.NET MVC 4 and ASP.NET Web API in the same project? Consider an example application, with an MVC controller HomeController and a Web API controller ContactController. Both have a…
aknuds1
  • 65,625
  • 67
  • 195
  • 317
25
votes
3 answers

How to reference to assembly in mvc at runtime

In my Asp.Net MVC application, i have some view file (.cshtml) which has reference to an external library which it will be loaded at runtime. so after app started, i load the assembly by Assembly.Load and i register the controllers by my own custom…
Ali Adlavaran
  • 3,697
  • 2
  • 23
  • 47
24
votes
1 answer

Using MEF to import a WPF DataTemplate?

I was looking at MEF as an extensibility framework, and I'm pretty much sold, except for one point: Let's say I want to import both a ViewModel and a View to display it. I think the "right" way to do that is for the MEF part to export a ViewModel…
Scott Whitlock
  • 13,739
  • 7
  • 65
  • 114
24
votes
3 answers

Is MEF OSGi for .NET?

I'm just trying to get my head around the Managed Extensibility Framework (MEF) at the moment and digging into a bit. I have an Eclipse background so in my brain I currently have the equation: MEF =~ OSGi for .NET Based on what I have heard so…
Martin Woodward
  • 11,770
  • 31
  • 45
24
votes
1 answer

Include Nuget dependencies in my build output?

I am building a modular .NET core application that can load extensions at runtime using MEF. I have 2 projects, one is a library that I want to be able to load at runtime, then I have my main application that will do the loading. My library project…
ldam
  • 4,412
  • 6
  • 45
  • 76
23
votes
3 answers

Handle ReflectionTypeLoadException during MEF composition

I am using a DirectoryCatalog in MEF to satisfy imports in my application. However, there are sometimes obfuscated assemblies in the directory that cause a ReflectionTypeLoadException when I try to compose the catalog. I know I can get round it by…
Phil J Pearson
  • 547
  • 6
  • 11
23
votes
3 answers

What are MEF best practices?

What are some best practices for using MEF in your code? Are there any pitfalls to take into account when starting your extensible application? Did you run into anything you should have known earlier?
Sorskoot
  • 10,190
  • 6
  • 55
  • 98
23
votes
3 answers

Add custom editor windows to Visual Studio window panes

My Problem I'm trying to build an extension to Visual Studio that allows code to be edited on a per-function basis, rather than a per-file basis. I'm basically attempting to display code in a similar fashion to Microsoft Debugger Canvas. I'm…
JoshVarty
  • 9,066
  • 4
  • 52
  • 80
22
votes
2 answers

Import property always null (MEF import issue)

I try for some time to get things done using MEF but now, I run into a problem i need help. Description: I have 2 DLL and one EXE file. ClassLibrary1 (LoggerImpl.cs, SomeClass.cs) ClassLibrary2 (ILogger.cs) WindowsApplicationForms1…
ITGoran
  • 442
  • 1
  • 7
  • 18
21
votes
2 answers

PRISM + MEF + MVVM -- Not sure where to really start?

What I'm using: Visual Studio 2010 Microsoft .NET Framework 4 Prism v4 What I am trying to figure out is how to get started with Prism + MEF while maintaining the MVVM pattern. When I go into the Prism Quickstarts, it has a Prism + MEF, but the…
michael
  • 14,844
  • 28
  • 89
  • 177
21
votes
9 answers

Whither Managed Extensibility Framework for .NET?

Has anyone worked much with Microsoft's Managed Extensibility Framework (MEF)? Kinda sounds like it's trying to be all things to all people - It's an add-in manager! It's duck typing! I'm wondering if anyone has an experience with it, positive or…
Andy S
  • 8,641
  • 6
  • 36
  • 40
20
votes
1 answer

MEF Plugins and EF CodeFirst - How?

Background: We have a project with many modules. We're using EntityFramework 4.2 with FluentAPI (CodeFirst). There is a central project named Diverto.ORM.EntityFramework.SQLServer which contains partial classes that build the context using the…