diff --git a/src/DependencyInjection.Attributed.Tests/GenerationTests.cs b/src/DependencyInjection.Attributed.Tests/GenerationTests.cs index 68b4453..9273fe6 100644 --- a/src/DependencyInjection.Attributed.Tests/GenerationTests.cs +++ b/src/DependencyInjection.Attributed.Tests/GenerationTests.cs @@ -150,6 +150,21 @@ public void ResolvesKeyedDependency() Assert.Same(singleton, instance.Dependency); } + [Fact] + public void ResolvesKeyedTransientDependency() + { + var collection = new ServiceCollection(); + collection.AddServices(); + var services = collection.BuildServiceProvider(); + + using var scope = services.CreateScope(); + + var first = scope.ServiceProvider.GetRequiredKeyedService("FromKeyedTransient"); + var second = scope.ServiceProvider.GetRequiredKeyedService("FromKeyedTransient"); + + // Within the scope, we get the same instance + Assert.NotSame(first, second); + } [Fact] public void ResolvesKeyedDependencyForNonKeyed() @@ -301,6 +316,12 @@ public class FromKeyedDependency([FromKeyedServices(42)] IFormattable dependency public IFormattable Dependency => dependency; } +[Service("FromKeyedTransient", ServiceLifetime.Transient)] +public class FromTransientKeyedDependency([FromKeyedServices(42)] IFormattable dependency) +{ + public IFormattable Dependency => dependency; +} + [Service] public class NonKeyedWithKeyedDependency([FromKeyedServices(PlatformID.Win32NT)] ICloneable dependency) { diff --git a/src/DependencyInjection.Attributed/IncrementalGenerator.cs b/src/DependencyInjection.Attributed/IncrementalGenerator.cs index 92c304c..944b181 100644 --- a/src/DependencyInjection.Attributed/IncrementalGenerator.cs +++ b/src/DependencyInjection.Attributed/IncrementalGenerator.cs @@ -314,7 +314,7 @@ void AddKeyedServices(IEnumerable services, Compilation compilatio } output.AppendLine($" services.AddKeyedTransient>({key}, (s, k) => () => s.GetRequiredKeyedService<{impl}>(k));"); - output.AppendLine($" services.AddKeyedTransient({key}, (s, k) => new Lazy<{impl}>(s.GetRequiredKeyedService<{impl}>(k)));"); + output.AppendLine($" services.AddKeyedTransient({key}, (s, k) => new Lazy<{impl}>(() => s.GetRequiredKeyedService<{impl}>(k)));"); foreach (var iface in type.Type.AllInterfaces) { @@ -323,7 +323,7 @@ void AddKeyedServices(IEnumerable services, Compilation compilatio { output.AppendLine($" services.{methodName}<{ifaceName}>({key}, (s, k) => s.GetRequiredKeyedService<{impl}>(k));"); output.AppendLine($" services.AddKeyedTransient>({key}, (s, k) => () => s.GetRequiredKeyedService<{ifaceName}>(k));"); - output.AppendLine($" services.AddKeyedTransient({key}, (s, k) => new Lazy<{ifaceName}>(s.GetRequiredKeyedService<{ifaceName}>(k)));"); + output.AppendLine($" services.AddKeyedTransient({key}, (s, k) => new Lazy<{ifaceName}>(() => s.GetRequiredKeyedService<{ifaceName}>(k)));"); registered.Add(ifaceName); } @@ -351,7 +351,7 @@ void AddKeyedServices(IEnumerable services, Compilation compilatio { output.AppendLine($" services.{methodName}<{candidate}>({key}, (s, k) => s.GetRequiredKeyedService<{impl}>(k));"); output.AppendLine($" services.AddKeyedTransient>({key}, (s, k) => () => s.GetRequiredKeyedService<{candidate}>(k));"); - output.AppendLine($" services.AddKeyedTransient({key}, (s, k) => new Lazy<{candidate}>(k, s.GetRequiredKeyedService<{candidate}>(k)));"); + output.AppendLine($" services.AddKeyedTransient({key}, (s, k) => new Lazy<{candidate}>(() => s.GetRequiredKeyedService<{candidate}>(k)));"); registered.Add(candidate); } }