/// <summary> /// Performs loading into AssemblyLoadContext.Default using LoadFromAssemblyName for TPA assemblies and LoadFromAssemblyPath for other dependecies. /// <para> /// Based on https://github.com/natemcmaster/DotNetCorePlugins/blob/8f5c28fa70f0869a1af2e2904536268f184e71de/src/Plugins/Loader/ManagedLoadContext.cs Load method, /// but avoided FileNotFoundException from LoadFromAssemblyName trying only load TPA assemblies that way. /// </para> /// </summary> /// <param name="managedLibrary">ManagedLibrary object containing assembly name and paths.</param> /// <param name="loadContext">ManagedAssemblyLoadContext object.</param> /// <returns>Returns loaded assembly.</returns> private Assembly LoadAssemblyInternal(ManagedLibrary managedLibrary, ManagedAssemblyLoadContext loadContext) { // To avoid FileNotFoundException for assemblies that are included in TPA - we load them using AssemblyLoadContext.Default.LoadFromAssemblyName. var assemblyFileName = Path.GetFileName(managedLibrary.AppLocalPath); if (TPA.ContainsAssembly(assemblyFileName)) { var defaultAssembly = AssemblyLoadContext.Default.LoadFromAssemblyName(managedLibrary.Name); if (defaultAssembly != null) { return(defaultAssembly); } } if (SearchForLibrary(managedLibrary, loadContext, out var path)) { return(AssemblyLoadContext.Default.LoadFromAssemblyPath(path)); } return(null); }
private void CopyAssemblies(string sourceParentPath, string targetDirectoryPath) { if (sourceParentPath != null) { var sourceDirectoryPath = Path.Combine(sourceParentPath, "bin"); if (Directory.Exists(sourceDirectoryPath)) { foreach (var sourceFilePath in Directory.EnumerateFiles(sourceDirectoryPath, "*.*", SearchOption.AllDirectories)) { // Copy all assembly related files except assemblies that are inlcuded in TPA list if (IsAssemblyRelatedFile(sourceFilePath) && !(IsAssemblyFile(sourceFilePath) && TPA.ContainsAssembly(Path.GetFileName(sourceFilePath)))) { // Copy localization resource files to related subfolders var targetFilePath = Path.Combine( IsLocalizationFile(sourceFilePath) ? Path.Combine(targetDirectoryPath, Path.GetFileName(Path.GetDirectoryName(sourceFilePath))) : targetDirectoryPath, Path.GetFileName(sourceFilePath)); CopyFile(sourceFilePath, targetFilePath); } } } } }