Skip to content

Commit 1400948

Browse files
committed
fix(runtime): remove duplicate module instance creation
Package-loaded modules were instantiated twice: 1. In ModuleLoader.LoadAsync -> RuntimeModuleHandle.ModuleInstances 2. In ModulusApplicationFactory -> ModuleManager This caused double initialization (OnApplicationInitializationAsync called twice). Fix: Only register host startup module (TStartupModule) to ModuleManager. Package-loaded modules are now managed exclusively via RuntimeModuleHandle and initialized through IHostAwareModuleLoader.InitializeLoadedModulesAsync().
1 parent 06de676 commit 1400948

File tree

1 file changed

+4
-27
lines changed

1 file changed

+4
-27
lines changed

src/Modulus.Core/Runtime/ModulusApplicationFactory.cs

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -144,35 +144,12 @@ public static async Task<ModulusApplication> CreateAsync<TStartupModule>(
144144
}
145145
}
146146

147-
// 4. Register Loaded Modules to Manager
147+
// 4. Register Host Startup Module to Manager
148+
// Note: Package-loaded modules are managed via RuntimeModuleHandle.ModuleInstances
149+
// and initialized through IHostAwareModuleLoader.InitializeLoadedModulesAsync().
150+
// Only the host startup module goes through ModuleManager to avoid double initialization.
148151
moduleManager.AddModule(new TStartupModule());
149152

150-
foreach (var runtimeModule in runtimeContext.RuntimeModules)
151-
{
152-
foreach (var assembly in runtimeModule.LoadContext.Assemblies)
153-
{
154-
var moduleTypes = assembly.GetTypes()
155-
.Where(t => typeof(IModule).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface);
156-
157-
foreach (var type in moduleTypes)
158-
{
159-
if (type == typeof(TStartupModule)) continue;
160-
161-
try
162-
{
163-
var instance = (IModule)Activator.CreateInstance(type)!;
164-
// Let ModuleManager resolve the ID from [Module] attribute or type name
165-
// Pass package-level dependencies from manifest
166-
moduleManager.AddModule(instance, manifestDependencies: runtimeModule.Manifest.Dependencies.Keys);
167-
}
168-
catch (Exception ex)
169-
{
170-
logger.LogError(ex, "Failed to instantiate module {Type}", type.Name);
171-
}
172-
}
173-
}
174-
}
175-
176153
// 5. Register Services to FINAL ServiceCollection
177154
ModulusLogging.AddLoggerFactory(services, loggerFactory);
178155
services.AddSingleton(runtimeContext);

0 commit comments

Comments
 (0)