Working on one PoC where i have load and unload assembly on the fly and do Hot reload. i have created custom load context and loading assembly from one folder. Assembly contains only one simple class and implements interface.
public class MyPlugin : IMyPluginInterface
{
public void DoSomething()
{
Console.Write("Hello From Plugin");
}
public void DoSomethingElse()
{
Console.Write("Hello From DoSomethingElse Function");
}
}
My host project, ConfigureServices method where i am loading this assembly.
public void ConfigureServices(IServiceCollection services) { PluginDiscoveryService pluginDiscoverService = new PluginDiscoveryService();
var binFolderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string pluginPath = Path.Combine(binFolderPath, "Plugin");
pluginPath = pluginPath + "\\MyPlugin.dll";
WeakReference hostAlcWeakRef;
IEnumerable<Type> types;
pluginDiscoverService.LoadPluginAssemblyUsingDI(out hostAlcWeakRef, out types);
foreach (var type in types)
{
services.AddTransient(typeof(IMyPluginInterface), type);
}
types = null;
var removePluginInterface = services.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(IMyPluginInterface));
services.Remove(removePluginInterface);
removePluginInterface = null;
for (int i = 0; hostAlcWeakRef.IsAlive && (i < 10); i++)
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
Console.WriteLine($"Unload success: {!hostAlcWeakRef.IsAlive}");
}
here hostAlcWeakRef.IsAlive is coming true. if i comment out foreach (var type in types) dont register type then i am able to unload it.
Please help