OSGI:OSGI集成ASP.NET MVC4.0
OSGI能动态的加载、启动和停止Bundle,之前我实现了和Ioc的集成以动态的注册和取消注册Bundle中公开的服务。今天简单的实现了和MVC的集成以动态的管理Controller。
ASP.NET MVC默认只识别BIN目录下的程序集,当然你可以修改一些配置让他支持其它目录,我采用的策略时重写DefaultControllerFactory+OSGI插件,插件动态管理ControllerType的注册和取消注册,ControllerFactory根据注册的信息获取ControllerType。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 using Happy.OSGI; 8 9 namespace Happy.Web.Mvc 10 { 11 public sealed class MvcBundleContainerPlug : IBundleContainerPlug 12 { 13 void IBundleContainerPlug.Start(BundleContext context) 14 { 15 MvcBundleContainerExtensions 16 .Current 17 .OSGIControllerTypeCache 18 .RegistAssembly(context.Bundle.Assembly); 19 } 20 21 void IBundleContainerPlug.Stop(BundleContext context) 22 { 23 MvcBundleContainerExtensions 24 .Current 25 .OSGIControllerTypeCache 26 .UnRegistAssembly(context.Bundle.Assembly); 27 } 28 } 29 }代码示例(代码下载)
关键配置代码
1 namespace Happy.OSGI.Demo.WebHost 2 { 3 // Note: For instructions on enabling IIS6 or IIS7 classic mode, 4 // visit http://go.microsoft.com/?LinkId=9394801 5 public class MvcApplication : System.Web.HttpApplication 6 { 7 protected void Application_Start() 8 { 9 this.Initialize(); 10 11 AreaRegistration.RegisterAllAreas(); 12 13 WebApiConfig.Register(GlobalConfiguration.Configuration); 14 FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 15 RouteConfig.RegisterRoutes(RouteTable.Routes); 16 } 17 18 private void Initialize() 19 { 20 BundleContainer 21 .Current 22 .UseDirectoryAssemblyLoader() 23 .UseDirectoryAssemblyLoader(@"E:\开发\Happy.OSGI.Demo\Happy.OSGI.Demo.Host\bin\Debug\Bundles") 24 .IntegrationWithMvc() 25 .UseUnity() 26 .RegistCommandHandlerByConvention() 27 .Start(); 28 29 DependencyResolver.SetResolver(new ServiceLocationDependencyResolver(ServiceLocator.Current)); 30 } 31 } 32 }项目结构及依赖关系
起始运行效果(状态与执行结果)
停止B后的效果(状态与执行结果)
分类: OSGI