asp.net-mvc – 我们如何使用ASP.NET MVC 4和MEF 2支持模块化和
我们正在尝试将MEF 2与ASP.NET MVC 4一起使用来支持可扩展的应用程序.这个问题实际上有两个部分(希望没关系,所以这就是神):
>我们如何使用Microsoft.Composition和MVC容器代码(MEF/MVC demo source)将Ninject替换为ICoreService,ICoreRepository,IUnitOfWork和IDbContext的DI? 编辑1 编辑2 CoreDbContext.cs(Data.csproj) [Export(typeof(IDbContext))] [Shared(Boundaries.HttpRequest)] public class CoreDbContext : IDbContext { ... } CoreRepository.cs(Data.csproj) [Export(typeof(IUnitOfWork))] [Export(typeof(ICoreRepository))] public class CoreRepository : ICoreRepository,IUnitOfWork { [ImportingConstructor] public CoreRepository(IInsightDbContext context) { _context = context; } ... } CoreService.cs(Services.csproj) [Export(typeof(ICoreService))] public class CoreService : ICoreService { [ImportingConstructor] public CoreService(ICoreRepository repository,IUnitOfWork unitOfWork) { _repository = repository; _unitOfWork = unitOfWork; } ... } UserController.cs(Web.csproj) public class UsersController : Controller { [ImportingConstructor] public UsersController(ICoreService service) { _service = service; } ... } Global.asax.cs(Web.csproj) public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { CompositionProvider.AddAssemblies( typeof(ICoreRepository).Assembly,typeof(ICoreService).Assembly,); // EDIT 2 -- // updated code to answer my 2nd question based on Nick Blumhardt's answer foreach (var file in System.IO.Directory.GetFiles(Server.MapPath("Plugins"),"*.dll")) { try { var name = System.Reflection.AssemblyName.GetAssemblyName(file); var assembly = System.Reflection.Assembly.Load(name); CompositionProvider.AddAssembly(assembly); } catch { // You'll need to craft exception handling to // your specific scenario. } } } } 解决方法
如果我理解正确,你正在寻找将从目录加载所有程序集并将它们加载到容器中的代码;这是执行此操作的框架:
var config = new ContainerConfiguration(); foreach (var file in Directory.GetFiles(@".Plugins","*.dll")) { try { var name = AssemblyName.GetAssemblyName(file); var assembly = Assembly.Load(name); config.WithAssembly(assembly); } catch { // You'll need to craft exception handling to // your specific scenario. } } var container = config.CreateContainer(); // ... Hammett讨论了这个场景,并在F#中显示了一个更完整的版本:http://hammett.castleproject.org/index.php/2011/12/a-decent-directorycatalog-implementation/ 注意,在应用程序启动后,这不会检测添加到目录的程序集 – Microsoft.Composition不适合这种用途,因此如果插件集更改,最好的办法是使用目录监视器检测到并提示用户重新启动应用程序. HTH! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 需要用户登录的UnitTest ASP.NET应用程序
- vbscript – 经典ASP中的三重DES解密?
- asp.net – 访问母版页中的祖父母内容占位符
- .net – 将GUID转换为整数和后退
- 在ASP.NET MVC2创建方法中使用FormCollection的正确方法?
- asp.net-mvc – 来自父Razor布局的@Functions继承
- asp.net-mvc-3 – 使用Quartz.Net的MVC 3
- iis – 在Asp.net核心应用程序中与SignalR 2.2.1协商404
- asp.net-mvc – ASP.NET MVC中的动态(运行时生成)窗体
- asp.net – 在MVC视图上没有客户端密码匹配
- asp.net-ajax – 找不到带ID的UpdatePanel
- ASP.NET标识如何为自定义ApplicationUser添加必需
- asp.net – 如何在swagger中为.Net Core Web API
- 如何在我的ASP.NET应用程序中创建“通用错误”页
- ASP.NET框架中的异步页面 – 其他线程在哪里,如何
- asp.net-mvc – 如何在ASP.Net MVC中对自定义Act
- 如何在ASP.NET中使用多个授权方案发布相应的承载
- asp.net GridView吐出无效的HTML
- asp.net-mvc – 为什么Microsoft堆栈说成本高昂?
- 如果我的Asp.Net会话有IsNewSession == true,那我