MEF,延迟加载高级元数据和序列化异常

本文关键字:序列化 异常 元数据 高级 延迟加载 MEF | 更新日期: 2023-09-27 18:16:22

我正在开发一个接受插件的应用程序,我决定使用MEF。一切都很好,直到我尝试使用AppDomain和ShadowCopy。现在,当试图从MEF容器中检索插件时,我面临着元数据接口上的序列化异常。

下面是我的代码的几个组成部分:

容器:

public class PluginManagerExtended : MarshalByRefObject
    {
        private static readonly string PluginPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "Plugins");
        private CompositionContainer container;
        private DirectoryCatalog directoryCatalog;            
        [ImportMany(AllowRecomposition = true)]
        public IEnumerable<Lazy<IXrmToolBoxPlugin, IPluginMetadata>> Plugins { get; set; }
        public void Initialize()
        {
            try
            {
                var regBuilder = new RegistrationBuilder();
                regBuilder.ForTypesDerivedFrom<Lazy<IXrmToolBoxPlugin, IPluginMetadata>>().Export<Lazy<IXrmToolBoxPlugin, IPluginMetadata>>();
                var catalog = new AggregateCatalog();
                catalog.Catalogs.Add(new AssemblyCatalog(typeof(PluginManagerExtended).Assembly, regBuilder));
                directoryCatalog = new DirectoryCatalog(PluginPath, regBuilder);
                catalog.Catalogs.Add(directoryCatalog);
                container = new CompositionContainer(catalog);
                container.ComposeParts(this);
            }
            catch (ReflectionTypeLoadException ex)
            {
                if (ex.LoaderExceptions.Length == 1)
                {
                    throw ex.LoaderExceptions[0];
                }
                var sb = new StringBuilder();
                var i = 1;
                sb.AppendLine("Multiple Exception Occured Attempting to Intialize the Plugin Manager");
                foreach (var exception in ex.LoaderExceptions)
                {
                    sb.AppendLine("Exception " + i++);
                    sb.AppendLine(exception.ToString());
                    sb.AppendLine();
                    sb.AppendLine();
                }
                throw new ReflectionTypeLoadException(ex.Types, ex.LoaderExceptions, sb.ToString());
            }
        }
        public void Recompose()
        {
            directoryCatalog.Refresh();
            container.ComposeParts(directoryCatalog.Parts)
        }
        internal void DoSomething()
        {
            Plugins.ToList().ForEach(p => MessageBox.Show(p.Metadata.Name));
        }
}

元数据接口

public interface IPluginMetadata
{
    string BackgroundColor { get; }
    string BigImageBase64 { get; }
    string Description { get; }
    string Name { get; }
    string PrimaryFontColor { get; }
    string SecondaryFontColor { get; }
    string SmallImageBase64 { get; }
}

我正在使用的插件:

[Export(typeof(IXrmToolBoxPlugin)),
ExportMetadata("Name", "A Sample Tool 2"),
ExportMetadata("Description", "This is a tool to learn XrmToolBox developement")
ExportMetadata("SmallImageBase64", null),
ExportMetadata("BigImageBase64", null),
ExportMetadata("BackgroundColor", "Lavender"),
ExportMetadata("PrimaryFontColor", "#000000"),
ExportMetadata("SecondaryFontColor", "DarkGray")]
public class Plugin : PluginBase
{
    /// <summary>
    /// This method return the actual usercontrol that will
    /// be used in XrmToolBox
    /// </summary>
    /// <returns>User control to display</returns>
    public override IXrmToolBoxPluginControl GetControl()
    {
        return new SampleTool2();
    }
}

以及同时使用两个元素的代码:

var cachePath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
    "ShadowCopyCache");
var pluginsPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
    "Plugins");
if (!Directory.Exists(cachePath))
{
    Directory.CreateDirectory(cachePath);
}
if (!Directory.Exists(pluginsPath))
{
    Directory.CreateDirectory(pluginsPath);
}
var setup = new AppDomainSetup
{
    CachePath = cachePath,
    ShadowCopyFiles = "true",
    ShadowCopyDirectories = pluginsPath
};
domain = AppDomain.CreateDomain("XrmToolBox_AppDomain", AppDomain.CurrentDomain.Evidence, setup);
pManager =
    (PluginManagerExtended)
        domain.CreateInstanceAndUnwrap(typeof(PluginManagerExtended).Assembly.FullName,
            typeof(PluginManagerExtended).FullName);
pManager.Initialize();
// This code works well as the Metadata are accessed in the container
pManager.DoSomething();
// This code fails
string plugins = String.Join(",", pManager.Plugins.Select(p => p.Metadata.Name));

我得到的异常如下:

System.Runtime.Serialization。SerializationException:类型"proxy_xrmtoolbox.extensibility.interfaces.ipluginmetadata_bafb7089 c69c - 4 f81 - 92 - f8 - a88eda6d70eb"MetadataViewProxies_760ed609-1713-4fe9-959c-7bfa78012252版本=0.0.0.0,文化=中性,PublicKeyToken=null'未被标记是可序列化的。

XrmToolBox.Extensibility.Interfaces。IPluginMetadata是上面的高级元数据接口。

我不知道"MetadataViewProxies_760ed609-1713-4fe9-959c-7bfa78012252, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"是什么

我能做什么来修复这个错误?

谢谢

MEF,延迟加载高级元数据和序列化异常

要访问另一个应用域中的对象,它们必须是可序列化的。虽然您似乎没有在上面的代码中使用IPluginMetadata,但我认为它是用于强类型自定义元数据属性的。