一手资料来源于“开放工厂”,以下程序将会引用到一个核心文件UIShell.OSGi.dll

目前我对于OSGi这个框架的理解就是,主程序搜索并加载插件,以插件方式开放,便于扩展。

现在开始正式的旅程。

首先新建一个C#控制台应用程序(.NET 4.0)按照惯例取名HelloWorld

添加OSGi.NET的引用(上述dll文件),设置输出目录为bin(建议这样做)

这个程序的主函数(Main)中只做一件事,就是启动插件框架,完整代码如下

  1. using System;
  2. using UIShell.OSGi;
  3. namespace OSGi.HelloWorld
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. using(BundleRuntime bdrt=new BundleRuntime())
  10. {
  11. Console.WriteLine("Start bundle...");
  12. bdrt.Start();
  13. bdrt.Stop();
  14. Console.WriteLine("Press any key to continue...");
  15. Console.ReadKey();
  16. }
  17. }
  18. }
  19. }
using System;
using UIShell.OSGi; namespace OSGi.HelloWorld
{
class Program
{
static void Main(string[] args)
{
using(BundleRuntime bdrt=new BundleRuntime())
{
Console.WriteLine("Start bundle...");
bdrt.Start();
bdrt.Stop();
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
}
}
}

现在,我们的各项功能可以通过插件扩展实现。

给两个示例吧,一个是打印消息,一个是弹出消息框,都很简单。

编写插件需要输出为dll形式,因此我们选择新建Class(类库),同样要添加OSGi.NET的引用

两个类的代码分别如下

  1. using System;
  2. using UIShell.OSGi;
  3. namespace PrintMessagePlugin
  4. {
  5. public class PrintMessage:IBundleActivator
  6. {
  7. public void Start(IBundleContext context)
  8. {
  9. Console.WriteLine("<Start> Hello World  -- PrintMessage");
  10. }
  11. public void Stop(IBundleContext context)
  12. {
  13. Console.WriteLine("<Stop> See you next time. -- PrintMessage");
  14. }
  15. }
  16. }
using System;
using UIShell.OSGi; namespace PrintMessagePlugin
{
public class PrintMessage:IBundleActivator
{
public void Start(IBundleContext context)
{
Console.WriteLine("<Start> Hello World -- PrintMessage");
} public void Stop(IBundleContext context)
{
Console.WriteLine("<Stop> See you next time. -- PrintMessage");
} }
}

输出PrintMessage.dll

  1. using System.Windows.Forms;
  2. using UIShell.OSGi;
  3. namespace PopupMsgBoxPlugin
  4. {
  5. public class PopupMsgBox:IBundleActivator
  6. {
  7. public void Start(IBundleContext context)
  8. {
  9. MessageBox.Show("<Start> Hello World!","MsgBox");
  10. }
  11. public void Stop(IBundleContext context)
  12. {
  13. MessageBox.Show("<Stop> See you.","MsgBox");
  14. }
  15. }
  16. }
using System.Windows.Forms;
using UIShell.OSGi; namespace PopupMsgBoxPlugin
{
public class PopupMsgBox:IBundleActivator
{
public void Start(IBundleContext context)
{
MessageBox.Show("<Start> Hello World!","MsgBox");
} public void Stop(IBundleContext context)
{
MessageBox.Show("<Stop> See you.","MsgBox");
}
}
}

输出PopupmsgBox.dll

为了能让主程序搜索并识别插件信息,我们需要添加清单文件

方法之一是 【项目(右键)】-->【添加(新建项...)】-->【数据(XML文件)】

将*.xml命名为manifest.xml然后根据类库代码编写内容,分别如下

PrintMessage的manifest.xml

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <Bundle xmlns="urn:uiosp-bundle-manifest-2.0"
  3. SymbolicName="PrintMessagePlugin"
  4. Name="PrintMessagePlugin"
  5. Version="1.0.0.0"
  6. InitializedState="Active" >
  7. <Activator Type="PrintMessagePlugin.PrintMessage" />
  8. <Runtime>
  9. <Assembly Path="PrintMessage.dll" />
  10. </Runtime>
  11. </Bundle>
<?xml version="1.0" encoding="utf-8" ?>
<Bundle xmlns="urn:uiosp-bundle-manifest-2.0"
SymbolicName="PrintMessagePlugin"
Name="PrintMessagePlugin"
Version="1.0.0.0"
InitializedState="Active" >
<Activator Type="PrintMessagePlugin.PrintMessage" />
<Runtime>
<Assembly Path="PrintMessage.dll" />
</Runtime>
</Bundle>

PopupMsgBox的manifest.xml

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <Bundle xmlns="urn:uiosp-bundle-manifest-2.0"
  3. SymbolicName="PopupMesgBoxPlugin"
  4. Name="PopupMsgBoxPlugin"
  5. Version="1.0.0.0"
  6. InitializedState="Active" >
  7. <Activator Type="PopupMsgBoxPlugin.PopupMsgBox" />
  8. <Runtime>
  9. <Assembly Path="PopupMsgBox.dll" />
  10. </Runtime>
  11. </Bundle>
<?xml version="1.0" encoding="utf-8" ?>
<Bundle xmlns="urn:uiosp-bundle-manifest-2.0"
SymbolicName="PopupMesgBoxPlugin"
Name="PopupMsgBoxPlugin"
Version="1.0.0.0"
InitializedState="Active" >
<Activator Type="PopupMsgBoxPlugin.PopupMsgBox" />
<Runtime>
<Assembly Path="PopupMsgBox.dll" />
</Runtime>
</Bundle>

然后将生成的*.dll及对应的*.xml清单拷贝到HelloWorld主程序对应的bin/Plugins目录(没有请新建)

其中bin就是HelloWorld主程序的exe输出目录

这是bin目录下的内容


这是bin/Plugins目录下的内容

这两个文件夹下的内容如下

执行主程序HelloWorld,运行结果如下

本文原创,转载请注明出处

http://blog.csdn.net/fengyhack/article/details/40508635

OSGi.NET使用笔记的更多相关文章

  1. OSGI.NET 学习笔记(一)

    1. 关于OSGI.NET 在介绍 OSGI.NET 前先介绍下OSGi, OSGI全称为Open Service Gateway Initiative,它一方面指由IBM.Oracle.BEA.SA ...

  2. OSGi.NET 学习笔记

    OSGi.NET 学习笔记 [目录]   持续更新和调整中,本人学习笔记,非官方文档,难免疏漏,仅供参考. OSGi.NET SDK下载地址. 前言及环境准备 模块化和插件化 概念 实例 小结 面向服 ...

  3. OSGI.NET 学习笔记--架构篇

    关于osgi.net ,想必大家也听说过,以下是自己在学习osgi.net 过程中整理出来的内容,供大家学习参与使用. 1.  UIOSP 开放工厂框架架构 开放工厂所有插件基于OSGi.NET面向服 ...

  4. OSGI.NET 学习笔记--应用篇

    关于osgi.net ,想必大家也听说过,以下是自己在学习osgi.net 过程中整理出来的内容,供大家学习参与使用. 1.  OSGI.NET 与UIOSP OSGi是Open Service Ga ...

  5. OSGI.NET 插件启动方法

    在使用OSGI.NET框架来开发插件过程中,有时为了测试一个插件,或运行一个插件,需要启动主个插件,如果没有主窗口程序,那么该 如何启动一个插件,而不是再开发一个主窗口程序(那样是不是太麻烦,仅仅是为 ...

  6. net资源1

    .net core 例子 https://github.com/aspnet/Docs/tree/master/aspnetcore/fundamentals C#中使用Spire.docx操作Wor ...

  7. 《深入理解OSGi:Equinox原理、应用与最佳实践》笔记_1_运行最简单的bundlehelloworld

    <深入理解OSGi:Equinox原理.应用与最佳实践>笔记_1_运行最简单的bundlehelloworld 买了周大大的OSGI的书看 先前完全没有基础 就靠这本书看看学学 顺便记一些 ...

  8. osgi笔记

    Bundle-Classpath可以实现内嵌jar. 一个Bundle的Activator不需要进行Export 一个Package中的类被两个ClassLoader加载,包中的Private cla ...

  9. OSGI入门笔记

    OSGI框架为Java定义了一个动态模块化系统,它使你可以更好地控制代码结构,动态管理代码的生命周期,并且提供了代码写作的松耦合方式:更值得称道的是,它的规范文档描述详尽.--<OSGI实战&g ...

随机推荐

  1. 微信小程序——单选项

    对于小程序单选,官方文档已经贴出了代码,我这里也不做过多解释,只是分享给大家一个小功能 一般在单选或者多选时,都会出现“其他”这个选项,如果通过input焦点事件.失焦事件来控制,代码会很繁琐 这里可 ...

  2. linux安装PyCharm,PyCharm常用快捷键及调试模式,pycharm里面对文件夹或者文件进行重命名

    PyCharm常用快捷键及调试模式 2017年10月18日 23:13:43 菜鸟之神 阅读数:5835    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn ...

  3. CreateRemoteThread简单应用

    要实现线程的远程注入必须使用Windows提供的CreateRemoteThread函数来创建一个远程线程 该函数的原型如下: HANDLE CreateRemoteThread(      HAND ...

  4. Python学习详细教程-武沛齐

    目录 Python之路[第一篇]:Python简介和入门 Python之路[第二篇]:Python基础(一) Python之路[第三篇]:Python基础(二) Python之路[第四篇]:模块 Py ...

  5. JDK9新特性实战:简化流关闭新姿势。

    做Java开发的都知道,每个资源的打开都需要对应的关闭操作,不然就会使资源一直占用而造成资源浪费,从而降低系统性能. 关于资源的关闭操作,从JDK7-JDK9有了不少的提升及简化. JDK6 在JDK ...

  6. Spring AspectJ 切入点语法详解(7)

    1.Spring AOP支持的AspectJ切入点指示符 切入点指示符用来指示切入点表达式目的,,在Spring AOP中目前只有执行方法这一个连接点,Spring AOP支持的AspectJ切入点指 ...

  7. CSDN的验证码,真得很糟糕

    这是以三种不同的高度来分割各字符 第一张图片是以宽度3来分割,可以看得出,验证码元素保存完好,但 Y 和 9 仍然连在一起 第二张图片是以宽度4来分割,看到了,N已经断了,肉眼虽然仍看得出来是N,但是 ...

  8. git安装与上传

    git安装与上传 上一篇 / 下一篇  2017-03-10 10:09:42 / 个人分类:代码管理工具 查看( 63 ) / 评论( 0 ) / 评分( 0 / 0 ) 1.安装Git-2.11. ...

  9. IDEA将代码推送至远程GitHub仓库

    1 在项目根路径下添加.gitignore文件 2 创建本地git仓库 3 git add操作 快捷键 ctrl+alt+a 4 git commit操作 快捷键ctrl+k 5 git push操作 ...

  10. php判断一个值是否在一个数组中,区分大小写-也可以判断是否在键中

    function in_array_case($value,$array){ return in_array(strtolower($value),array_map('strtolower',$ar ...