.net Mef with Enterprise Library 5.0

半城伤御伤魂 提交于 2020-01-04 15:58:18

问题


I am wanting to use mef with the Logging and Exception Blocks of Enterprise Library 5.0.

What I am doing is I have a plugin defined that MEF is looking for in a directory. It then imports it for me. However I am using logging and exception handling in the plugin also and I would like to be able to insert the instance of the LogWriter and ExceptionWriter via mef. The example shows you how to do it with unity which won't work since I am using mef to get the plugin and instantiate it.

thanks


回答1:


You can export LogWriter with a class like this:

public class LogWriterExporter
{

    [Export(typeof(LogWriter))]
    public LogWriter LogWriter
    {
        get
        {
            return new LogWriter(...);
        }
    }
}

Note that MEF will typically call this property getter only once, except if an importer demands CreationPolicy.NonShared. If you want to enforce singleton behavior, then you can explicitly add a [PartCreationPolicy(CreationPolicy.Shared)] attribute to the class.



来源:https://stackoverflow.com/questions/5637678/net-mef-with-enterprise-library-5-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!