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

  • Home
  • SEARCH
  • 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 7162375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:43:28+00:00 2026-05-28T13:43:28+00:00

I am using container hierarchy to control IDisposable parts lifetime. Child container is attached

  • 0

I am using container hierarchy to control IDisposable parts lifetime. Child container is attached to the filtered catalog that contains non-shared parts. Here is a code snippet:

[Export(typeof(ITest)), PartCreationPolicy(CreationPolicy.NonShared)]
class Test : ITest, IDisposable
{
    public void Dispose() {}
}

public interface ITest {}

class Program
{
  static void Main()
  {
    // parent container to hold shared disposable parts
    var cat = new AssemblyCatalog(typeof(Program).Assembly);
    var parent = new CompositionContainer(cat);

    // child container to hold non-shared disposable parts
    var nsCat = CreateNonSharedPartCatalog(cat);
    var child = new CompositionContainer(nsCat, parent);

    // no cardinality mismatch exception: exactly one export found
    var exp = child.GetExport<ITest>();

    // lazy exports: count == 2 -- why?
    var exports = child.GetExports<ITest>();
    Console.WriteLine("Exports count = {0}", exports.Count());
  }

  static ComposablePartCatalog 
    CreateNonSharedPartCatalog(ComposablePartCatalog cat)
  {
    return new FilteredCatalog(cat,
      def => def.Metadata.ContainsKey(
        CompositionConstants.PartCreationPolicyMetadataName) &&
      ((CreationPolicy)def.Metadata[
        CompositionConstants.PartCreationPolicyMetadataName]) == 
          CreationPolicy.NonShared);
  }
}

(FilteredCatalog class is the same that mentioned in MEF documentation).

GetExport doesn’t throw cardinality mismatch exception which indicates that there is no ambiguity (exactly one export is found). But to my surprise, GetExports() returns 2 lazy exports instead of 1.

I this a bug or this behavior is by design? How could I set up child container so that GetExports returns one export in this sample?

  • 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-28T13:43:29+00:00Added an answer on May 28, 2026 at 1:43 pm

    this is a known limitation of container hierarchies in MEF using filtered catalogs. I believe (but can’t confirm right now) that setting the import source:

    [ImportMany(Source=ImportSource.Local)]
    public IEnumerable<ITest> Tests { get; set; }
    

    Should give the behaviour you want (but only in MEF2/.NET 4.5 Developer Preview).

    Hope this helps!

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

Sidebar

Related Questions

I am using Ajax tab container control with 3 tabs. I have placed a
It was recommended to me that, when using an IOC container, I should change
My task is to create a parent-child hierarchy file using perl. Sample Input file
I have an xml snippet that contains an object hierarchy: doc = \ <RootObj
I have a core data model, using the database store, that contains entities with
What is the best way to transparently inject dependencies (using IOC container) to user
i'm trying to insert an image in a div container using css and html,
How do we inject IRegionManager in the ViewModel using MEF Container. I have to
I have a table of frequently updated information. This is presented using a container
I've recently started using an IoC container for the first time, but I'm not

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.