public CoreClrAnalyzerAssemblyLoader()
        {
            _loadContext = AssemblyLoadContext.GetLoadContext(typeof(CoreClrAnalyzerAssemblyLoader).GetTypeInfo().Assembly);

            _loadContext.Resolving += (context, name) =>
            {
                Debug.Assert(ReferenceEquals(context, _loadContext));
                return Load(name.FullName);
            };
        }
            public LoadContextWrapper(AssemblyLoadContext assemblyLoadContext)
            {
                _assemblyLoadContext = assemblyLoadContext;

                var typeInfo = _assemblyLoadContext.GetType().GetTypeInfo();
                var loaderFileMethod = typeInfo.GetDeclaredMethod("LoadFile");
                var loadStreamMethod = typeInfo.GetDeclaredMethod("LoadStream");

                _loadFile = (Func<string, Assembly>)loaderFileMethod.CreateDelegate(typeof(Func<string, Assembly>), _assemblyLoadContext);
                _loadStream = (Func<Stream, Stream, Assembly>)loadStreamMethod.CreateDelegate(typeof(Func<Stream, Stream, Assembly>), _assemblyLoadContext);
            }
Exemple #3
0
    public static void LoadLazyAssemblyInALC(string asm_base64, string pdb_base64)
    {
        var context = new System.Runtime.Loader.AssemblyLoadContext("testContext", true);

        byte[] asm_bytes = Convert.FromBase64String(asm_base64);
        byte[] pdb_bytes = null;
        if (pdb_base64 != null)
        {
            pdb_bytes = Convert.FromBase64String(pdb_base64);
        }

        loadedAssembly = context.LoadFromStream(new System.IO.MemoryStream(asm_bytes), new System.IO.MemoryStream(pdb_bytes));
        Console.WriteLine($"Loaded - {loadedAssembly}");
    }
 // This will be used to set the AssemblyLoadContext for DefaultContext, for the AppDomain,
 // by a host. Once set, the runtime will invoke the LoadFromAssemblyName method against it to perform
 // assembly loads for the DefaultContext.
 //
 // This method will throw if the Default AssemblyLoadContext is already set or the Binding model is already locked.
 public static void InitializeDefaultContext(AssemblyLoadContext context)
 {
     if (context == null)
     {
         throw new ArgumentNullException(nameof(context));
     }
     
     // Try to override the default assembly load context
     if (!AssemblyLoadContext.OverrideDefaultAssemblyLoadContextForCurrentDomain(context.m_pNativeAssemblyLoadContext))
     {
         throw new InvalidOperationException(Environment.GetResourceString("AppDomain_BindingModelIsLocked"));
     }
     
     // Update the managed side as well.
     s_DefaultAssemblyLoadContext = context;
 }
Exemple #5
0
 private static void Assembly_Unloading(Runtime.Loader.AssemblyLoadContext obj)
 {
 }
Exemple #6
0
 public HomeController(AssemblyLoadContext accessor)
 {
     _accessor = accessor;
 }
        private Assembly Load(AssemblyLoadContext loadContext, AssemblyName assemblyName)
        {
            lock (_guard)
            {
                // Try and grab assembly using standard load
                Assembly assembly = AppContextLoad(assemblyName);
                if (assembly != null)
                {
                    return assembly;
                }

                string fullName = assemblyName.FullName;

                if (_namesToAssemblies.TryGetValue(fullName, out assembly))
                {
                    return assembly;
                }

                AssemblyIdentity requestedIdentity;
                if (!AssemblyIdentity.TryParseDisplayName(fullName, out requestedIdentity))
                {
                    return null;
                }

                foreach (var candidatePath in _dependencyPaths)
                {
                    if (IsAssemblyAlreadyLoaded(candidatePath) ||
                        !FileMatchesAssemblyName(candidatePath, requestedIdentity.Name))
                    {
                        continue;
                    }

                    var candidateIdentity = TryGetAssemblyIdentity(candidatePath);

                    if (requestedIdentity.Equals(candidateIdentity))
                    {
                        return LoadAndCache(candidatePath);
                    }
                }

                return null;
            }
        }
        /// <summary>
        /// The handler for the Resolving event
        /// </summary>
        private Assembly Resolve(AssemblyLoadContext loadContext, AssemblyName assemblyName)
        {
            // Probe the assembly cache
            Assembly asmLoaded;
            if (TryGetAssemblyFromCache(assemblyName, out asmLoaded))
                return asmLoaded;

            // Prepare to load the assembly
            lock (s_syncObj)
            {
                // Probe the cache again in case it's already loaded
                if (TryGetAssemblyFromCache(assemblyName, out asmLoaded))
                    return asmLoaded;

                // Search the specified assembly in probing paths, and load it through 'LoadFromAssemblyPath' if the file exists and matches the requested AssemblyName.
                // If the CultureName of the requested assembly is not NullOrEmpty, then it's a resources.dll and we need to search corresponding culture sub-folder.
                bool isAssemblyFileFound = false, isAssemblyFileMatching = false;
                string asmCultureName = assemblyName.CultureName ?? string.Empty;
                string asmFilePath = null;

                for (int i = 0; i < _probingPaths.Count; i++)
                {
                    string probingPath = _probingPaths[i];
                    string asmCulturePath = Path.Combine(probingPath, asmCultureName);
                    for (int k = 0; k < _extensions.Length; k++)
                    {
                        string asmFileName = assemblyName.Name + _extensions[k];
                        asmFilePath = Path.Combine(asmCulturePath, asmFileName);

                        if (File.Exists(asmFilePath))
                        {
                            isAssemblyFileFound = true;
                            AssemblyName asmNameFound = GetAssemblyName(asmFilePath);
                            if (IsAssemblyMatching(assemblyName, asmNameFound))
                            {
                                isAssemblyFileMatching = true;
                                break;
                            }
                        }
                    }

                    if (isAssemblyFileFound && isAssemblyFileMatching)
                    {
                        break;
                    }
                }

                // We failed to find the assembly file; or we found the file, but the assembly file doesn't match the request.
                // In this case, return null so that other Resolving event handlers can kick in to resolve the request.
                if (!isAssemblyFileFound || !isAssemblyFileMatching)
                {
                    return null;
                }

                asmLoaded = asmFilePath.EndsWith(".ni.dll", StringComparison.OrdinalIgnoreCase)
                                ? loadContext.LoadFromNativeImagePath(asmFilePath, null)
                                : loadContext.LoadFromAssemblyPath(asmFilePath);
                if (asmLoaded != null)
                {
                    // Add the loaded assembly to the cache
                    s_assemblyCache.TryAdd(assemblyName.Name, asmLoaded);
                }
            }

            // Raise AssemblyLoad event
            OnAssemblyLoaded(asmLoaded);
            return asmLoaded;
        }
Exemple #9
0
 public AssemblyLoader()
 {
     Context = new CustomAssemblyLoadContext();
 }
Exemple #10
0
        // This method is invoked by the VM when using the host-provided assembly load context
        // implementation.
        private static IntPtr ResolveUnmanagedDll(String unmanagedDllName, IntPtr gchManagedAssemblyLoadContext)
        {
            AssemblyLoadContext context = (AssemblyLoadContext)(GCHandle.FromIntPtr(gchManagedAssemblyLoadContext).Target);

            return(context.LoadUnmanagedDll(unmanagedDllName));
        }
Exemple #11
0
 public AlekseyDelegateALC(System.Runtime.Loader.AssemblyLoadContext d)
 {
     Console.WriteLine("Created a1");
     this.d = d;
 }
Exemple #12
0
        // This method is invoked by the VM when using the host-provided assembly load context
        // implementation.
        private static Assembly Resolve(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName)
        {
            AssemblyLoadContext context = (AssemblyLoadContext)(GCHandle.FromIntPtr(gchManagedAssemblyLoadContext).Target);

            return(context.ResolveUsingLoad(assemblyName));
        }
        // This method is invoked by the VM to resolve a native library using the ResolvingUnmanagedDll event
        // after trying all other means of resolution.
        private static IntPtr ResolveUnmanagedDllUsingEvent(string unmanagedDllName, Assembly assembly, IntPtr gchManagedAssemblyLoadContext)
        {
            AssemblyLoadContext context = (AssemblyLoadContext)(GCHandle.FromIntPtr(gchManagedAssemblyLoadContext).Target) !;

            return(context.GetResolvedUnmanagedDll(assembly, unmanagedDllName));
        }
Exemple #14
0
        private static void MonoResolveUnmanagedDllUsingEvent(string unmanagedDllName, Assembly assembly, IntPtr gchManagedAssemblyLoadContext, ref IntPtr dll)
        {
            AssemblyLoadContext context = GetAssemblyLoadContext(gchManagedAssemblyLoadContext);

            dll = context.GetResolvedUnmanagedDll(assembly, unmanagedDllName);
        }
Exemple #15
0
        private static void MonoResolveUnmanagedDll(string unmanagedDllName, IntPtr gchManagedAssemblyLoadContext, ref IntPtr dll)
        {
            AssemblyLoadContext context = GetAssemblyLoadContext(gchManagedAssemblyLoadContext);

            dll = context.LoadUnmanagedDll(unmanagedDllName);
        }
Exemple #16
0
        // Invoked by Mono to resolve requests to load satellite assemblies.
        private static Assembly?MonoResolveUsingResolveSatelliteAssembly(IntPtr gchALC, string assemblyName)
        {
            AssemblyLoadContext context = GetAssemblyLoadContext(gchALC);

            return(context.ResolveSatelliteAssembly(new AssemblyName(assemblyName)));
        }