optimize assembly loading performance
This commit is contained in:
parent
e92d34a9e3
commit
a152d8664d
|
@ -106,167 +106,154 @@ namespace Oqtane.Controllers
|
||||||
// GET api/<controller>/list
|
// GET api/<controller>/list
|
||||||
[HttpGet("list")]
|
[HttpGet("list")]
|
||||||
public List<string> List()
|
public List<string> List()
|
||||||
|
{
|
||||||
|
return GetAssemblyList().Select(item => item.HashedName).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<ClientAssembly> GetAssemblyList()
|
||||||
{
|
{
|
||||||
var alias = _tenantManager.GetAlias();
|
var alias = _tenantManager.GetAlias();
|
||||||
var site = _sites.GetSite(alias.SiteId);
|
|
||||||
if (site != null && site.Runtime == "WebAssembly")
|
return _cache.GetOrCreate($"assemblieslist:{alias.SiteKey}", entry =>
|
||||||
{
|
{
|
||||||
return GetAssemblyList().Select(item => item.HashedName).ToList();
|
var assemblyList = new List<ClientAssembly>();
|
||||||
}
|
|
||||||
else
|
var site = _sites.GetSite(alias.SiteId);
|
||||||
{
|
if (site != null && site.Runtime == "WebAssembly")
|
||||||
return new List<string>();
|
{
|
||||||
}
|
var binFolder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
|
||||||
|
|
||||||
|
// testmode setting is used for validating that the API is downloading the appropriate assemblies to the client
|
||||||
|
bool hashfilename = true;
|
||||||
|
if (_configManager.GetSetting($"{SettingKeys.TestModeKey}", "false") == "true")
|
||||||
|
{
|
||||||
|
hashfilename = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get site assemblies which should be downloaded to client
|
||||||
|
var assemblies = _serverState.GetServerState(alias.SiteKey).Assemblies;
|
||||||
|
|
||||||
|
// populate assembly list
|
||||||
|
foreach (var assembly in assemblies)
|
||||||
|
{
|
||||||
|
if (assembly != Constants.ClientId)
|
||||||
|
{
|
||||||
|
var filepath = Path.Combine(binFolder, assembly) + ".dll";
|
||||||
|
if (System.IO.File.Exists(filepath))
|
||||||
|
{
|
||||||
|
assemblyList.Add(new ClientAssembly(Path.Combine(binFolder, assembly + ".dll"), hashfilename));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// insert satellite assemblies at beginning of list
|
||||||
|
foreach (var culture in _localizationManager.GetInstalledCultures())
|
||||||
|
{
|
||||||
|
if (culture != Constants.DefaultCulture)
|
||||||
|
{
|
||||||
|
var assembliesFolderPath = Path.Combine(binFolder, culture);
|
||||||
|
if (Directory.Exists(assembliesFolderPath))
|
||||||
|
{
|
||||||
|
foreach (var assembly in assemblies)
|
||||||
|
{
|
||||||
|
var filepath = Path.Combine(assembliesFolderPath, assembly) + ".resources.dll";
|
||||||
|
if (System.IO.File.Exists(filepath))
|
||||||
|
{
|
||||||
|
assemblyList.Insert(0, new ClientAssembly(Path.Combine(assembliesFolderPath, assembly + ".resources.dll"), hashfilename));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_filelogger.LogError(Utilities.LogMessage(this, $"The Satellite Assembly Folder For {culture} Does Not Exist"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return assemblyList;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET api/<controller>/load?list=x,y
|
// GET api/<controller>/load?list=x,y
|
||||||
[HttpGet("load")]
|
[HttpGet("load")]
|
||||||
public IActionResult Load(string list = "*")
|
public IActionResult Load(string list = "*")
|
||||||
{
|
{
|
||||||
var alias = _tenantManager.GetAlias();
|
return File(GetAssemblies(list), System.Net.Mime.MediaTypeNames.Application.Octet, "oqtane.dll");
|
||||||
var site = _sites.GetSite(alias.SiteId);
|
|
||||||
if (site != null && site.Runtime == "WebAssembly")
|
|
||||||
{
|
|
||||||
return File(GetAssemblies(list), System.Net.Mime.MediaTypeNames.Application.Octet, "oqtane.dll");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return File(GetEmptyZip(), System.Net.Mime.MediaTypeNames.Application.Octet, "oqtane.dll");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<ClientAssembly> GetAssemblyList()
|
|
||||||
{
|
|
||||||
var siteKey = _tenantManager.GetAlias().SiteKey;
|
|
||||||
|
|
||||||
return _cache.GetOrCreate($"assemblieslist:{siteKey}", entry =>
|
|
||||||
{
|
|
||||||
var binFolder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
|
|
||||||
var assemblyList = new List<ClientAssembly>();
|
|
||||||
|
|
||||||
// testmode setting is used for validating that the API is downloading the appropriate assemblies to the client
|
|
||||||
bool hashfilename = true;
|
|
||||||
if (_configManager.GetSetting($"{SettingKeys.TestModeKey}", "false") == "true")
|
|
||||||
{
|
|
||||||
hashfilename = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// get site assemblies which should be downloaded to client
|
|
||||||
var assemblies = _serverState.GetServerState(siteKey).Assemblies;
|
|
||||||
|
|
||||||
// populate assembly list
|
|
||||||
foreach (var assembly in assemblies)
|
|
||||||
{
|
|
||||||
if (assembly != Constants.ClientId)
|
|
||||||
{
|
|
||||||
var filepath = Path.Combine(binFolder, assembly) + ".dll";
|
|
||||||
if (System.IO.File.Exists(filepath))
|
|
||||||
{
|
|
||||||
assemblyList.Add(new ClientAssembly(Path.Combine(binFolder, assembly + ".dll"), hashfilename));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// insert satellite assemblies at beginning of list
|
|
||||||
foreach (var culture in _localizationManager.GetInstalledCultures())
|
|
||||||
{
|
|
||||||
if (culture != Constants.DefaultCulture)
|
|
||||||
{
|
|
||||||
var assembliesFolderPath = Path.Combine(binFolder, culture);
|
|
||||||
if (Directory.Exists(assembliesFolderPath))
|
|
||||||
{
|
|
||||||
foreach (var assembly in assemblies)
|
|
||||||
{
|
|
||||||
var filepath = Path.Combine(assembliesFolderPath, assembly) + ".resources.dll";
|
|
||||||
if (System.IO.File.Exists(filepath))
|
|
||||||
{
|
|
||||||
assemblyList.Insert(0, new ClientAssembly(Path.Combine(assembliesFolderPath, assembly + ".resources.dll"), hashfilename));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_filelogger.LogError(Utilities.LogMessage(this, $"The Satellite Assembly Folder For {culture} Does Not Exist"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return assemblyList;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] GetAssemblies(string list)
|
private byte[] GetAssemblies(string list)
|
||||||
{
|
{
|
||||||
var siteKey = _tenantManager.GetAlias().SiteKey;
|
var alias = _tenantManager.GetAlias();
|
||||||
|
|
||||||
if (list == "*")
|
if (list == "*")
|
||||||
{
|
{
|
||||||
return _cache.GetOrCreate($"assemblies:{siteKey}", entry =>
|
return _cache.GetOrCreate($"assemblies:{alias.SiteKey}", entry =>
|
||||||
{
|
{
|
||||||
return GetZIP(list);
|
return GetZIP(list, alias);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return GetZIP(list);
|
return GetZIP(list, alias);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] GetZIP(string list)
|
private byte[] GetZIP(string list, Alias alias)
|
||||||
{
|
{
|
||||||
var binFolder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
|
var site = _sites.GetSite(alias.SiteId);
|
||||||
|
if (site != null && site.Runtime == "WebAssembly")
|
||||||
// get list of assemblies which should be downloaded to client
|
|
||||||
List<ClientAssembly> assemblies = GetAssemblyList();
|
|
||||||
if (list != "*")
|
|
||||||
{
|
{
|
||||||
var filter = list.Split(',').ToList();
|
var binFolder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
|
||||||
assemblies.RemoveAll(item => !filter.Contains(item.HashedName));
|
|
||||||
}
|
|
||||||
|
|
||||||
// create zip file containing assemblies and debug symbols
|
// get list of assemblies which should be downloaded to client
|
||||||
using (var memoryStream = new MemoryStream())
|
List<ClientAssembly> assemblies = GetAssemblyList();
|
||||||
{
|
if (list != "*")
|
||||||
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
|
|
||||||
{
|
{
|
||||||
foreach (var assembly in assemblies)
|
var filter = list.Split(',').ToList();
|
||||||
|
assemblies.RemoveAll(item => !filter.Contains(item.HashedName));
|
||||||
|
}
|
||||||
|
|
||||||
|
// create zip file containing assemblies and debug symbols
|
||||||
|
using (var memoryStream = new MemoryStream())
|
||||||
|
{
|
||||||
|
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
|
||||||
{
|
{
|
||||||
if (Path.GetFileNameWithoutExtension(assembly.FilePath) != Constants.ClientId)
|
foreach (var assembly in assemblies)
|
||||||
{
|
{
|
||||||
if (System.IO.File.Exists(assembly.FilePath))
|
if (Path.GetFileNameWithoutExtension(assembly.FilePath) != Constants.ClientId)
|
||||||
{
|
{
|
||||||
using (var filestream = new FileStream(assembly.FilePath, FileMode.Open, FileAccess.Read))
|
if (System.IO.File.Exists(assembly.FilePath))
|
||||||
using (var entrystream = archive.CreateEntry(assembly.HashedName).Open())
|
|
||||||
{
|
{
|
||||||
filestream.CopyTo(entrystream);
|
using (var filestream = new FileStream(assembly.FilePath, FileMode.Open, FileAccess.Read))
|
||||||
|
using (var entrystream = archive.CreateEntry(assembly.HashedName).Open())
|
||||||
|
{
|
||||||
|
filestream.CopyTo(entrystream);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
var pdb = assembly.FilePath.Replace(".dll", ".pdb");
|
||||||
var pdb = assembly.FilePath.Replace(".dll", ".pdb");
|
if (System.IO.File.Exists(pdb))
|
||||||
if (System.IO.File.Exists(pdb))
|
|
||||||
{
|
|
||||||
using (var filestream = new FileStream(pdb, FileMode.Open, FileAccess.Read))
|
|
||||||
using (var entrystream = archive.CreateEntry(assembly.HashedName.Replace(".dll", ".pdb")).Open())
|
|
||||||
{
|
{
|
||||||
filestream.CopyTo(entrystream);
|
using (var filestream = new FileStream(pdb, FileMode.Open, FileAccess.Read))
|
||||||
|
using (var entrystream = archive.CreateEntry(assembly.HashedName.Replace(".dll", ".pdb")).Open())
|
||||||
|
{
|
||||||
|
filestream.CopyTo(entrystream);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return memoryStream.ToArray();
|
return memoryStream.ToArray();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
else
|
||||||
|
|
||||||
private byte[] GetEmptyZip()
|
|
||||||
{
|
|
||||||
using (var stream = new MemoryStream())
|
|
||||||
{
|
{
|
||||||
using (var zip = new ZipArchive(stream, ZipArchiveMode.Create))
|
// return empty zip
|
||||||
|
using (var memoryStream = new MemoryStream())
|
||||||
{
|
{
|
||||||
|
using (var zip = new ZipArchive(memoryStream, ZipArchiveMode.Create)) {}
|
||||||
|
return memoryStream.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
return stream.ToArray();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user