Confusion regarding app domain call backs using multiple domains, please help!

  • Thread starter Thread starter k7s41gx
  • Start date Start date
K

k7s41gx

Guest
Dear Reader,


I am learning how to create class libraries using c# with a simple main form. So far I am able to load/unload plugins using seperate appdomains. However I am looking at add the list of plugins loaded and their respective domains. This way, say I add a weather module. I would load the weather.dll file using its own app domain, then everytime I enter weather zipcode it will use the already loaded appdomain/dll to perform the command. Im not sure if I am asking this right. Sample code below.


Main Form:

if (input[0] == "/run")
{
try
{
textBox1.Text = null;
AppDomainSetup domaininfo = new AppDomainSetup();
domaininfo.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
domaininfo.ApplicationName = input[1];
domaininfo.PrivateBinPath = AppDomain.CurrentDomain.BaseDirectory + "Plugins\\";
//Evidence field below is the NULL.
var domain = AppDomain.CreateDomain(input[1], null, domaininfo);
KPlugin child = domain.CreateInstanceAndUnwrap(input[1], input[1] + ".Plug") as KPlugin;
child.Initialize(new DomainHost());
child.Go(input[2]);
//child.Unload();
//AppDomain.Unload(domain);
}
catch (Exception r)
{
output.AppendText(Environment.NewLine);
output.AppendText("Error trying to load plugin: " + r);
output.ScrollToCaret();
foreach (string s in input)
{
output.AppendText(Environment.NewLine);
output.AppendText("STRING LOADER: " + s);
output.ScrollToCaret();
}
}



Kplugin Interface:

public interface KPlugin
{
string Name { get; }
string Explanation { get; }
string Version { get; }
void Initialize(DomainHost host);
void Go(string parameters);
}

Domainhost:

public class DomainHost : MarshalByRefObject
{
// sends any object to the host. The object must be serializable
public void SendData(object data)
{
string msg = data.ToString();
Program.form1.output.AppendText(Environment.NewLine);
Program.form1.output.AppendText(msg);
Program.form1.output.ScrollToCaret();
}
// there is no timeout for host
public override object InitializeLifetimeService() => null;
}

Here is how I unload a currently running appdomain / plugin:

public void UnloadPlugin(string name)
{
IntPtr handle = IntPtr.Zero;
CorRuntimeHost host = new CorRuntimeHost();
try
{
host.EnumDomains(out handle);
while (true)
{
object domain;
host.NextDomain(handle, out domain);
AppDomain myAppDomain = domain as AppDomain;
if (myAppDomain == null)
break;
if (myAppDomain.FriendlyName.Equals(name))
{
AppDomain.Unload(myAppDomain);
}
}
}
finally
{
host.CloseEnum(handle);
}
}


Im not sure if I am making this way more complex then it needs to be? I thought perhaps making a list of appdomains and update that as new domains are created or closed, but then how do I run the "child.go" from an already running class library in a different domain?

Continue reading...
 
Back
Top