-
Notifications
You must be signed in to change notification settings - Fork 855
Expand file tree
/
Copy pathPlatformStartupDiscovery.cs
More file actions
124 lines (111 loc) · 5.44 KB
/
PlatformStartupDiscovery.cs
File metadata and controls
124 lines (111 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using VirtoCommerce.Platform.Core.Modularity;
namespace VirtoCommerce.Platform.Modules
{
/// <summary>
/// Discovers and instantiates <see cref="IPlatformStartup"/> implementations from module assemblies.
/// Designed to run early in the platform lifecycle (Program.cs level) before DI is configured.
/// </summary>
public static class PlatformStartupDiscovery
{
/// <summary>
/// Scans module manifests in the discovery path for those declaring a startupType,
/// loads their assemblies from the probing path, and instantiates the startup types.
/// Returns an empty list if paths are missing or no startup types are found.
/// </summary>
/// <param name="discoveryPath">Path to scan for module.manifest files (e.g., "modules/").</param>
/// <param name="probingPath">Path where module assemblies are located (e.g., "app_data/modules/").</param>
/// <returns>List of discovered startup instances ordered by <see cref="IPlatformStartup.Priority"/>.</returns>
public static IReadOnlyList<IPlatformStartup> DiscoverStartups(string discoveryPath, string probingPath)
{
var startups = new List<IPlatformStartup>();
if (string.IsNullOrEmpty(discoveryPath) || !Directory.Exists(discoveryPath))
{
return startups;
}
if (string.IsNullOrEmpty(probingPath) || !Directory.Exists(probingPath))
{
return startups;
}
// Register a permanent handler to resolve assemblies from the probing path.
// This is needed because startup assemblies are loaded into the default ALC
// before the full module loader (LoadContextAssemblyResolver) is configured,
// so their transitive dependencies won't be found otherwise.
// The handler must remain active because startup methods (ConfigureAppConfiguration,
// ConfigureHostServices, etc.) are invoked later during host building and may
// trigger assembly loads at that point.
var fullProbingPath = Path.GetFullPath(probingPath);
AssemblyLoadContext.Default.Resolving += (context, assemblyName) =>
{
var candidatePath = Path.Combine(fullProbingPath, assemblyName.Name + ".dll");
if (File.Exists(candidatePath))
{
return context.LoadFromAssemblyPath(candidatePath);
}
return null;
};
foreach (var manifestFile in Directory.EnumerateFiles(discoveryPath, "module.manifest", SearchOption.AllDirectories))
{
// Exclude manifests from built modules artifacts
if (manifestFile.Contains("artifacts"))
{
continue;
}
ModuleManifest manifest;
try
{
manifest = ManifestReader.Read(manifestFile);
}
catch (Exception ex)
{
Console.Error.WriteLine($"Warning: Failed to read module manifest {manifestFile}: {ex.Message}");
continue;
}
if (string.IsNullOrEmpty(manifest?.StartupType) || string.IsNullOrEmpty(manifest.AssemblyFile))
{
continue;
}
var assemblyPath = Path.GetFullPath(Path.Combine(probingPath, manifest.AssemblyFile));
if (!File.Exists(assemblyPath))
{
// Assembly not yet in probing path (first run). Skip gracefully.
continue;
}
try
{
var assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath);
var startupType = assembly.GetType(manifest.StartupType);
if (startupType == null)
{
// Try partial match (same pattern as ModuleInitializer.TryResolveModuleTypeFromAssembly)
startupType = assembly.GetTypes()
.FirstOrDefault(t => typeof(IPlatformStartup).IsAssignableFrom(t)
&& t.AssemblyQualifiedName?.StartsWith(manifest.StartupType) == true);
}
if (startupType != null
&& typeof(IPlatformStartup).IsAssignableFrom(startupType)
&& Activator.CreateInstance(startupType) is IPlatformStartup startup)
{
startups.Add(startup);
}
else
{
Console.Error.WriteLine(
$"Warning: Startup type '{manifest.StartupType}' in module '{manifest.Id}' does not implement IPlatformStartup or could not be instantiated.");
}
}
catch (Exception ex)
{
Console.Error.WriteLine(
$"Warning: Failed to load startup type '{manifest.StartupType}' from '{manifest.AssemblyFile}' (module '{manifest.Id}'): {ex.Message}");
}
}
return startups.OrderBy(s => s.Priority).ToList();
}
}
}