• 多模块后带来的问题解决方法


    目前只做了基础的功能,比如:

    1. 各个模块单独的AppDomain容器
    2. Activator激活
    3. 导出的服务检查

    不过,虽说这样,但目前的这个版本已经能实现模块分离、互相依赖调用等功能了,对模块划分已经有很好的作用了。

    先来说下基本结构:

     

    目前这个框架对UI的模块化尚不支持,只支持单机下的模块加载(以后会改进)。

    特点:

    • Runtime、Module1、Module2、Module3都是在各自的AppDomain下运行的,不会互相干扰
    • 由于采用了隔离AppDomain的方式加载Module,所以能实现轻松卸载AppDomain、某dll、dll版本切换之类的任务,对后续扩展提供了方便

    来看看模块的编写

     

     OrderModule库是主要的业务逻辑以及模块定义的地方

    OrderModule.PublicInterfaces是公用定义,比如接口、事件、dto等,考虑到方便分发,因此独立了出来(如果不介意这点,也可以合并成一个库)

    模块定义文件:

    <?xml version="1.0" encoding="utf-8" ?>
    <Module>
      <Name>Order Module</Name>
      <Version>1</Version>
      <Assembly>OrderModule</Assembly>
      <Activator>OrderModule.Activator</Activator>  //这个是OSGI调用的第一个classType, 会执行里面的2个方法:Start和Stop
      <RequiredService>LoggingModule.PublicInterfaces.ILog</RequiredService>//此模块需要依赖哪些其他Service,可以有多个
      <ProvidedService>OrderModule.PublicInterfaces.IOrderProcessor</ProvidedService>//此模块会提供的服务,可以有多个
    </Module>

     Activator:

    class Activator:OSGIFramework.BundleActivator
        {
            public override void Startup(BundleContext bc)
            {
                Console.WriteLine("OrderModule Startup");
    
                BundleContext.Current.RegisterProvidedService<IOrderProcessor, OrderProcessorImpl>();
    //注册服务 }
    public override void Stop(BundleContext bc) { Console.WriteLine("OrderModule Stop"); BundleContext.Current.UnRegisterProvidedService<IOrderProcessor, OrderProcessorImpl>();
    //卸载服务 } }

     服务的实现:

    class OrderProcessorImpl : ServiceProvider, IOrderProcessor //ServiceProvider是基类,由于需要在AppDomain之间穿梭,因此必须继承这个
        {
            public Guid PlaceOrder(OrderModule.PublicInterfaces.Dtos.OrderDto order)
            {
                BundleContext ctx=BundleContext.Current;  //获得上下文
    
                ILog log = ctx.GetProvidedService<ILog>(); //获得一个服务
                log.Log("log something...");
    
                Console.WriteLine("PlaceOrder body");
    
                return Guid.NewGuid();
            }
        }

    模块化,还要考虑散落在不同目录的dll文件和xml定义文件。在Console程序中,也可以通过xml定义格式来做:

    <?xml version="1.0" encoding="utf-8" ?>
    <Manifests>
      <Manifest>D:documentsvisual studio 2010ProjectsOSGIDemoLoggingModuleinDebugManifest.xml</Manifest>
      <Manifest>D:documentsvisual studio 2010ProjectsOSGIDemoOrderModuleinDebugManifest.xml</Manifest>
      <Manifest>D:documentsvisual studio 2010ProjectsOSGIDemoOrderPDFProcessorinDebugManifest.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;
            }

    就可以运行了,效果图:

    当然,由于是AppDomain隔离的,性能上肯定下降。

    代码下载 

  • 相关阅读:
    centos 查看硬盘情况
    centos 修改默认启动内核,及删除无用内核
    Linux centos关机和重启命令
    MySQL同步故障:" Slave_SQL_Running:No" 两种解决办法
    centos下直接使用命令备份mysql数据库
    centos下导入mysql数据库
    HDP 3.1.0 集成 Sqoop2
    HDP 3.1.0 集成 Sqoop2 踩坑问题记录
    Spark-Bench 测试教程
    PySpark DataFrame 添加自增 ID
  • 原文地址:https://www.cnblogs.com/aarond/p/OSGI.html
Copyright © 2020-2023  润新知