Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/DependencyInjection.Attributed.Tests/GenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FromTransientKeyedDependency>("FromKeyedTransient");
var second = scope.ServiceProvider.GetRequiredKeyedService<FromTransientKeyedDependency>("FromKeyedTransient");

// Within the scope, we get the same instance
Assert.NotSame(first, second);
}

[Fact]
public void ResolvesKeyedDependencyForNonKeyed()
Expand Down Expand Up @@ -301,6 +316,12 @@ public class FromKeyedDependency([FromKeyedServices(42)] IFormattable dependency
public IFormattable Dependency => dependency;
}

[Service<string>("FromKeyedTransient", ServiceLifetime.Transient)]
public class FromTransientKeyedDependency([FromKeyedServices(42)] IFormattable dependency)
{
public IFormattable Dependency => dependency;
}

[Service]
public class NonKeyedWithKeyedDependency([FromKeyedServices(PlatformID.Win32NT)] ICloneable dependency)
{
Expand Down
6 changes: 3 additions & 3 deletions src/DependencyInjection.Attributed/IncrementalGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ void AddKeyedServices(IEnumerable<KeyedService> services, Compilation compilatio
}

output.AppendLine($" services.AddKeyedTransient<Func<{impl}>>({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)
{
Expand All @@ -323,7 +323,7 @@ void AddKeyedServices(IEnumerable<KeyedService> services, Compilation compilatio
{
output.AppendLine($" services.{methodName}<{ifaceName}>({key}, (s, k) => s.GetRequiredKeyedService<{impl}>(k));");
output.AppendLine($" services.AddKeyedTransient<Func<{ifaceName}>>({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);
}

Expand Down Expand Up @@ -351,7 +351,7 @@ void AddKeyedServices(IEnumerable<KeyedService> services, Compilation compilatio
{
output.AppendLine($" services.{methodName}<{candidate}>({key}, (s, k) => s.GetRequiredKeyedService<{impl}>(k));");
output.AppendLine($" services.AddKeyedTransient<Func<{candidate}>>({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);
}
}
Expand Down