OSGI:OSGI集成ASP.NET MVC4.0
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
作者: Leo_wl
出处: http://www.cnblogs.com/Leo_wl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息
查看更多关于OSGI:OSGI集成ASP.NET MVC4.0的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did46399
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
作者: Leo_wl
出处: http://www.cnblogs.com/Leo_wl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息查看更多关于OSGI:OSGI集成ASP.NET MVC4.0的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did46399