OSGI:OSGI集成ASP.NET MVC4.0

OSGi与ASP.NET MVC集成动态管理Controller
本文介绍如何将OSGi与ASP.NET MVC结合,实现动态注册和取消注册Controller,通过重写DefaultControllerFactory并使用OSGI插件来管理ControllerType的注册与取消注册。

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后的效果(状态与执行结果)

 


<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值