Merge pull request #2412 from sbwalker/dev

optimize assembly loading for MAUI to use client storage
This commit is contained in:
Shaun Walker 2022-09-11 10:50:20 -04:00 committed by GitHub
commit 530d80a011
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 267 additions and 129 deletions

View File

@ -71,43 +71,41 @@ namespace Oqtane.Client
// get assemblies from server and load into client app domain // get assemblies from server and load into client app domain
var zip = await http.GetByteArrayAsync($"/api/Installation/load"); var zip = await http.GetByteArrayAsync($"/api/Installation/load");
var dlls = new Dictionary<string, byte[]>();
var pdbs = new Dictionary<string, byte[]>();
// asemblies and debug symbols are packaged in a zip file // asemblies and debug symbols are packaged in a zip file
using (ZipArchive archive = new ZipArchive(new MemoryStream(zip))) using (ZipArchive archive = new ZipArchive(new MemoryStream(zip)))
{ {
var dlls = new Dictionary<string, byte[]>();
var pdbs = new Dictionary<string, byte[]>();
foreach (ZipArchiveEntry entry in archive.Entries) foreach (ZipArchiveEntry entry in archive.Entries)
{ {
if (!assemblies.Contains(Path.GetFileNameWithoutExtension(entry.FullName))) using (var memoryStream = new MemoryStream())
{ {
using (var memoryStream = new MemoryStream()) entry.Open().CopyTo(memoryStream);
byte[] file = memoryStream.ToArray();
switch (Path.GetExtension(entry.FullName))
{ {
entry.Open().CopyTo(memoryStream); case ".dll":
byte[] file = memoryStream.ToArray(); dlls.Add(entry.FullName, file);
switch (Path.GetExtension(entry.FullName)) break;
{ case ".pdb":
case ".dll": pdbs.Add(entry.FullName, file);
dlls.Add(entry.FullName, file); break;
break;
case ".pdb":
pdbs.Add(entry.FullName, file);
break;
}
} }
} }
} }
}
foreach (var item in dlls) // load assemblies into app domain
foreach (var item in dlls)
{
if (pdbs.ContainsKey(item.Key.Replace(".dll", ".pdb")))
{ {
if (pdbs.ContainsKey(item.Key)) AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(item.Value), new MemoryStream(pdbs[item.Key.Replace(".dll", ".pdb")]));
{ }
AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(item.Value), new MemoryStream(pdbs[item.Key])); else
} {
else AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(item.Value));
{
AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(item.Value));
}
} }
} }
} }

View File

@ -4,6 +4,8 @@ using System.Runtime.Loader;
using System.Diagnostics; using System.Diagnostics;
using Oqtane.Modules; using Oqtane.Modules;
using Oqtane.Services; using Oqtane.Services;
using System.Globalization;
using System.Text.Json;
namespace Oqtane.Maui; namespace Oqtane.Maui;
@ -61,49 +63,116 @@ public static class MauiProgram
{ {
try try
{ {
// get list of loaded assemblies on the client // ensure local assembly folder exists
var assemblies = AppDomain.CurrentDomain.GetAssemblies().Select(a => a.GetName().Name).ToList(); string folder = Path.Combine(FileSystem.Current.AppDataDirectory, "oqtane");
if (!Directory.Exists(folder))
// get assemblies from server and load into client app domain
var zip = Task.Run(() => http.GetByteArrayAsync("/api/Installation/load")).GetAwaiter().GetResult();
// asemblies and debug symbols are packaged in a zip file
using (ZipArchive archive = new ZipArchive(new MemoryStream(zip)))
{ {
var dlls = new Dictionary<string, byte[]>(); Directory.CreateDirectory(folder);
var pdbs = new Dictionary<string, byte[]>(); }
foreach (ZipArchiveEntry entry in archive.Entries) var dlls = new Dictionary<string, byte[]>();
var pdbs = new Dictionary<string, byte[]>();
var filter = new List<string>();
var files = new List<string>();
foreach (var file in Directory.EnumerateFiles(folder, "*.dll", SearchOption.AllDirectories))
{
files.Add(file.Substring(folder.Length + 1).Replace("\\", "/"));
}
if (files.Count() != 0)
{
// get list of assemblies from server
var json = Task.Run(() => http.GetStringAsync("/api/Installation/list")).GetAwaiter().GetResult();
var assemblies = JsonSerializer.Deserialize<List<string>>(json);
// determine which assemblies need to be downloaded
foreach (var assembly in assemblies)
{ {
if (!assemblies.Contains(Path.GetFileNameWithoutExtension(entry.FullName))) var file = files.FirstOrDefault(item => item.Contains(assembly));
if (file == null)
{
filter.Add(assembly);
}
else
{
// check if newer version available
if (GetFileDate(assembly) > GetFileDate(file))
{
filter.Add(assembly);
}
}
}
// get assemblies already downloaded
foreach (var file in files)
{
if (assemblies.Contains(file) && !filter.Contains(file))
{
dlls.Add(file, File.ReadAllBytes(Path.Combine(folder, file)));
var pdb = file.Replace(".dll", ".pdb");
if (File.Exists(Path.Combine(folder, pdb)))
{
pdbs.Add(pdb, File.ReadAllBytes(Path.Combine(folder, pdb)));
}
}
else // file is deprecated
{
File.Delete(Path.Combine(folder, file));
}
}
}
else
{
filter.Add("*");
}
if (filter.Count != 0)
{
// get assemblies from server
var zip = Task.Run(() => http.GetByteArrayAsync("/api/Installation/load?list=" + string.Join(",", filter))).GetAwaiter().GetResult();
// asemblies and debug symbols are packaged in a zip file
using (ZipArchive archive = new ZipArchive(new MemoryStream(zip)))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{ {
using (var memoryStream = new MemoryStream()) using (var memoryStream = new MemoryStream())
{ {
entry.Open().CopyTo(memoryStream); entry.Open().CopyTo(memoryStream);
byte[] file = memoryStream.ToArray(); byte[] file = memoryStream.ToArray();
switch (Path.GetExtension(entry.FullName))
// save assembly to local folder
int subfolder = entry.FullName.IndexOf('/');
if (subfolder != -1 && !Directory.Exists(Path.Combine(folder, entry.FullName.Substring(0, subfolder))))
{ {
case ".dll": Directory.CreateDirectory(Path.Combine(folder, entry.FullName.Substring(0, subfolder)));
dlls.Add(entry.FullName, file); }
break; using var stream = File.Create(Path.Combine(folder, entry.FullName));
case ".pdb": stream.Write(file, 0, file.Length);
pdbs.Add(entry.FullName, file);
break; if (Path.GetExtension(entry.FullName) == ".dll")
{
dlls.Add(entry.FullName, file);
}
else
{
pdbs.Add(entry.FullName, file);
} }
} }
} }
} }
}
foreach (var item in dlls) // load assemblies into app domain
foreach (var item in dlls)
{
if (pdbs.ContainsKey(item.Key.Replace(".dll", ".pdb")))
{ {
if (pdbs.ContainsKey(item.Key)) AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(item.Value), new MemoryStream(pdbs[item.Key.Replace(".dll", ".pdb")]));
{ }
AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(item.Value), new MemoryStream(pdbs[item.Key])); else
} {
else AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(item.Value));
{
AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(item.Value));
}
} }
} }
} }
@ -113,6 +182,12 @@ public static class MauiProgram
} }
} }
private static DateTime GetFileDate(string filepath)
{
var segments = filepath.Split('.');
return DateTime.ParseExact(segments[segments.Length - 2], "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
}
private static void RegisterModuleServices(Assembly assembly, IServiceCollection services) private static void RegisterModuleServices(Assembly assembly, IServiceCollection services)
{ {
// dynamically register module scoped services // dynamically register module scoped services

View File

@ -18,6 +18,9 @@ using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Diagnostics;
using static System.Net.WebRequestMethods;
namespace Oqtane.Controllers namespace Oqtane.Controllers
{ {
@ -49,7 +52,7 @@ namespace Oqtane.Controllers
[HttpPost] [HttpPost]
public async Task<Installation> Post([FromBody] InstallConfig config) public async Task<Installation> Post([FromBody] InstallConfig config)
{ {
var installation = new Installation {Success = false, Message = ""}; var installation = new Installation { Success = false, Message = "" };
if (ModelState.IsValid && (User.IsInRole(RoleNames.Host) || string.IsNullOrEmpty(_configManager.GetSetting("ConnectionStrings:" + SettingKeys.ConnectionStringKey, "")))) if (ModelState.IsValid && (User.IsInRole(RoleNames.Host) || string.IsNullOrEmpty(_configManager.GetSetting("ConnectionStrings:" + SettingKeys.ConnectionStringKey, ""))))
{ {
@ -85,7 +88,7 @@ namespace Oqtane.Controllers
[Authorize(Roles = RoleNames.Host)] [Authorize(Roles = RoleNames.Host)]
public Installation Upgrade() public Installation Upgrade()
{ {
var installation = new Installation {Success = true, Message = ""}; var installation = new Installation { Success = true, Message = "" };
_installationManager.UpgradeFramework(); _installationManager.UpgradeFramework();
return installation; return installation;
} }
@ -98,107 +101,169 @@ namespace Oqtane.Controllers
_installationManager.RestartApplication(); _installationManager.RestartApplication();
} }
// GET api/<controller>/load // GET api/<controller>/list
[HttpGet("load")] [HttpGet("list")]
public IActionResult Load() public List<string> List()
{ {
return File(GetAssemblies(), System.Net.Mime.MediaTypeNames.Application.Octet, "oqtane.dll"); return GetAssemblyList();
} }
private byte[] GetAssemblies() // GET api/<controller>/load?list=x,y
{ [HttpGet("load")]
return _cache.GetOrCreate("assemblies", entry => public IActionResult Load(string list = "*")
{
return File(GetAssemblies(list), System.Net.Mime.MediaTypeNames.Application.Octet, "oqtane.dll");
}
private List<string> GetAssemblyList()
{
// get list of assemblies which should be downloaded to client
var binFolder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var assemblies = AppDomain.CurrentDomain.GetOqtaneClientAssemblies();
var list = assemblies.Select(a => a.GetName().Name).ToList();
// include version numbers
for (int i = 0; i < list.Count; i++)
{ {
// get list of assemblies which should be downloaded to client list[i] = Path.GetFileName(AddFileDate(Path.Combine(binFolder, list[i] + ".dll")));
var assemblies = AppDomain.CurrentDomain.GetOqtaneClientAssemblies(); }
var list = assemblies.Select(a => a.GetName().Name).ToList();
var binFolder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
// insert satellite assemblies at beginning of list // insert satellite assemblies at beginning of list
foreach (var culture in _localizationManager.GetInstalledCultures()) foreach (var culture in _localizationManager.GetInstalledCultures())
{
var assembliesFolderPath = Path.Combine(binFolder, culture);
if (culture == Constants.DefaultCulture)
{ {
var assembliesFolderPath = Path.Combine(binFolder, culture); continue;
if (culture == Constants.DefaultCulture)
{
continue;
}
if (Directory.Exists(assembliesFolderPath))
{
foreach (var resourceFile in Directory.EnumerateFiles(assembliesFolderPath))
{
list.Insert(0, Path.Combine(culture, Path.GetFileNameWithoutExtension(resourceFile)));
}
}
else
{
_filelogger.LogError(Utilities.LogMessage(this, $"The Satellite Assembly Folder For {culture} Does Not Exist"));
}
} }
// insert module and theme dependencies at beginning of list if (Directory.Exists(assembliesFolderPath))
foreach (var assembly in assemblies)
{ {
foreach (var type in assembly.GetTypes().Where(item => item.GetInterfaces().Contains(typeof(IModule)))) foreach (var resourceFile in Directory.EnumerateFiles(assembliesFolderPath))
{ {
var instance = Activator.CreateInstance(type) as IModule; list.Insert(0, culture + "/" + Path.GetFileName(AddFileDate(resourceFile)));
foreach (string name in instance.ModuleDefinition.Dependencies.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
if (System.IO.File.Exists(Path.Combine(binFolder, name + ".dll")))
{
if (!list.Contains(name)) list.Insert(0, name);
}
else
{
_filelogger.LogError(Utilities.LogMessage(this, $"Module {instance.ModuleDefinition.ModuleDefinitionName} Dependency {name}.dll Does Not Exist"));
}
}
} }
foreach (var type in assembly.GetTypes().Where(item => item.GetInterfaces().Contains(typeof(ITheme)))) }
else
{
_filelogger.LogError(Utilities.LogMessage(this, $"The Satellite Assembly Folder For {culture} Does Not Exist"));
}
}
// insert module and theme dependencies at beginning of list
foreach (var assembly in assemblies)
{
foreach (var type in assembly.GetTypes().Where(item => item.GetInterfaces().Contains(typeof(IModule))))
{
var instance = Activator.CreateInstance(type) as IModule;
foreach (string name in instance.ModuleDefinition.Dependencies.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{ {
var instance = Activator.CreateInstance(type) as ITheme; var path = Path.Combine(binFolder, name + ".dll");
foreach (string name in instance.Theme.Dependencies.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) if (System.IO.File.Exists(path))
{ {
if (System.IO.File.Exists(Path.Combine(binFolder, name + ".dll"))) path = Path.GetFileName(AddFileDate(path));
{ if (!list.Contains(path)) list.Insert(0, path);
if (!list.Contains(name)) list.Insert(0, name); }
} else
else {
{ _filelogger.LogError(Utilities.LogMessage(this, $"Module {instance.ModuleDefinition.ModuleDefinitionName} Dependency {name}.dll Does Not Exist"));
_filelogger.LogError(Utilities.LogMessage(this, $"Theme {instance.Theme.ThemeName} Dependency {name}.dll Does Not Exist"));
}
} }
} }
} }
foreach (var type in assembly.GetTypes().Where(item => item.GetInterfaces().Contains(typeof(ITheme))))
// create zip file containing assemblies and debug symbols
using (var memoryStream = new MemoryStream())
{ {
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) var instance = Activator.CreateInstance(type) as ITheme;
foreach (string name in instance.Theme.Dependencies.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{ {
foreach (string file in list) var path = Path.Combine(binFolder, name + ".dll");
if (System.IO.File.Exists(path))
{ {
using (var filestream = new FileStream(Path.Combine(binFolder, file + ".dll"), FileMode.Open, FileAccess.Read)) path = Path.GetFileName(AddFileDate(path));
using (var entrystream = archive.CreateEntry(file + ".dll").Open()) if (!list.Contains(path)) list.Insert(0, path);
}
else
{
_filelogger.LogError(Utilities.LogMessage(this, $"Theme {instance.Theme.ThemeName} Dependency {name}.dll Does Not Exist"));
}
}
}
}
return list;
}
private byte[] GetAssemblies(string list)
{
if (list == "*")
{
return _cache.GetOrCreate("assemblies", entry =>
{
return GetZIP(list);
});
}
else
{
return GetZIP(list);
}
}
private byte[] GetZIP(string list)
{
var binFolder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
// get list of assemblies which should be downloaded to client
List<string> assemblies;
if (list == "*")
{
assemblies = GetAssemblyList();
}
else
{
assemblies = list.Split(',').ToList();
}
// create zip file containing assemblies and debug symbols
using (var memoryStream = new MemoryStream())
{
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
foreach (string file in assemblies)
{
var filename = RemoveFileDate(file);
if (System.IO.File.Exists(Path.Combine(binFolder, filename)))
{
using (var filestream = new FileStream(Path.Combine(binFolder, filename), FileMode.Open, FileAccess.Read))
using (var entrystream = archive.CreateEntry(file).Open())
{ {
filestream.CopyTo(entrystream); filestream.CopyTo(entrystream);
} }
}
// include debug symbols filename = filename.Replace(".dll", ".pdb");
if (System.IO.File.Exists(Path.Combine(binFolder, file + ".pdb"))) if (System.IO.File.Exists(Path.Combine(binFolder, filename)))
{
using (var filestream = new FileStream(Path.Combine(binFolder, filename), FileMode.Open, FileAccess.Read))
using (var entrystream = archive.CreateEntry(file.Replace(".dll", ".pdb")).Open())
{ {
using (var filestream = new FileStream(Path.Combine(binFolder, file + ".pdb"), FileMode.Open, FileAccess.Read)) filestream.CopyTo(entrystream);
using (var entrystream = archive.CreateEntry(file + ".pdb").Open())
{
filestream.CopyTo(entrystream);
}
} }
} }
} }
return memoryStream.ToArray();
} }
});
return memoryStream.ToArray();
}
}
private string AddFileDate(string filepath)
{
DateTime lastwritetime = System.IO.File.GetLastWriteTime(filepath);
return Path.GetFileNameWithoutExtension(filepath) + "." + lastwritetime.ToString("yyyyMMddHHmmss") + Path.GetExtension(filepath);
}
private string RemoveFileDate(string filepath)
{
var segments = filepath.Split(".");
return string.Join(".", segments, 0, segments.Length - 2) + Path.GetExtension(filepath);
} }
private async Task RegisterContact(string email) private async Task RegisterContact(string email)