Asp.net mvc 在MVC中从DLL导入方法

Asp.net mvc 在MVC中从DLL导入方法,asp.net-mvc,mef,Asp.net Mvc,Mef,我想从MVC中的.dll文件导入方法,并在控制器的操作中运行它们。可以使用MEF吗?是的,我该如何进行 我终于让它工作了。写下这个答案,以防有人在这里被击中 接口DLL namespace MefContracts { public interface IPlugin { String Work(String input); } } 包含所需方法的插件 namespace Plugin { [Export(typeof(MefContracts

我想从MVC中的.dll文件导入方法,并在控制器的操作中运行它们。可以使用MEF吗?是的,我该如何进行

我终于让它工作了。写下这个答案,以防有人在这里被击中

接口DLL

namespace MefContracts
{
    public interface IPlugin
    {
        String Work(String input);
    }
}
包含所需方法的插件

namespace Plugin
{

    [Export(typeof(MefContracts.IPlugin))]
    public class Mytest:MefContracts.IPlugin
    {
        public String Work(String input)
        {
            return "Plugin Called from dll with (Input: " + input + ")";
        }
    }

}
Program.cs

(将其包括在主MVC项目中)。此类包含链接所有导入和导出的函数

namespace MyTest
{
    public class Program
    {
        private CompositionContainer _container;

        [Import(typeof(MefContracts.IPlugin))]
        public MefContracts.IPlugin plugin;

        public Program()
        {
            var catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new DirectoryCatalog(@"D:\Temp"));


            _container = new CompositionContainer(catalog);


            try
            {
                this._container.ComposeParts(this);
            }
            catch (CompositionException compositionException)
            {
                Console.WriteLine(compositionException.ToString());
            }
        }
    }
}
最后从控制器调用此方法

public class HomeController : Controller
    {
        Program p = new Program();

        public ActionResult Index()
        {
            ViewBag.Message = p.plugin.Work("test input");
            return View();
        }
    }

.NET还是.NET内核?它基于.NET