Let’s create a console application with OSGi.NET to output the words “Hello World!” (Download First).

There are 2 parts

  • “Host”, a normal console app starts the OSGi.NET runtime environment
  • “Plugin”, an OSGi.NET style console app outputs “Hello World!”

We will see

  • How OSGi.NET manages the Host and Plugins
  • How Host works with Plugins

Here goes

  1. Create an console app named “OSGi.NET.HelloWorld”, that’s the Host

    image

    Set the project “Output Path” to “bin\”, it’s easy to make the relevant path same when switch solution configurations

    image
  2. Add Reference to “UIShell.OSGi.dll”and try to start OSGi.NET BundleRuntime, also output a hint to indicate Host starts successfully

            static void Main(string[] args)
    
            {
    
                using (BundleRuntime bundleRuntime = new BundleRuntime())
    
                {
    
                    bundleRuntime.Start();
    
                    //TODO
    
                    Console.WriteLine("Press enter to exit...");
    
                    Console.ReadLine();
    
                }
    
            }
  3. Going on to create the Plugin Class Library named “OSGi.NET.OutputHelloWorldPlugin”.  Notice, we need put it in “bin\Plugins”of Host

    image

    Set the project “Output Path” to “bin\”, too

    image

  4. Add Reference to “UIShell.OSGi.dll”and rename Class1.cs to Activator.cs which is inheriting “IBundleActivator”and implementing 2 public methods, Start() and Stop().

        public class Activator : IBundleActivator
    
        {
    
            public void Start(IBundleContext context)
    
            {
    
                //todo:
    
                Console.WriteLine("Hello World!");
    
            }
    
            public void Stop(IBundleContext context)
    
            {
    
                //todo:
    
            }
    
        }

    Start() is the entry point of this Plugin, just like the Main() in Program.cs, but we need to declare this in a config file named “Manifest.xml”to BundleRuntime.

    Add Manifest.xml to Plugin project at root

    <?xml version="1.0" encoding="utf-8" ?>
    
    <Bundle xmlns="urn:uiosp-bundle-manifest-2.0" SymbolicName="OSGi.NET.OutputHelloWorldPlugin" Name="OSGi.NET.OutputHelloWorldPlugin" Version="1.0.0.0" InitializedState="Active">
    
      <Activator Type="OSGi.NET.OutputHelloWorldPlugin.Activator"/>
    
      <Runtime>
    
        <Assembly Path="bin\OSGi.NET.OutputHelloWorldPlugin.dll"/>
    
      </Runtime>
    
    </Bundle>
  5. OK, that’s all done. Build whole solution and press F5, you will see this

    image
  6. Let’s detail it and see how this happen

    A. Host starts the BundleRuntime

    B. then runtime search all Manifest.xml files under a specified path, here it’s “bin\Plugins”of Host itself

    C. from Manifest, runtime gets where assembly should be load and where the entry ponit Start() in Activator is

    D. at last, run the Start(), that’s all

  7. If you would like add another Plugin, like what we’ve done from step 3, you will see some interesting things are

    A. Host never know whether there are some Plugins or not, but eventually BundleRuntime will load them all or none

    B. Plugins never konw wheter there is a Host or not, but eventually it will be loaded by BundleRuntime if there is one

    C. Every single Plugin is in a single Folder, you can add it or remove it from “bin\Plugins”,  we don’t need re-build Host or other Plugins but they could work together without any problems

Last edited Mar 21, 2013 at 5:21 AM by shalahu, version 8

Comments

No comments yet.