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后的效果(狀態與執行結果)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM