Ejemplo n.º 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);
    }
Ejemplo n.º 2
0
    private static Assembly LoadUsingLoadOverride(bool collectibleParent)
    {
        TestAssemblyLoadContext alc2 = new TestAssemblyLoadContext("Test2", collectibleParent);
        Assembly assembly            = alc2.LoadFromAssemblyPath(Test.GetTestAssemblyPath(@"..\TestClass\TestClass.dll"));

        Type t = assembly.GetType("TestClass.Class");

        Console.WriteLine($"Load done, type {t} obtained");

        interfaceAssemblyRef = alc2.InterfaceAssemblyRef;

        return(assembly);
    }
        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));
        }
Ejemplo n.º 5
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));
        }
Ejemplo n.º 6
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);
    }
Ejemplo n.º 7
0
        public void Load_Contract_Compiled_Against_V1_With_V2_SmartContracts_Succeeds()
        {
            var source = @"
using Stratis.SmartContracts;

public class TestContract : SmartContract
{
    public bool AMethod(){ return true; }
}
";

            var loader   = @"
using Stratis.SmartContracts;
using System.Reflection;
using System;

public class TestLoader
{
    public static bool Load(Assembly assembly)
    {
        var contractType = assembly.GetType(""TestContract"");

        if (contractType == null)
            return false;

        var method = contractType.GetMethod(""TestMethod"");

        if (method == null)
            return false;

        var instance = (SmartContract) Activator.CreateInstance(contractType);

        var result = (string)method.Invoke(instance, null);

        return !string.IsNullOrWhiteSpace(result);
    }
}
";
            var basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            var references = new List <MetadataReference>
            {
                MetadataReference.CreateFromFile(Path.Combine(basePath, "Packages", "netcoreapp3.1", "System.Runtime.dll")),
            };

            // Stratis.SmartContracts.SmartContract with the constructor removed
            var version1DllPath = Path.Combine(basePath, "Packages", "1.0.0-TEST", "Stratis.SmartContracts.dll");

            // Version 2.0.0-TEST adds string TestMethod() to Stratis.SmartContracts.SmartContract
            // and GetString() to Stratis.SmartContracts.ISmartContractState
            var version2DllPath = Path.Combine(basePath, "Packages", "4.0.0-TEST", "Stratis.SmartContracts.dll");

            references.Add(MetadataReference.CreateFromFile(version1DllPath));

            SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source);

            var compilation = CSharpCompilation.Create(
                "smartContract",
                new[] { syntaxTree },
                references,
                new CSharpCompilationOptions(
                    OutputKind.DynamicallyLinkedLibrary,
                    checkOverflow: true));

            byte[] version1CompiledContract;

            using (var dllStream = new MemoryStream())
            {
                EmitResult emitResult = compilation.Emit(dllStream);

                Assert.True(emitResult.Success);

                version1CompiledContract = dllStream.ToArray();
            }

            SyntaxTree syntaxTreeLoader = CSharpSyntaxTree.ParseText(loader);

            var loaderCompilation = CSharpCompilation.Create(
                "loader",
                new[] { syntaxTreeLoader },
                references,
                new CSharpCompilationOptions(
                    OutputKind.DynamicallyLinkedLibrary,
                    checkOverflow: true));

            var alc = new TestAssemblyLoadContext();

            Assembly version2Assembly = alc.LoadFromAssemblyPath(version2DllPath);

            Assert.Equal(Version.Parse("4.0.0.0"), version2Assembly.GetName().Version);

            Assembly loaderAssembly;

            using (var dllStream = new MemoryStream())
            {
                EmitResult emitResult = loaderCompilation.Emit(dllStream);

                Assert.True(emitResult.Success);

                dllStream.Seek(0, SeekOrigin.Begin);
                loaderAssembly = alc.LoadFromStream(dllStream);
            }

            var      version1ContractMemoryStream = new MemoryStream(version1CompiledContract);
            Assembly version1ContractAssembly     = alc.LoadFromStream(version1ContractMemoryStream);

            version1ContractMemoryStream.Dispose();

            Type loaderType = loaderAssembly.ExportedTypes.First(t => t.Name == "TestLoader");

            MethodInfo loaderMethod = loaderType.GetMethod("Load");

            var version2MethodInvocationResult = (bool)loaderMethod.Invoke(null, new[] { version1ContractAssembly });

            // If this condition is not null, we have a v1 contract referencing a v2 assembly and a successful
            // invocation of a method that only exists on v2
            Assert.True(version2MethodInvocationResult);
        }