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 987425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:28:23+00:00 2026-05-16T05:28:23+00:00

I’m trying to figure out why one of my objects does not seem to

  • 0

I’m trying to figure out why one of my objects does not seem to be satisfying its Import. I think it may be the way I am using the container.ComposeParts() function but I have not been able to find much documentation with regards to it.

I have four projects
MEF.Service.Contracts
MEF.Service.Messaging (implements contracts)
MEF.Service.Core
MEF.Service.Console

MEF.Service.Contracts contains a simple interface called IMessageService with one function

public interface IMessageService
{
    void SendMessage(string message);
}

MEF.Service.Messaging has one implementation of the above

[Export(typeof(IMessageService))]
public class ConsoleMessageService : IMessageService
{
    #region IMessageService Members

    public void SendMessage(string message)
    {
        Console.WriteLine(message);
    }

    #endregion
}

MEF.Service.Core has a class called ServiceManager that imports available services. In this example the IMessageService

public class ServiceManager
{
    [Import]
    public IMessageService MessageService { get; set; }
}

Finally, in the MEF.Service.Console application project I am creating the MEF container using a DirectoryCatalog. Then I create an instance of ServiceManager and call its MessageService property.
However at this point it fails with an object reference error.

Here is the code in the MEF.Service.Console project

class Program
{
    static void Main(string[] args)
    {
        Program p = new Program();
        p.Run();
    }

    public void Run()
    {
        Compose();

        ServiceManager manager = new ServiceManager();
        manager.MessageService.SendMessage("Test Message");

        Console.ReadLine();
    }

    private void Compose()
    {
        var catalog = new DirectoryCatalog(@".\");
        var container = new CompositionContainer(catalog);
        container.ComposeParts(this);
    }
}

MEF.Service.Console has references to the other three projects just to ensure the dlls are in the same folder during runtime.

I’ve examined the catalog and container after they are initialized and it does contain my ConsoleMessageService export as a part.

I’m trying to figure out why my [Import] MessageService in my ServiceManager is not getting satisfied.

Any help or pointers on composing parts and how Imports get satified would be appreciated.

  • 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-16T05:28:24+00:00Added an answer on May 16, 2026 at 5:28 am

    I’m pretty new to MEF myself, but what I see here is this: you don’t have the IMessageService as an instance of your main app – instead, it’s on a separate object ServiecManager, that you create dynamically after the container.ComposeParts has been called…

    My guess: MEF doesn’t get a chance to satisfy the import on the ServiceManager since that object isn’t around at the time of the composition.

    Suggestion: add a IMessageService instance to your main app and let MEF handle the import, and then pass that instance into your ServiceManager – something like this:

    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            p.Run();
        }
    
        [Import]
        IMessageService MessageService { get; set; }
    
        public void Run()
        {
            Compose();
    
            ServiceManager manager = new ServiceManager(MessageService);
            manager.MessageService.SendMessage("Test Message");
    
            Console.ReadLine();
        }
    
        private void Compose()
        {
            var catalog = new DirectoryCatalog(@".\");
            var container = new CompositionContainer(catalog);
            container.ComposeParts(this);
        }
    }
    

    I’m pretty sure if you do it this way, then MEF will be able to satisfy the import and you’ll have yourself a MessageService instance which you can then pass into the ServiceManager.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string

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.