diff --git a/test/Autofac.Specification.Test/LoadContextScopeTests.cs b/test/Autofac.Specification.Test/LoadContextScopeTests.cs
index e27f6f481..33dacac51 100644
--- a/test/Autofac.Specification.Test/LoadContextScopeTests.cs
+++ b/test/Autofac.Specification.Test/LoadContextScopeTests.cs
@@ -2,10 +2,12 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System.Collections;
+using System.Diagnostics.SymbolStore;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Loader;
+using Autofac.Core;
using Autofac.Features.ResolveAnything;
namespace Autofac.Specification.Test;
@@ -34,7 +36,7 @@ public void CanLoadInstanceOfScanAssemblyAndUnloadIt()
}
[Fact]
- public void CanLoadInstanceOfScanAssemblyAndUnloadItAfterActnars()
+ public void CanLoadInstanceOfAssemblyAndUnloadItAfterActnars()
{
var builder = new ContainerBuilder();
@@ -56,6 +58,72 @@ public void CanLoadInstanceOfScanAssemblyAndUnloadItAfterActnars()
WaitForUnload(loadContextRef);
}
+ [Fact]
+ public void CanLoadInstanceOfAssemblyAndUnloadItAfterOnActivatedInModule()
+ {
+ var builder = new ContainerBuilder();
+
+ using var rootContainer = builder.Build();
+
+ LoadAssemblyAndTest(
+ rootContainer,
+ out var loadContextRef,
+ (builder, assembly) =>
+ {
+ var module = (IModule)Activator.CreateInstance(assembly.GetType("A.OnActivatedModule"), 100);
+
+ builder.RegisterModule(module);
+ },
+ (scope, loadContext, assembly) =>
+ {
+ var serviceType = assembly.GetType("A.Service1");
+
+ var instance = scope.Resolve(serviceType);
+
+ Assert.Contains(instance.GetType().Assembly, loadContext.Assemblies);
+
+ var valueProp = serviceType.GetProperty("Value");
+
+ Assert.Equal(100, valueProp.GetValue(instance));
+ });
+
+ WaitForUnload(loadContextRef);
+ }
+
+ [Fact]
+ public void CanLoadInstanceOfAssemblyAndUnloadItAfterLifetimeScopeEndingInModule()
+ {
+ var builder = new ContainerBuilder();
+
+ using var rootContainer = builder.Build();
+
+ bool callbackInvoked = false;
+
+ LoadAssemblyAndTest(
+ rootContainer,
+ out var loadContextRef,
+ (builder, assembly) =>
+ {
+ Action invoke = () => { callbackInvoked = true; };
+
+ var module = (IModule)Activator.CreateInstance(assembly.GetType("A.LifetimeScopeEndingModule"), invoke);
+
+ builder.RegisterModule(module);
+ },
+ (scope, loadContext, assembly) =>
+ {
+ var serviceType = assembly.GetType("A.Service1");
+
+ var instance = scope.Resolve(serviceType);
+
+ Assert.Contains(instance.GetType().Assembly, loadContext.Assemblies);
+ });
+
+ WaitForUnload(loadContextRef);
+
+ Assert.True(callbackInvoked);
+ }
+
[Fact]
public void CanLoadInstanceOfScanAssemblyAndUnloadItAfterEnumerable()
{
diff --git a/test/Autofac.Test.Scenarios.LoadContext/Autofac.Test.Scenarios.LoadContext.csproj b/test/Autofac.Test.Scenarios.LoadContext/Autofac.Test.Scenarios.LoadContext.csproj
index 2bce1883e..37db0cdae 100644
--- a/test/Autofac.Test.Scenarios.LoadContext/Autofac.Test.Scenarios.LoadContext.csproj
+++ b/test/Autofac.Test.Scenarios.LoadContext/Autofac.Test.Scenarios.LoadContext.csproj
@@ -22,4 +22,8 @@
+
+
+
+
diff --git a/test/Autofac.Test.Scenarios.LoadContext/LifetimeScopeEndingModule.cs b/test/Autofac.Test.Scenarios.LoadContext/LifetimeScopeEndingModule.cs
new file mode 100644
index 000000000..f99c13359
--- /dev/null
+++ b/test/Autofac.Test.Scenarios.LoadContext/LifetimeScopeEndingModule.cs
@@ -0,0 +1,26 @@
+// Copyright (c) Autofac Project. All rights reserved.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+using Autofac;
+
+namespace A;
+
+public class LifetimeScopeEndingModule : Module
+{
+ private readonly Action _invokeOnEndCallback;
+
+ public LifetimeScopeEndingModule(Action invokeOnEndCallback)
+ {
+ _invokeOnEndCallback = invokeOnEndCallback;
+ }
+
+ protected override void Load(ContainerBuilder builder)
+ {
+ builder.RegisterType();
+
+ builder.RegisterBuildCallback(scope => scope.CurrentScopeEnding += (sender, ev) =>
+ {
+ _invokeOnEndCallback();
+ });
+ }
+}
diff --git a/test/Autofac.Test.Scenarios.LoadContext/OnActivatedModule.cs b/test/Autofac.Test.Scenarios.LoadContext/OnActivatedModule.cs
new file mode 100644
index 000000000..9231b67ef
--- /dev/null
+++ b/test/Autofac.Test.Scenarios.LoadContext/OnActivatedModule.cs
@@ -0,0 +1,21 @@
+// Copyright (c) Autofac Project. All rights reserved.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+using Autofac;
+
+namespace A;
+
+public class OnActivatedModule : Module
+{
+ private readonly int _value;
+
+ public OnActivatedModule(int value)
+ {
+ _value = value;
+ }
+
+ protected override void Load(ContainerBuilder builder)
+ {
+ builder.RegisterType().OnActivated(x => x.Instance.Value = _value);
+ }
+}
diff --git a/test/Autofac.Test.Scenarios.LoadContext/Service1.cs b/test/Autofac.Test.Scenarios.LoadContext/Service1.cs
index 47f6259db..ca5187b69 100644
--- a/test/Autofac.Test.Scenarios.LoadContext/Service1.cs
+++ b/test/Autofac.Test.Scenarios.LoadContext/Service1.cs
@@ -5,4 +5,5 @@ namespace A;
public class Service1
{
+ public int Value { get; set; }
}