diff --git a/src/Testing/CoreTests/Compilation/using_container_or_service_provider_in_handlers.cs b/src/Testing/CoreTests/Compilation/using_container_or_service_provider_in_handlers.cs index fbf5f598e..9c1a9d134 100644 --- a/src/Testing/CoreTests/Compilation/using_container_or_service_provider_in_handlers.cs +++ b/src/Testing/CoreTests/Compilation/using_container_or_service_provider_in_handlers.cs @@ -53,11 +53,39 @@ public async Task using_service_location_with_one_service() opts.Services.AddSingleton(new ColorContext("Red")); }); - + await Execute(new CSP5()); CSP5User.Flag.ShouldBeOfType(); } + + [Fact] + public async Task service_location_flag_does_not_leak_to_subsequent_handlers() + { + IfWolverineIsConfiguredAs(opts => + { + opts.IncludeType(typeof(CSP5User)); + opts.IncludeType(typeof(CSP6User)); + opts.CodeGeneration.AlwaysUseServiceLocationFor(); + + opts.Services.AddScoped(); + opts.Services.AddScoped(x => + { + var context = x.GetRequiredService(); + return context.Color.EqualsIgnoreCase("red") ? new RedFlag() : new GreenFlag(); + }); + + opts.Services.AddSingleton(new ColorContext("Red")); + }); + + // Execute first handler + await Execute(new CSP5()); + CSP5User.Flag.ShouldBeOfType(); + + // Execute second handler - should still work with constructor injection + await Execute(new CSP6()); + CSP6User.Gateway.ShouldNotBeNull(); + } } public class CSP3; @@ -117,4 +145,23 @@ public record RedFlag : IFlag; public record GreenFlag : IFlag; public interface IGateway; -public class Gateway : IGateway; \ No newline at end of file +public class Gateway : IGateway; + +public record CSP6; + +[WolverineIgnore] +public class CSP6User +{ + private readonly IGateway _gateway; + public static IGateway? Gateway { get; set; } + + public CSP6User(IGateway gateway) + { + _gateway = gateway; + } + + public void Handle(CSP6 message) + { + Gateway = _gateway; + } +} \ No newline at end of file diff --git a/src/Wolverine/Wolverine.csproj b/src/Wolverine/Wolverine.csproj index 803f9f1ac..1e4727d63 100644 --- a/src/Wolverine/Wolverine.csproj +++ b/src/Wolverine/Wolverine.csproj @@ -4,8 +4,8 @@ WolverineFx - - + +