Table of Contents

VSMEF005 Multiple importing constructors

A class that exports itself or has exported members can only have one constructor marked with [ImportingConstructor]. MEF requires exactly one importing constructor per type to know how to instantiate the class.

The following class definition would produce a diagnostic from this rule:

[Export]
class Foo
{
    [ImportingConstructor]
    public Foo()
    {
    }

    [ImportingConstructor]
    public Foo(string parameter)
    {
    }
}

This class has two constructors both marked with [ImportingConstructor], which creates ambiguity for MEF about which constructor to use for instantiation.

Fixing the diagnostic

Remove the [ImportingConstructor] attribute from all but one constructor. The remaining importing constructor will be used by MEF to create instances of the class.

[Export]
class Foo
{
    public Foo()
    {
    }

    [ImportingConstructor]
    public Foo(string parameter)
    {
    }
}

In this corrected version, only one constructor is marked as the importing constructor, and MEF will use it to instantiate the class, providing the required string parameter through dependency injection.

Alternative solutions

If you need multiple constructors for different scenarios:

Option 1: Use only one importing constructor

Keep only one constructor marked with [ImportingConstructor] and use that for MEF instantiation:

[Export]
class Foo
{
    // Regular constructor for manual instantiation
    public Foo()
    {
    }

    // MEF importing constructor
    [ImportingConstructor]
    public Foo(IService service)
    {
        // Initialize with injected dependencies
    }
}

Option 2: Use factory pattern

If you need complex instantiation logic, consider using a factory pattern:

[Export]
class FooFactory
{
    [ImportingConstructor]
    public FooFactory(IService service)
    {
        this.service = service;
    }

    public Foo CreateFoo() => new Foo();
    public Foo CreateFoo(string parameter) => new Foo(parameter, this.service);
}