예제 #1
0
    static Stream LoadGetResourceStreamAndUnload(string assemblyPath, out WeakReference alcWeakRef)
    {
        var alc = new TestAssemblyLoadContext();

        alcWeakRef = new WeakReference(alc);

        Assembly a = alc.LoadFromAssemblyPath(assemblyPath);
        Stream   resourceStream = a.GetManifestResourceStream("test22888.resources");

        alc.Unload();

        return(resourceStream);
    }
        public void CanCreateMultipleEventSources()
        {
            EventSource GetEventSource(TestAssemblyLoadContext testAssemblyLoadContext)
            {
                return((EventSource)Activator.CreateInstance(testAssemblyLoadContext.Assemblies
                                                             .Single()
                                                             .GetType("Azure.Core.Tests.AzureEventSourceTests+TestEventSource")));
            }

            void LogEvent(EventSource azureCoreEventSource)
            {
                azureCoreEventSource.GetType()
                .GetMethod("LogSomething", BindingFlags.Public | BindingFlags.Instance)
                .Invoke(azureCoreEventSource, Array.Empty <object>());
            }

            var alc  = new TestAssemblyLoadContext("Test 1");
            var alc2 = new TestAssemblyLoadContext("Test 2");

            try
            {
                List <EventWrittenEventArgs> events = new();
                using var listener = new AzureEventSourceListener((args, s) => events.Add(args), EventLevel.Verbose);

                alc.LoadFromAssemblyPath(typeof(TestEventSource).Assembly.Location);
                alc2.LoadFromAssemblyPath(typeof(TestEventSource).Assembly.Location);

                using var es0 = new TestEventSource();
                using var es1 = GetEventSource(alc);
                using var es2 = GetEventSource(alc2);

                LogEvent(es0);
                LogEvent(es1);
                LogEvent(es2);

                Assert.AreEqual("Azure-Corez", es0.Name);
                Assert.AreEqual("Azure-Corez-1", es1.Name);
                Assert.AreEqual("Azure-Corez-2", es2.Name);

                Assert.AreEqual(3, events.Count);

                Assert.AreEqual("Azure-Corez", events[0].EventSource.Name);
                Assert.AreEqual("Azure-Corez-1", events[1].EventSource.Name);
                Assert.AreEqual("Azure-Corez-2", events[2].EventSource.Name);
            }
            finally
            {
                alc.Unload();
                alc2.Unload();
            }
        }
        private WeakReference TestAssemblyLoadContextHookStep(int id1, int id2)
        {
            AssemblyLoadContext alc = new TestAssemblyLoadContext($"Test Context #{id1}");

            Assembly asm      = alc.LoadFromAssemblyPath(Assembly.GetExecutingAssembly().Location);
            Type     typeOrig = typeof(AssemblyLoadContextHookTest);
            Type     type     = asm.GetType(typeOrig.FullName);

            Assert.NotEqual(typeOrig, type);

            Verify(null, -1, -1);

            type.GetMethod("TestAssemblyLoadContextHookLoaded", BindingFlags.Public | BindingFlags.Static)
            .Invoke(null, new object[] { this, id1, id2 });

            alc.Unload();

            return(new WeakReference(alc));
        }
예제 #4
0
        static int ExecuteAndUnloadInternal(string assemblyPath, string[] args, Action <AssemblyLoadContext> unloadingCallback, out WeakReference alcWeakRef)
        {
            TestAssemblyLoadContext alc = new TestAssemblyLoadContext();

            if (unloadingCallback != null)
            {
                alc.Unloading += unloadingCallback;
            }
            alcWeakRef = new WeakReference(alc);

            Assembly a = alc.LoadFromAssemblyPath(assemblyPath);

            object[] argsObjArray = (a.EntryPoint.GetParameters().Length != 0) ? new object[] { args } : null;
            object   res          = a.EntryPoint.Invoke(null, argsObjArray);

            alc.Unload();

            return((a.EntryPoint.ReturnType == typeof(void)) ? Environment.ExitCode : Convert.ToInt32(res));
        }
예제 #5
0
    static int ExecuteAndUnload(string assemblyPath, out WeakReference alcWeakRef)
    {
        // <Snippet3>
        var      alc = new TestAssemblyLoadContext();
        Assembly a   = alc.LoadFromAssemblyPath(assemblyPath);

        // </Snippet3>

        alcWeakRef = new WeakReference(alc, trackResurrection: true);

        // <Snippet4>
        var args = new object[1] {
            new string[] { "Hello" }
        };
        int result = (int)a.EntryPoint.Invoke(null, args);

        // </Snippet4>

        // </Snippet5>
        alc.Unload();
        // </Snippet5>

        return(result);
    }