简体   繁体   中英

How to fulfill System.Composition MEF ImportingConstructor with an instance

I'm using MEF (System.Composition from nuget) + Common.Logging and have classes that call ILog, eg,

[Export(typeof(ITranslator))]
public class ATranslator : BaseTranslator
{
    [ImportingConstructor]
    public ATranslator(ILog log)
    {
...
    }

The logger instance is available from Common.Logging via log = LogManager.GetLogger<ITranslator>(); but how do I plugin this into my composition container?

        var container = new ContainerConfiguration()
            .WithAssembly(typeof(ITranslator).Assembly)
            .CreateContainer();            
        container.SatisfyImports(this);

so that I can

    [ImportMany]
    private IEnumerable<ITranslator> Translators { get; set; }

without

System.Composition.Hosting.CompositionFailedException : No export was found for the contract 'ILog' -> required by import 'log' of part 'ATranslator' -> required by import 'item' of part 'ITranslator[]' -> required by initial request for contract 'IEnumerable { IsImportMany = True }'

Maybe you can try to do the export to ILog as a property in other class?

public class LogExporter
{
    [Export(typeof(ILog))]
    public ILog Log
    {
        return LogManager.GetLogger<ITranslator>();
    }
}

Alternatively you can try to do it using ComposeExportedValue Method with code instead of attributes:

container.ComposeExportedValue<ILog>(LogManager.GetLogger<ITranslator>());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM