首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

.NET 多模組架構:OSGi 原型核心機制 - 隔離容器、激活器、服務註冊與相依解析

     編輯:關於.NET

前言

本文深入剖析 OSGi 原型的核心運行機制:AppDomain 隔離容器、BundleActivator 生命週期、服務註冊發現、相依注入解析,以及 XML 設定驅動的模組載入流程。

核心架構圖

OSGi 原型核心架構

關鍵特性

  • 完全隔離:Runtime、Module1-3 各自獨立 AppDomain
  • 動態卸載:AppDomain 卸載實現 DLL 熱更新、版本切換
  • 介面導向:PublicInterfaces 組件分離,僅共用契約
  • 目前限制:僅支援單機模組載入,UI 模組化與分散式待擴展

模組專案結構

模組專案結構

  • OrderModule:業務邏輯與模組定義
  • OrderModule.PublicInterfaces:介面、事件、DTO 定義 (獨立部署)
  • LoggingModule、OrderPDFProcessor:其他功能模組

模組定義檔:Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<Module>
    <Name>Order Module</Name>
    <Version>1</Version>
    <Assembly>OrderModule</Assembly>
    <Activator>OrderModule.Activator</Activator>
    <RequiredService>LoggingModule.PublicInterfaces.ILog</RequiredService>
    <ProvidedService>OrderModule.PublicInterfaces.IOrderProcessor</ProvidedService>
</Module>

Activator 實作


public class Activator : OSGIFramework.BundleActivator
{
    public override void Startup(BundleContext bc)
    {
        Console.WriteLine("OrderModule Startup");
        BundleContext.Current.RegisterProvidedService();
    }

    public override void Stop(BundleContext bc)
    {
        Console.WriteLine("OrderModule Stop");
        BundleContext.Current.UnRegisterProvidedService();
    }
}

服務實作


public class OrderProcessorImpl : ServiceProvider, IOrderProcessor
{
    public Guid PlaceOrder(OrderDto order)
    {
        BundleContext ctx = BundleContext.Current;
        ILog log = ctx.GetProvidedService(); // 相依解析
        log.Log("log something...");
        Console.WriteLine("PlaceOrder body");
        return Guid.NewGuid();
    }
}

主程式設定:Manifests.xml (單機多模組)

<?xml version="1.0" encoding="utf-8"?>
<Manifests>
    <Manifest>D:\Projects\OSGIDemo\LoggingModule\bin\Debug\Manifest.xml</Manifest>
    <Manifest>D:\Projects\OSGIDemo\OrderModule\bin\Debug\Manifest.xml</Manifest>
    <Manifest>D:\Projects\OSGIDemo\OrderPDFProcessor\bin\Debug\Manifest.xml</Manifest>
</Manifests>

主程式啟動碼


static void Main(string[] args)
{
    BundleRuntime runtime = new BundleRuntime();
    runtime.Start();
    
    BundleContext ctx = BundleContext.Current;
    IOrderProcessor processor = (IOrderProcessor)ctx.GetProvidedService(typeof(IOrderProcessor).FullName);
    
    OrderDto order = new OrderDto();
    Guid id = processor.PlaceOrder(order);
    Console.WriteLine("id=" + id.ToString());
    
    Console.ReadKey();
    runtime.Stop();
    runtime.Dispose();
    runtime = null;
}

核心類別職責

類別職責
BundleRuntime解析 Manifests.xml、建立 AppDomain、載入組件、實例化 Activator、啟動/停止協調
BundleContext服務註冊表、相依解析、當前模組上下文、跨 AppDomain 代理管理
BundleActivator模組生命週期進入點 (Startup/Stop)
ServiceProvider跨 AppDomain 封送基類 (繼承 MarshalByRefObject)

設計亮點

  1. 設定驅動:純 XML 宣告模組組合,無硬編碼相依
  2. 介面隔離:PublicInterfaces 獨立組件,降低耦合
  3. 透明代理:GetProvidedService 內部處理跨 AppDomain 封送
  4. 生命週期管理:Startup/Stop 配對確保資源正確釋放

查看本欄目

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved