0

So I have a program that I want to load an assembly and in the Load method, some assemblies do not seem to be loaded and my methods are not returned. I want to load the assembly, retrieve all test methods, and unload it because I want to then reload it a second time later on in the program.

Also, unload doesn't seem to free the assembly. I try building the referenced DLL after I unload and I get that "...being used by another process. The file is locked by "MyProject""

public class TestAssemblyLoadContext : AssemblyLoadContext
{
    private AssemblyDependencyResolver _resolver;

    public TestAssemblyLoadContext(string pluginPath) : base(isCollectible: true)
    {
        _resolver = new AssemblyDependencyResolver(pluginPath);
    }

    protected override Assembly Load(AssemblyName name)
    {
        string assemblyPath = _resolver.ResolveAssemblyToPath(name);
        if (assemblyPath != null)
        {
            Console.WriteLine($"Loading assembly {assemblyPath} into the TestAssemblyLoadContext");
            return LoadFromAssemblyPath(assemblyPath);
        }

        return null;
    }
}

And I call it here and unload it. I want to call this method twice to compare methods in different git branches and figure out some info in the methods.

    private static IOrderedEnumerable<MethodInfo> GetMethods(string assemblyPath)
    {
        var alc = new TestAssemblyLoadContext(assemblyPath);

        Assembly assembly = alc.LoadFromAssemblyPath(assemblyPath);

        var methods = assembly.GetTypes()
            .SelectMany(t => t.GetMethods())
            .Where(m => m.GetCustomAttributes(typeof(TestAttribute), false).Length > 0 || m.GetCustomAttributes(typeof(TestCaseAttribute), false).Length > 0)
            .OrderBy(tm => tm.DeclaringType.FullName[..tm.DeclaringType.FullName.LastIndexOf(".")]);

        // Assembly is still locked and can't build as an example
        alc.Unload();

        return methods;
    }
  • Try loading your assembly into a different domain. Create a domain and load assembly there. – T.S. Jan 26 '23 at 16:23
  • I'm not sure I understand - why would I need another domain? –  Jan 26 '23 at 16:25
  • 1
    Because if you want to keep loading/unloading assembly, best is to do it in the different domain, which you can also destroy. And your file shouldn't be held anymore. – T.S. Jan 26 '23 at 16:28
  • Can you point me to a reference as I'm new to this and I believe you are talking about the AppDomain class? –  Jan 26 '23 at 16:31
  • I say these things with the hope that you and other OP know how to do a google search: *"c# load assembly into another appdomain"*. **BANG!** SO has it - https://stackoverflow.com/questions/88717/loading-dlls-into-a-separate-appdomain Advise from MS https://learn.microsoft.com/en-us/dotnet/framework/deployment/best-practices-for-assembly-loading – T.S. Jan 26 '23 at 16:51
  • I believe this is the issue - AppDomain is legacy and the new standard is using AssemblyLoadContext - that is .NET Framework and I am in .NET Core –  Jan 26 '23 at 16:56
  • `AppDomain` is available passed net478. And still suggested to use in the certain scenarios https://learn.microsoft.com/en-us/dotnet/framework/deployment/best-practices-for-assembly-loading#consider-using-application-domains – T.S. Jan 26 '23 at 19:33

0 Answers0