Welcome

首页 / 软件开发 / .NET编程技术 / .NET中多模块后带来的问题解决方法:OSGI原形

.NET中多模块后带来的问题解决方法:OSGI原形2014-07-03 cnblogs Aaron目前只做了基础的功能,比如:

各个模块单独的AppDomain容器

Activator激活

导出的服务检查

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

先来说下基本结构:

目前这个框架对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>();//卸载服务}}