1. Home
  2. Docs
  3. Documentation
  4. Plugins
  5. Plugin implementation

Plugin implementation

Next step is to implement the plugin.

Both, client and the server can have plugins. Implementation of the ICalculator plugin will be executed on the server. As noted before, server code and client code should not be mixed, so another project is needed.

Add a new project and name id SB.Plugins.Demo.Server. This project needs a reference to SB.Plugins.Demo.Core and NuGet package SB.PluginSDK.Server.

The plugin has only one method for now, and it’s implementation is very simple:

using SB.Plugins.Demo.Core;

namespace SB.Plugins.Demo.Server
{
    public class CalculatorServer : ICalculatorServer
    {
        public int Sum(int a, int b)
        {
            var sum = a + b;

            // return the sum to the caller
            return sum;
        }
    }
}

What this method does is, calculates the sum and returns the sum to the caller.

The plugin implementation is completed, but there are a few required steps to get SB to run this plugin. SB uses dependency injection to load plugins. Therefore, this plugin needs to be registered in the IServiceCollection. To do so, create a new class and name it Startup. This class needs to implement IConfigurable interface, which is defined in SB.PluginSDK.Server.

The IConfigurable interface contains two methods which are used to configure all plugins in the assembly. Startup class, which registers the CalculatorServer plugin contains:

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SB.PluginSDK;
using SB.PluginSDK.Server;
using System;

namespace SB.Plugins.Demo.Server
{
    public class Startup : IConfigurable
    {
        public void Configure(IServiceProvider serviceProvider)
        {

        }

        public void ConfigureServices(IServiceCollection services, IConfiguration config)
        {
            // use transient lifetime - the plugins are loaded only once on server startup
            // so it doesn't have much effect to use other service lifetimes
            services.AddTransient<IPlugin, CalculatorServer>();
        }
    }
}

Similarity to Asp Net Core is intended. To understand how IServiceCollection works, read more on
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.2

What is important for SB is, this line: services.AddTransient<IPlugin, CalculatorServer>(); When plugins are loaded from each assembly, SB looks for IPlugins, and will use only those registered in the IServiceCollection.

The final step is to build the project, and copy the output to the Plugins folder inside SB directory.

Note: The project’s csproj file should have this line: <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> inside the PropertyGroup element. This forces Visual Studio to publish the dll from this project and all external dependencies. If any dependency is not found, loading the assembly will fail.

Next step: Client implementation.

Was this article helpful to you? Yes No

How can we help?