Skip to content

Commit c4e8b3b

Browse files
authored
Make serviceKey nullable for ImageGenerator and ISpeechToTextClient (dotnet#6807)
* Make serviceKey nullable for ImageGenerator Aligned with AddKeyedEmbeddingGenerator and AddKeyedChatClient. * Remove null check * Add test * Same for SpeechToTextClient
1 parent d299e16 commit c4e8b3b

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

src/Libraries/Microsoft.Extensions.AI/Image/ImageGeneratorBuilderServiceCollectionExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static ImageGeneratorBuilder AddImageGenerator(
5555
/// <remarks>The generator is registered as a scoped service.</remarks>
5656
public static ImageGeneratorBuilder AddKeyedImageGenerator(
5757
this IServiceCollection serviceCollection,
58-
object serviceKey,
58+
object? serviceKey,
5959
IImageGenerator innerGenerator,
6060
ServiceLifetime lifetime = ServiceLifetime.Singleton)
6161
=> AddKeyedImageGenerator(serviceCollection, serviceKey, _ => innerGenerator, lifetime);
@@ -70,12 +70,11 @@ public static ImageGeneratorBuilder AddKeyedImageGenerator(
7070
/// <remarks>The generator is registered as a scoped service.</remarks>
7171
public static ImageGeneratorBuilder AddKeyedImageGenerator(
7272
this IServiceCollection serviceCollection,
73-
object serviceKey,
73+
object? serviceKey,
7474
Func<IServiceProvider, IImageGenerator> innerGeneratorFactory,
7575
ServiceLifetime lifetime = ServiceLifetime.Singleton)
7676
{
7777
_ = Throw.IfNull(serviceCollection);
78-
_ = Throw.IfNull(serviceKey);
7978
_ = Throw.IfNull(innerGeneratorFactory);
8079

8180
var builder = new ImageGeneratorBuilder(innerGeneratorFactory);

src/Libraries/Microsoft.Extensions.AI/SpeechToText/SpeechToTextClientBuilderServiceCollectionExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static SpeechToTextClientBuilder AddSpeechToTextClient(
5252
/// <remarks>The client is registered as a scoped service.</remarks>
5353
public static SpeechToTextClientBuilder AddKeyedSpeechToTextClient(
5454
this IServiceCollection serviceCollection,
55-
object serviceKey,
55+
object? serviceKey,
5656
ISpeechToTextClient innerClient,
5757
ServiceLifetime lifetime = ServiceLifetime.Singleton)
5858
=> AddKeyedSpeechToTextClient(serviceCollection, serviceKey, _ => innerClient, lifetime);
@@ -66,12 +66,11 @@ public static SpeechToTextClientBuilder AddKeyedSpeechToTextClient(
6666
/// <remarks>The client is registered as a scoped service.</remarks>
6767
public static SpeechToTextClientBuilder AddKeyedSpeechToTextClient(
6868
this IServiceCollection serviceCollection,
69-
object serviceKey,
69+
object? serviceKey,
7070
Func<IServiceProvider, ISpeechToTextClient> innerClientFactory,
7171
ServiceLifetime lifetime = ServiceLifetime.Singleton)
7272
{
7373
_ = Throw.IfNull(serviceCollection);
74-
_ = Throw.IfNull(serviceKey);
7574
_ = Throw.IfNull(innerClientFactory);
7675

7776
var builder = new SpeechToTextClientBuilder(innerClientFactory);

test/Libraries/Microsoft.Extensions.AI.Tests/Image/ImageGeneratorDependencyInjectionPatterns.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,22 @@ public void AddKeyedImageGenerator_RegistersExpectedLifetime(ServiceLifetime? li
154154
Assert.Equal(expectedLifetime, sd.Lifetime);
155155
}
156156

157+
[Fact]
158+
public void AddKeyedImageGenerator_WorksWithNullServiceKey()
159+
{
160+
ServiceCollection sc = new();
161+
sc.AddKeyedImageGenerator(null, _ => new TestImageGenerator());
162+
163+
ServiceDescriptor sd = Assert.Single(sc);
164+
Assert.Equal(typeof(IImageGenerator), sd.ServiceType);
165+
Assert.False(sd.IsKeyedService);
166+
Assert.Null(sd.ServiceKey);
167+
Assert.Null(sd.ImplementationInstance);
168+
Assert.NotNull(sd.ImplementationFactory);
169+
Assert.IsType<TestImageGenerator>(sd.ImplementationFactory(null!));
170+
Assert.Equal(ServiceLifetime.Singleton, sd.Lifetime);
171+
}
172+
157173
public class SingletonMiddleware(IImageGenerator inner, IServiceProvider services) : DelegatingImageGenerator(inner)
158174
{
159175
public new IImageGenerator InnerGenerator => base.InnerGenerator;

test/Libraries/Microsoft.Extensions.AI.Tests/SpeechToText/SpeechToTextClientDependencyInjectionPatterns.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,22 @@ public void AddKeyedSpeechToTextClient_RegistersExpectedLifetime(ServiceLifetime
154154
Assert.Equal(expectedLifetime, sd.Lifetime);
155155
}
156156

157+
[Fact]
158+
public void AddKeyedSpeechToTextClient_WorksWithNullServiceKey()
159+
{
160+
ServiceCollection sc = new();
161+
sc.AddKeyedSpeechToTextClient(null, _ => new TestSpeechToTextClient());
162+
163+
ServiceDescriptor sd = Assert.Single(sc);
164+
Assert.Equal(typeof(ISpeechToTextClient), sd.ServiceType);
165+
Assert.False(sd.IsKeyedService);
166+
Assert.Null(sd.ServiceKey);
167+
Assert.Null(sd.ImplementationInstance);
168+
Assert.NotNull(sd.ImplementationFactory);
169+
Assert.IsType<TestSpeechToTextClient>(sd.ImplementationFactory(null!));
170+
Assert.Equal(ServiceLifetime.Singleton, sd.Lifetime);
171+
}
172+
157173
public class SingletonMiddleware(ISpeechToTextClient inner, IServiceProvider services) : DelegatingSpeechToTextClient(inner)
158174
{
159175
public new ISpeechToTextClient InnerClient => base.InnerClient;

0 commit comments

Comments
 (0)