natemcmaster / DotNetCorePlugins

.NET Core library for dynamically loading code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Host-to-Plugin and Plugin-to-Host question

lonnietc opened this issue · comments

Hello,

I am developing an application in which the host will load some Plugins and then call their start method.

I need to be able to do 2 things after loading a plugin:

  1. Start the plugin in a thread so that it runs independent from the host. This is the case when the host is starting up a type of client CLI plugin or as Web server plugin.

  2. I have learned how to call methods in plugins, but I am not sure how to call a set of host methods from a plugin. I think that this is in the interface but am not clear on how it is done.

Can you please assist with these 2 cases?
thanks

Is there any update on this question?

You would use Dependecy Injection to do this easily... i think there is an example in the samples

If you want to call a method in the host, wrap it in a Task.Run() call.

await Task.Run(plugin.Start);

If you want to call a method from the host assembly, you could have a function in your plugin that accepts a function, lambda, etc. as a sort of 'callback'.

public interface IPlugin{
    void RegisterCallback(Action funcFromHost);
}

public class Plugin : IPlugin {
    public Action FuncFromHost;
    public void RegisterCallback(Action funcFromHost) {
        this.FuncFromHost = funcFromHost;
    }
}

// In host
static async Main() {
    var plugin = ...;
    plugin.RegisterCallback(hostFunc);
    await Task.Run(plugin.Start);
}

I haven't tested this.

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please comment if you believe this should remain open, otherwise it will be closed in 14 days. Thank you for your contributions to this project.

Closing due to inactivity.
If you are looking at this issue in the future and think it should be reopened, please make a commented here and mention natemcmaster so he sees the notification.