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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:22:55+00:00 2026-05-25T21:22:55+00:00

In a WCF service project, I have created a simple wrapper for MEF CompositionContainer

  • 0

In a WCF service project, I have created a simple wrapper for MEF CompositionContainer to simplify its instantiation :

internal class CompositionProxy
{
    private static Lazy<CompositionContainer> m_lazyCC;
    static CompositionProxy()
    {
        m_lazyCC = new Lazy<CompositionContainer>(() =>
            {
                var batch = new CompositionBatch();

                var dc1 = new DirectoryCatalog(
                    HttpContext.Current.Server.MapPath("~/bin")
                    );

                return new CompositionContainer(dc1);
            }
        );
    }
    public static CompositionContainer DefaultContainer
    {
        get
        {
            return m_lazyCC.Value;
        }
    }
}

The idea is to have one CompositionContainer for the application lifetime, which search for export in the bin directory.

Then, I set up some webservices, that requires to have on imported property :

All of them are built like this :

public class MyService: IMyService
{
    public MyService()
    {
        CompositionProxy.DefaultContainer.SatisfyImportsOnce(this);
    }
    [Import]
    private IContext Context { get; set; }

    public void DoTheJob()
    {
        // Logic goes here
    }
}

Elsewhere, I have one class that match this export :

[Export(typeof(IContext))]
public class MyContext 
{
    public MyContext(){
        Log("MyContext created");
    }
}

In the constructor, I ask the composition container to populate the IContext Context property.

This seems to work, in my service, I can see the Context property is correctly populated.

However, I’m experiencing memory leaks, and my tracing show me the MyContext class is instantiated only once.

Can you clarify if I’m misusing the composition framework ?

  1. I supposed it’s a good idea to have one composition container for the application lifetime, was I wrong ?
  2. the multiple calls to SatisfyImportsOnce seems to populate the target with the same unique instance. Is it true ? If true, how can I simply change my code to have a new instance each time the method is called ?
  3. Any suggestion to improve my code ?
  • 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-25T21:22:56+00:00Added an answer on May 25, 2026 at 9:22 pm

    I supposed it’s a good idea to have one composition container for the application lifetime

    Yes, you are supposed to create one container for the application lifetime.

    the multiple calls to SatisfyImportsOnce seems to populate the target with the same unique instance. Is it true ? If true, how can I simply change my code to have a new instance each time the method is called ?

    You need [Import(RequiredCreationPolicy=CreationPolicy.NonShared)].

    Any suggestion to improve my code ?

    If possible, do not expose the container as a global and litter your code with calls to it. That’s the Service Locator pattern, which has some disadvantages when compared to Dependency Injection. Instead of your service trying to compose itself, just declare what it needs:

    [Export(typeof(IMyService))]
    public class MyService: IMyService
    {
        private readonly IContext context;
    
        public MyService(
            [Import(typeof(IContext),      
                RequiredCreationPolicy=CreationPolicy.NonShared)]
            IContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context");
            this.context = context;
        }
    
        public void DoTheJob()
        {
            // Logic goes here
        }
    }
    

    In a WCF service, I think you should only need to call the container in your ServiceHostFactory implementation. I’m not really familiar with WCF though.

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

Sidebar

Related Questions

I created a very simple WCF class library and added this project to the
I have created WCF service in project. Now using Jquery I am successfully able
I have created a WCF REST style service in my project. In development I
This is real simple. I have a wcf service (nothing fancy, just New Project->
I have created a WCF service in my project and I have some classes
I have created a new WCF service library type project. I have created a
In my project I have a Windows Service and a WCF Service doing some
I am new to WCF and created a simple REST service to accept an
I have created a RIA services class library project and things are not going
I have a WCF Web Service which is referenced from a class library. After

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.