OSGI:C#如何實現簡單的OSGI


 我對OSGI的認識

  • 面向接口編程在組件(DLL)級別的體現。
  • 插件機制的一種擴展。
  • 動態管理組件(DLL)的生命周期(加載、啟動、停止、卸載)。
  • 回調和插件的支持,在Bundle的生命周期中,允許通過插件和回調進行攔截。
  • Ioc容器的集成(非必須),如:Bundle啟動時自動將服務注冊到Ioc中,停止時自動取消服務注冊。
  • Ioc管理服務的生命周期,OSGI管理組件的生命周期。

代碼示例(下載地址

項目結構及他們的依賴關系

Program.cs 中的代碼

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 using Microsoft.Practices.ServiceLocation;
 8 
 9 using Happy.OSGI;
10 using Happy.Commands;
11 using Happy.Ioc.Unity;
12 using Happy.OSGI.Demo.Commands;
13 
14 namespace Happy.OSGI.Demo.Host
15 {
16     public static class Program
17     {
18         public static void Main()
19         {
20             Initialize();
21 
22             var commandBus = ServiceLocator.Current.GetInstance<CommandBus>();
23             var command = new TestCommand { Message = "測試消息" };
24 
25             Console.WriteLine("默認所有的Bundle都是啟動的,執行情況如下:");
26             commandBus.Send(command);
27 
28             Console.WriteLine();
29 
30             Console.WriteLine("停止一個Bundle后,執行情況如下:");
31             BundleContainer.Current.Bundles.First().Stop();
32             commandBus.Send(command);
33 
34             Console.WriteLine();
35 
36             Console.WriteLine("重新啟動Bundle后,執行情況如下:");
37             BundleContainer.Current.Bundles.First().Start();
38             commandBus.Send(command);
39         }
40 
41         private static void Initialize()
42         {
43             BundleContainer
44                 .Current
45                 .UseDirectoryAssemblyLoader()
46                 .UseUnity()
47                 .RegistCommandHandlerByConvention()
48                 .Start();
49         }
50     }
51 }

執行結果


免責聲明!

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



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