First we define a couple of interfaces:
// this is your host public interface IHostController { // names of all loaded plugins string[] Plugins { get; } void StartPlugin(string name); void StopPlugin(string name); } public interface IPlugin { // with this method you will pass plugin a reference to host void Init(IHostController host); void Start(); void Stop(); } // helper class to combine app domain and loader together public class PluginInfo { public AppDomain Domain { get; set; } public RemoteLoader Loader { get; set; } }
Now the RemoteLoader is slightly rewritten (for me this did not work):
public class RemoteLoader : MarshalByRefObject { private Assembly _pluginAassembly; private IPlugin _instance; private string _name; public void Init(IHostController host, string assemblyPath) {
And host:
// note : inherits from MarshalByRefObject and implements interface public class HostController : MarshalByRefObject, IHostController { private readonly Dictionary<string, PluginInfo> _plugins = new Dictionary<string, PluginInfo>(); public void ScanAssemblies(params string[] paths) { foreach (var path in paths) { var setup = new AppDomainSetup(); var domain = AppDomain.CreateDomain(Path.GetFileNameWithoutExtension(path), null, setup); var assemblyPath = Assembly.GetExecutingAssembly().Location; var loader = (RemoteLoader) domain.CreateInstanceFromAndUnwrap(assemblyPath, typeof (RemoteLoader).FullName); // you are passing "this" (which is IHostController) to your plugin here loader.Init(this, path); _plugins.Add(loader.Name, new PluginInfo { Domain = domain, Loader = loader }); } } public string[] Plugins => _plugins.Keys.ToArray(); public void StartPlugin(string name) { if (_plugins.ContainsKey(name)) { var p = _plugins[name].Loader; if (!p.IsStarted) { p.Start(); } } } public void StopPlugin(string name) { if (_plugins.ContainsKey(name)) { var p = _plugins[name].Loader; if (p.IsStarted) { p.Stop(); } } } }
Now create two different assemblies. Each of them only needs to bind the IPlugin and IHostController interfaces. In the first build, define the plugin:
public class FirstPlugin : IPlugin { const string Name = "First Plugin"; public void Init(IHostController host) { Console.WriteLine(Name + " initialized"); } public void Start() { Console.WriteLine(Name + " started"); } public void Stop() { Console.WriteLine(Name + " stopped"); } }
In the second build, define another plugin:
public class FirstPlugin : IPlugin { const string Name = "Second Plugin"; private Timer _timer; private IHostController _host; public void Init(IHostController host) { Console.WriteLine(Name + " initialized"); _host = host; } public void Start() { Console.WriteLine(Name + " started"); Console.WriteLine("Will try to restart first plugin every 5 seconds"); _timer = new Timer(RestartFirst, null, 5000, 5000); } int _iteration = 0; private void RestartFirst(object state) {
Now in your main .exe that contains all the plugins:
static void Main(string[] args) { var host = new HostController(); host.ScanAssemblies(@"path to your first Plugin1.dll", @"path to your second Plugin2.dll"); host.StartPlugin("Plugin2"); Console.ReadKey(); }
And the result:
First Plugin initialized Second Plugin initialized Second Plugin started Will try to restart first plugin every 5 seconds Found plugin Plugin1 Found plugin Plugin2 Trying to start first plugin First Plugin started Found plugin Plugin1 Found plugin Plugin1 Found plugin Plugin2 Trying to stop first plugin Found plugin Plugin2 Trying to stop first plugin First Plugin stopped First Plugin stopped Found plugin Plugin1 Found plugin Plugin2 Trying to stop first plugin