zoukankan      html  css  js  c++  java
  • 多模块后带来的问题解决方法

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

    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隔离的,性能上肯定下降。

    代码下载 

  • 相关阅读:
    Discuz X 2.5 点点(伪静态)
    jq 、xml 省市级联动
    php memcache 初级使用(2)
    关于windows虚拟内存管理的页目录自映射
    SharePoint 2010 网络上的开发经验和资源
    SharePoint 2010 Reporting Services 报表服务器正在内置 NT AUTHORITY\SYSTEM 账户下运行 解决方法
    SharePoint 2010 Reporting Services 报表服务器无法解密用于访问报表服务器数据库中的敏感数据或加密数据的对称密钥 解决方法
    Active Directory Rights Management Services (AD RMS)无法检索证书层次结构。 解决方法
    SharePoint 2010 Reporting Services 报表服务器实例没有正确配置 解决方法
    SharePoint 2010 页面引用 Reporting Services 展现 List 报表
  • 原文地址:https://www.cnblogs.com/aarond/p/OSGI.html
Copyright © 2011-2022 走看看