Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6677751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:09:36+00:00 2026-05-26T04:09:36+00:00

Let’s say I have 5 separate assemblies with the following (assume the class name

  • 0

Let’s say I have 5 separate assemblies with the following (assume the class name is different in each):

[Export(typeof(IService))]
public class Service: IService
{
    // ...
}

And I have a class that will be a composite of these in my main assembly

public class CompositeService : IService
{
    public CompositeService(IEnumerable<IService> services)
    {
        // ...
    }
}

What I would like to do is have the Unity container resolve the CompositeService for the IService and have the MefContrib extension for Unity go and find the 5 other exports and inject them into CompositeService‘s constructor.

The problem is that you can’t have N instances for a nameless unityContainer.RegisterType<IService> nor can you for named instances if they all have the same name.

I think I’m missing something simple in the combination of the 2 technologies (Unity + MEF) via the third (MefContrib) but can’t seem to pick up on what it is.

Is this possible or is there a workaround? Eventually, I’m going for full bi-directional dependency injection and dynamic component discovery.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:09:36+00:00Added an answer on May 26, 2026 at 4:09 am

    I think what is likely the best approach is to flip this around. Instead of trying to register your components via Unity, you actually leave the discovery of these parts to MEF. MEFContrib includes an Unity integration mechanism that allows your MEF composed parts to be injected into Unity components. This was original detailed at Piotr WŁodek’s blog, whereby he also gives you a sample. Essentialy, the way it works is you can use a series of extension methods on your UnityContainer to register your catalogs. Internally, it will create the appropriate extension and wire up your container.

    Here is a quick and dirty example, we’ll create some interfaces:

    public interface IUnityComponent
    {
      IEnumerable<IMefComponent> MefComponents { get; }
    }
    
    public interface IMefComponent
    {
      string Name { get; }
    }
    

    And then some sample parts which we’ll export (via MEF):

    [Export(typeof(IMefComponent))]
    public class MefComponent1 : IMefComponent
    {
      public string Name { get { return "MEF Component 1"; } }
    }
    
    [Export(typeof(IMefComponent))]
    public class MefComponent2 : IMefComponent
    {
      public string Name { get { return "MEF Component 2"; } }
    }
    

    Now, we’ll create another part (this will be created via Unity):

    public class UnityComponent : IUnityComponent
    {
      public UnityComponent(IEnumerable<IMefComponent> mefComponents) 
      { 
        // mefComponents should be provided from your MEF container.
        MefComponents = mefComponents;
      }
    
      public IEnumerable<IMefComponent> MefComponents { get; private set; }
    }
    

    To wire it all up, we simply need to use the RegisterCatalog extension method on your UnityContainer (import MefContrib.Integration.Unity after you’ve added a reference to MEFContrib):

    var container = new UnityContainer();
    
    // Register the catalog - this handles MEF integration.
    container.RegisterCatalog(new DirectoryCatalog("."));
    
    // Register our Unity components.
    container.RegisterType<IUnityComponent, UnityComponent>(new ContainerControlledLifetimeManager());
    

    Now you should be able to grab the instance and enumerate the MEF-provided parts:

    // Grab an instance of our component.
    var instance = container.Resolve<IUnityComponent>();
    foreach (var mefComponent in instance.MefComponents)
    {
      Console.WriteLine(mefComponent.Name);
    }
    

    note: 100% untested.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have the following models class Photo(models.Model): tags = models.ManyToManyField(Tag) class Tag(models.Model):
Let's assume we have the following structure: index.php config.inc.php \ lib \ lib \
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have window.open (without name parameter), scattered in my project and I
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say I have two assemblies: BusinessLogic and Web. BusinessLogic has an application setting
Let say I have the following desire, to simplify the IConvertible's to allow me

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.