static int ExecuteAndUnload(string assemblyPath, out WeakReference testAlcWeakRef, out MethodInfo testEntryPoint)
        {
            var alc = new TestAssemblyLoadContext();

            testAlcWeakRef = new WeakReference(alc);

            Assembly a = alc.LoadFromAssemblyPath(assemblyPath);

            if (a == null)
            {
                testEntryPoint = null;
                Console.WriteLine("Loading the test assembly failed");
                return(-1);
            }

            var args = new object[1] {
                new string[] { "Hello" }
            };

            // Issue preventing unloading #1 - we keep MethodInfo of a method for an assembly loaded into the TestAssemblyLoadContext in a static variable
            entryPoint     = new TestInfo(a.EntryPoint);
            testEntryPoint = a.EntryPoint;

            int result = (int)a.EntryPoint.Invoke(null, args);

            alc.Unload();

            return(result);
        }
Esempio n. 2
0
        public void Execute(out WeakReference testAlcWeakRef)
        {
            var alc = new TestAssemblyLoadContext();

            testAlcWeakRef = new WeakReference(alc);
            alc.Resolving += (alc2, assemblyName) =>
            {
                var dllName = assemblyName.Name.Split(',').First();

                var dllPath = dllName.Contains("FSharp.Core")
                    ? @"C:\projects\FsFun\Qwantalabs.LoadAssemblies\bin\Release\netcoreapp3.1\FSharp.Core.dll"
                    : @$ "C:\projects\FsFun\Qwantalabs.DummyAssembly\bin\Debug\netstandard2.0\{dllName}.dll";

                return(alc2.LoadFromAssemblyPath(dllPath));
            };