OSGI原形(.NET)

 OSGI原形(.NET)

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

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

代码下载 

自省推动进步,视野决定未来。
心怀远大理想。
为了家庭幸福而努力。
A2D科技,服务社会。
A2D Framework(Alpha)
  • 1. Cache System(本地缓存与分布式缓存共存、支持Memcache和Redis、支持贴标签形式(类似Spring 3.x的Cache形式))
  • 2. Event System(本地事件与分布式事件分发)
  • 3. IoC(自动匹配功能,实例数量限制功能)
  • 4. Sql Dispatcher System(基于Sql server的读写分离系统)
  • 5. Session System(分布式Session系统)
  • 6. 分布式Command Bus(MSMQ实现,解决4M限制,支持Session的读取)
  • 7. 规则引擎
 
原文地址:https://www.cnblogs.com/Leo_wl/p/3312662.html