簡體   English   中英

將 MEF 與 asp.net web 表單應用程序一起使用 (SharePoint)

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

我試圖在我的 ASP.NET web 表單(SharePoint)應用程序中使用 MEF 在運行時從目錄加載一些控件。 我沒有收到任何錯誤,但沒有加載控件。

這是我的代碼 -

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()));
            }
        }
    }

合同

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

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

樣品導出

    [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
    }

我可以看到在部件目錄中復制的 DLL。 但我無法加載零件。 導入未填充,為空。

我正在使用.Net Framework 3.5 並從 MEF Codeplex 下載了 MEF dll 並自己簽署了程序集。

有任何想法嗎?

好的,我終於找到了問題所在。 只有當我簽署它們並將 dll 添加到全局程序集中時,我才能加載部件 dll。 但我不明白DirectoryCatalog加載文件夾的內容。

底線:如果 dll 只是在部件文件夾中而不是在 GAC 中,它們就不會被加載。

嘗試從任意位置加載程序集時可能會出現問題。 這篇博文的“裝配負載問題”部分提供了一些關於此的信息以及指向更多信息的鏈接。

您還可以在調試器中檢查目錄以查看它是否加載了任何部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM