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
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static ImageGeneratorBuilder AddImageGenerator(
/// <remarks>The generator is registered as a scoped service.</remarks>
public static ImageGeneratorBuilder AddKeyedImageGenerator(
this IServiceCollection serviceCollection,
object serviceKey,
object? serviceKey,
IImageGenerator innerGenerator,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
=> AddKeyedImageGenerator(serviceCollection, serviceKey, _ => innerGenerator, lifetime);
Expand All @@ -70,12 +70,11 @@ public static ImageGeneratorBuilder AddKeyedImageGenerator(
/// <remarks>The generator is registered as a scoped service.</remarks>
public static ImageGeneratorBuilder AddKeyedImageGenerator(
this IServiceCollection serviceCollection,
object serviceKey,
object? serviceKey,
Func<IServiceProvider, IImageGenerator> innerGeneratorFactory,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
_ = Throw.IfNull(serviceCollection);
_ = Throw.IfNull(serviceKey);
_ = Throw.IfNull(innerGeneratorFactory);

var builder = new ImageGeneratorBuilder(innerGeneratorFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static SpeechToTextClientBuilder AddSpeechToTextClient(
/// <remarks>The client is registered as a scoped service.</remarks>
public static SpeechToTextClientBuilder AddKeyedSpeechToTextClient(
this IServiceCollection serviceCollection,
object serviceKey,
object? serviceKey,
ISpeechToTextClient innerClient,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
=> AddKeyedSpeechToTextClient(serviceCollection, serviceKey, _ => innerClient, lifetime);
Expand All @@ -66,12 +66,11 @@ public static SpeechToTextClientBuilder AddKeyedSpeechToTextClient(
/// <remarks>The client is registered as a scoped service.</remarks>
public static SpeechToTextClientBuilder AddKeyedSpeechToTextClient(
this IServiceCollection serviceCollection,
object serviceKey,
object? serviceKey,
Func<IServiceProvider, ISpeechToTextClient> innerClientFactory,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
_ = Throw.IfNull(serviceCollection);
_ = Throw.IfNull(serviceKey);
_ = Throw.IfNull(innerClientFactory);

var builder = new SpeechToTextClientBuilder(innerClientFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,22 @@ public void AddKeyedImageGenerator_RegistersExpectedLifetime(ServiceLifetime? li
Assert.Equal(expectedLifetime, sd.Lifetime);
}

[Fact]
public void AddKeyedImageGenerator_WorksWithNullServiceKey()
{
ServiceCollection sc = new();
sc.AddKeyedImageGenerator(null, _ => new TestImageGenerator());

ServiceDescriptor sd = Assert.Single(sc);
Assert.Equal(typeof(IImageGenerator), sd.ServiceType);
Assert.False(sd.IsKeyedService);
Assert.Null(sd.ServiceKey);
Assert.Null(sd.ImplementationInstance);
Assert.NotNull(sd.ImplementationFactory);
Assert.IsType<TestImageGenerator>(sd.ImplementationFactory(null!));
Assert.Equal(ServiceLifetime.Singleton, sd.Lifetime);
}

public class SingletonMiddleware(IImageGenerator inner, IServiceProvider services) : DelegatingImageGenerator(inner)
{
public new IImageGenerator InnerGenerator => base.InnerGenerator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,22 @@ public void AddKeyedSpeechToTextClient_RegistersExpectedLifetime(ServiceLifetime
Assert.Equal(expectedLifetime, sd.Lifetime);
}

[Fact]
public void AddKeyedSpeechToTextClient_WorksWithNullServiceKey()
{
ServiceCollection sc = new();
sc.AddKeyedSpeechToTextClient(null, _ => new TestSpeechToTextClient());

ServiceDescriptor sd = Assert.Single(sc);
Assert.Equal(typeof(ISpeechToTextClient), sd.ServiceType);
Assert.False(sd.IsKeyedService);
Assert.Null(sd.ServiceKey);
Assert.Null(sd.ImplementationInstance);
Assert.NotNull(sd.ImplementationFactory);
Assert.IsType<TestSpeechToTextClient>(sd.ImplementationFactory(null!));
Assert.Equal(ServiceLifetime.Singleton, sd.Lifetime);
}

public class SingletonMiddleware(ISpeechToTextClient inner, IServiceProvider services) : DelegatingSpeechToTextClient(inner)
{
public new ISpeechToTextClient InnerClient => base.InnerClient;
Expand Down
Loading