OSGi.NET使用笔记
一手资料来源于“开放工厂”,以下程序将会引用到一个核心文件UIShell.OSGi.dll
目前我对于OSGi这个框架的理解就是,主程序搜索并加载插件,以插件方式开放,便于扩展。
现在开始正式的旅程。
首先新建一个C#控制台应用程序(.NET 4.0)按照惯例取名HelloWorld
添加OSGi.NET的引用(上述dll文件),设置输出目录为bin(建议这样做)
这个程序的主函数(Main)中只做一件事,就是启动插件框架,完整代码如下
- 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();
- }
- }
- }
- }
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的引用
两个类的代码分别如下
- 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");
- }
- }
- }
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
- 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");
- }
- }
- }
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
- <?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>
<?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
- <?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>
<?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,运行结果如下
本文原创,转载请注明出处
OSGi.NET使用笔记的更多相关文章
- OSGI.NET 学习笔记(一)
1. 关于OSGI.NET 在介绍 OSGI.NET 前先介绍下OSGi, OSGI全称为Open Service Gateway Initiative,它一方面指由IBM.Oracle.BEA.SA ...
- OSGi.NET 学习笔记
OSGi.NET 学习笔记 [目录] 持续更新和调整中,本人学习笔记,非官方文档,难免疏漏,仅供参考. OSGi.NET SDK下载地址. 前言及环境准备 模块化和插件化 概念 实例 小结 面向服 ...
- OSGI.NET 学习笔记--架构篇
关于osgi.net ,想必大家也听说过,以下是自己在学习osgi.net 过程中整理出来的内容,供大家学习参与使用. 1. UIOSP 开放工厂框架架构 开放工厂所有插件基于OSGi.NET面向服 ...
- OSGI.NET 学习笔记--应用篇
关于osgi.net ,想必大家也听说过,以下是自己在学习osgi.net 过程中整理出来的内容,供大家学习参与使用. 1. OSGI.NET 与UIOSP OSGi是Open Service Ga ...
- OSGI.NET 插件启动方法
在使用OSGI.NET框架来开发插件过程中,有时为了测试一个插件,或运行一个插件,需要启动主个插件,如果没有主窗口程序,那么该 如何启动一个插件,而不是再开发一个主窗口程序(那样是不是太麻烦,仅仅是为 ...
- net资源1
.net core 例子 https://github.com/aspnet/Docs/tree/master/aspnetcore/fundamentals C#中使用Spire.docx操作Wor ...
- 《深入理解OSGi:Equinox原理、应用与最佳实践》笔记_1_运行最简单的bundlehelloworld
<深入理解OSGi:Equinox原理.应用与最佳实践>笔记_1_运行最简单的bundlehelloworld 买了周大大的OSGI的书看 先前完全没有基础 就靠这本书看看学学 顺便记一些 ...
- osgi笔记
Bundle-Classpath可以实现内嵌jar. 一个Bundle的Activator不需要进行Export 一个Package中的类被两个ClassLoader加载,包中的Private cla ...
- OSGI入门笔记
OSGI框架为Java定义了一个动态模块化系统,它使你可以更好地控制代码结构,动态管理代码的生命周期,并且提供了代码写作的松耦合方式:更值得称道的是,它的规范文档描述详尽.--<OSGI实战&g ...
随机推荐
- mac 10.9 install cocoapods issue
If you've installed the OS X Mavericks Beta and you're having ruby issues like this: /System/Library ...
- manacher/马拉车常用用法一览
因为manacher算法把原来的字符串扩大了两倍,因此在应用时许多二级结论都非常不直观,现场推出来很麻烦,因此笔者在此做个简单整理,如果发现有错误或者有常用的我没有涉及到的,恳请在下方评论区指出,我会 ...
- JavaScript实现几种常见的图形
一.四种常见的三角形 第一种三角形: for(var i=1;i<=5;i++){ for( var j=i;j<=5;j++){ docum ...
- Eclipse Unable to install breakpoint in XXX 解决办法
Debug 时偶尔会出现:Eclipse Unable to install breakpoint in XXX 情况一: 清除所有断点就行了,原因是断点打到注释上了. breakpoint 窗口: ...
- 基本包装类型Boolean、Number、String特性及常用方法
基本包装类型:Boolean.Number.String 一.String 字符串常用方法 1.indexOf() lastIndexOf() 返回相应字符的索引号 2.slice(index1, ...
- data hazard in CPU pipeline
1, background info 5 stages in CPU pipeline: IF, ID, EX, MM, WB IF – Instruction Fetch ID – Instruct ...
- 廖雪峰Java15JDBC编程-3JDBC接口-3JDBC更新
使用update语句的时候,需要通过JDBC实现update语句的执行,这个时候仍然通过PreparedStatement对象来使用,直接传入update语句,然后通过setObject传入占位符的值 ...
- 廖雪峰Java13网络编程-3其他-1HTTP编程
1.HTTP协议: Hyper Text Transfer Protocol:超文本传输协议 基于TCP协议之上的请求/响应协议 目前使用最广泛的高级协议 * 使用浏览器浏览网页和服务器交互使用的就是 ...
- html+css简单的实现360搜索引擎首页面
今天主要学习了是如何实现的,以及我在写这个页面的时候我所遇到的一些困难. 主要实现是用代码的,不说废话了,其实我是想说我走的坑有哪些. 1.代码的基础不好,元素的一些属性不熟悉,对于HTML和css还 ...
- 组件component
<!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...