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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:19:10+00:00 2026-06-08T00:19:10+00:00

I have a base controller class that exposes some logging functionality to it’s child

  • 0

I have a base controller class that exposes some logging functionality to it’s child classes. This logging dependency is constructor injected. To provide some simplified code this is how it all looks like:

public abstract class LogControllerBase : Controller
{
    private readonly ILogger logger = null;

    protected LogControllerBase(ILogger logger)
    {
        this.logger = logger;
    }
}

My child controllers also have their own dependencies hence they look something like this:

public class SomeController : LogControllerBase
{
    private readonly IService service = null;

    [ImportingConstructor]
    public SomeController(ILogger logger, IService service)
        : base(logger)
    {
        this.service = service;
    }
}

I’m using these constructors to make it easy to do dependency injection in my unit tests but in production all composition is (should be) done by MEF. I’m using my custom controller factory class that uses MEF to instantiate controllers.

To sum it up:

  1. I have a base abstract controller class that has its own dependencies
  2. I have descendant controllers that use importing to get their parameters injected by MEF (that is their dependencies as well as base class’ ones)
  3. Unit tests don’t use MEF so mocks are injected to constructor parameters

The problem

This all holds water to me, but MEF thinks differently. When I compile and run this code I get this exception:

GetExportedValue cannot be called before prerequisite import ‘SomeController..ctor (Parameter=”logger”, ContractName=”ILogger”)’ has been set.

All interface types that are used as constructor parameters have attribute InheritedExport set on them and also have concrete implementations, so it should work as expected.

Working alternative which isn’t the same

When I try an alternative approach by doing imports directly on those private fields everything seems to work just fine.

public abstract class LogControllerBase : ControllerBase
{
    [Import]
    private ILogger logger = null;

    protected LogControllerBase() { }
}

public class SomeController : LogControllerBase
{
    [Import]
    private IService service = null;

    public SomeController() { }
}

So this works but it’s not the same… I could add constructors here for dependency injection, but then I’d have two sets of constructors and when doing unit tests one may use the parameterless constructor which would of course be wrong because no dependencies would get set. Not real ones nor mocks.

Question

How can I convince MEF to create my controllers by injecting concrete implementations in constructor by setting dependency injection constructor as ImportingConstructor?

  • 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-06-08T00:19:12+00:00Added an answer on June 8, 2026 at 12:19 am

    This might be related to threading, as MEF containers are not constructed as thread-safe by default. In your constructor for your container, try enabling thread-safety:

    var container = new CompositionContainer(catalog, true);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a library base class ( Controller ) and three sub-classes that inherit
I have a base controller which is defined as follows, but the constructor that
I have a base controller class that attempts to look at Request.ContentType to see
I have a base controller class: And all my other controller inherits this BaseClass
I have made a base controller class that overrides OnActionExecuting as follows: public class
I have a project with a base view controller class, that is inherited by
I have a base controller abstract class, which my other controllers inherit. This class
I have a base class looking like this: public class BaseController : Controller {
I have a class that looks something like this: class User < ActiveRecord:Base has_many
I have base class BaseClass and derived classes DerivedA , DerivedB , and DerivedC

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.