简体   繁体   English

将 MEF 与 asp.net web 表单应用程序一起使用 (SharePoint)

[英]Using MEF with asp.net web form application (SharePoint)

I'm trying to use MEF in my ASP.NET web form (SharePoint) application to load some controls from a directory at runtime.我试图在我的 ASP.NET web 表单(SharePoint)应用程序中使用 MEF 在运行时从目录加载一些控件。 I'm not getting any error but the controls are not getting loaded.我没有收到任何错误,但没有加载控件。

Here is my code -这是我的代码 -

aspx.cs aspx.cs

    public partial class SampleMEF : System.Web.UI.Page
    {
        [ImportMany(typeof(IControlLoader))]
        IEnumerable<Lazy<IControlLoader, IControlLoaderMetaData>> controls;
        private CompositionContainer _partsContainer;
        private AggregateCatalog _catalog;
        private string _partsPath;

        /// <summary>
        /// default constructor
        /// </summary>
        public SampleMEF()
        {
        }

        /// <summary>
        /// Initialize the page
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            _partsPath = SPUtility.GetGenericSetupPath(@"TEMPLATE\LAYOUTS\MEFProtoType\Parts");
            _catalog = new AggregateCatalog();
            DirectoryCatalog c = new DirectoryCatalog(_partsPath, "*.dll");
            _partsContainer = new CompositionContainer(c);
            _partsContainer.ComposeParts(this);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            foreach(Lazy<IControlLoader, IControlLoaderMetaData> i in controls)
            {
                SPPControl ctrl = i.Value.LoadControl();
                lbxControls.Items.Add(new ListItem(ctrl.Name, ctrl.ControlID.ToString()));
            }
        }
    }

Contracts合同

    /// <summary>
    /// Contract for Imports and Exports
    /// </summary>
    public interface IControlLoader
    {
        SPPControl LoadControl();
    }

    /// <summary>
    /// Exports metadata
    /// </summary>
    public interface IControlLoaderMetaData
    {
        string ControlID { get; }
    }

Sample Export样品导出

    [Export(typeof(IControlLoader))]
    [ExportMetadata("ControlID", "7a6c6288-ab52-4010-8c56-79959843ec7c")]
    public class ctrlAccordion : IControlLoader
    {
        #region IControlLoader Members

        public SPPControl LoadControl()
        {
            SPPControl ctrl = new SPPControl("Accordion", new Guid("7a6c6288-ab52-4010-8c56-79959843ec7c"), 5);
            return ctrl;
        }

        #endregion
    }

I'm able to see the DLLs copied in the parts directory.我可以看到在部件目录中复制的 DLL。 But I'm not able to load the parts.但我无法加载零件。 The import is not filled and it is empty.导入未填充,为空。

I'm using.Net Framework 3.5 and downloaded the MEF dll from MEF Codeplex and signed the assembly myself.我正在使用.Net Framework 3.5 并从 MEF Codeplex 下载了 MEF dll 并自己签署了程序集。

Any ideas?有任何想法吗?

Okay, I eventually found the issue.好的,我终于找到了问题所在。 I'm able to load the part dlls only if I sign them and add the dlls to global assembly.只有当我签署它们并将 dll 添加到全局程序集中时,我才能加载部件 dll。 But I don't understand what DirectoryCatalog does loading the folder.但我不明白DirectoryCatalog加载文件夹的内容。

Bottom line: If the dlls are just in the parts folder and not in the GAC they are not getting loaded.底线:如果 dll 只是在部件文件夹中而不是在 GAC 中,它们就不会被加载。

There can be problems when trying to load assemblies from arbitrary locations.尝试从任意位置加载程序集时可能会出现问题。 The "Assembly Load Issues" section of this blog post has a bit of information on this and links to more information. 这篇博文的“装配负载问题”部分提供了一些关于此的信息以及指向更多信息的链接。

You can also inspect the catalog in the debugger to see if it loaded any parts at all.您还可以在调试器中检查目录以查看它是否加载了任何部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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