zoukankan      html  css  js  c++  java
  • OSGI:C#如何实现简单的OSGI

     我对OSGI的认识

    • 面向接口编程在组件(DLL)级别的体现。
    • 插件机制的一种扩展。
    • 动态管理组件(DLL)的生命周期(加载、启动、停止、卸载)。
    • 回调和插件的支持,在Bundle的生命周期中,允许通过插件和回调进行拦截。
    • Ioc容器的集成(非必须),如:Bundle启动时自动将服务注册到Ioc中,停止时自动取消服务注册。
    • Ioc管理服务的生命周期,OSGI管理组件的生命周期。

    代码示例(下载地址

    项目结构及他们的依赖关系

    Program.cs 中的代码

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 using Microsoft.Practices.ServiceLocation;
     8 
     9 using Happy.OSGI;
    10 using Happy.Commands;
    11 using Happy.Ioc.Unity;
    12 using Happy.OSGI.Demo.Commands;
    13 
    14 namespace Happy.OSGI.Demo.Host
    15 {
    16     public static class Program
    17     {
    18         public static void Main()
    19         {
    20             Initialize();
    21 
    22             var commandBus = ServiceLocator.Current.GetInstance<CommandBus>();
    23             var command = new TestCommand { Message = "测试消息" };
    24 
    25             Console.WriteLine("默认所有的Bundle都是启动的,执行情况如下:");
    26             commandBus.Send(command);
    27 
    28             Console.WriteLine();
    29 
    30             Console.WriteLine("停止一个Bundle后,执行情况如下:");
    31             BundleContainer.Current.Bundles.First().Stop();
    32             commandBus.Send(command);
    33 
    34             Console.WriteLine();
    35 
    36             Console.WriteLine("重新启动Bundle后,执行情况如下:");
    37             BundleContainer.Current.Bundles.First().Start();
    38             commandBus.Send(command);
    39         }
    40 
    41         private static void Initialize()
    42         {
    43             BundleContainer
    44                 .Current
    45                 .UseDirectoryAssemblyLoader()
    46                 .UseUnity()
    47                 .RegistCommandHandlerByConvention()
    48                 .Start();
    49         }
    50     }
    51 }

    执行结果

  • 相关阅读:
    一天一个Linux命令--find
    一天一个Linux命令--dhclient
    一道CTF题引发的思考——SSI注入
    netcat的简单使用(一)
    文件头类型
    windows10禁用更新方法
    Burpsuite 之intruder
    localstorage sessionstorage和cookie的区别
    js中的迭代方法-every, some, filter, map, forEach
    前端日志分类
  • 原文地址:https://www.cnblogs.com/happyframework/p/2990371.html
Copyright © 2011-2022 走看看