Skip to content

Commit 73c726b

Browse files
authored
Fix client factory extension method validation issues (#2159)
1 parent 697f349 commit 73c726b

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

src/Grpc.Net.ClientFactory/GrpcClientServiceExtensions.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#region Copyright notice and license
1+
#region Copyright notice and license
22

33
// Copyright 2019 The gRPC Authors
44
//
@@ -65,9 +65,7 @@ public static IHttpClientBuilder AddGrpcClient<
6565
throw new ArgumentNullException(nameof(services));
6666
}
6767

68-
var name = TypeNameHelper.GetTypeDisplayName(typeof(TClient), fullName: false);
69-
70-
return services.AddGrpcClientCore<TClient>(name);
68+
return services.AddGrpcClient<TClient>(o => { });
7169
}
7270

7371
/// <summary>
@@ -183,7 +181,7 @@ public static IHttpClientBuilder AddGrpcClient<
183181
throw new ArgumentNullException(nameof(name));
184182
}
185183

186-
return services.AddGrpcClientCore<TClient>(name);
184+
return services.AddGrpcClient<TClient>(name, o => { });
187185
}
188186

189187
/// <summary>

src/Grpc.Net.ClientFactory/GrpcHttpClientBuilderExtensions.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#region Copyright notice and license
1+
#region Copyright notice and license
22

33
// Copyright 2019 The gRPC Authors
44
//
@@ -47,7 +47,7 @@ public static IHttpClientBuilder ConfigureChannel(this IHttpClientBuilder builde
4747
throw new ArgumentNullException(nameof(configureChannel));
4848
}
4949

50-
ValidateGrpcClient(builder);
50+
ValidateGrpcClient(builder, nameof(ConfigureChannel));
5151

5252
builder.Services.AddTransient<IConfigureOptions<GrpcClientFactoryOptions>>(services =>
5353
{
@@ -78,7 +78,7 @@ public static IHttpClientBuilder ConfigureChannel(this IHttpClientBuilder builde
7878
throw new ArgumentNullException(nameof(configureChannel));
7979
}
8080

81-
ValidateGrpcClient(builder);
81+
ValidateGrpcClient(builder, nameof(ConfigureChannel));
8282

8383
builder.Services.Configure<GrpcClientFactoryOptions>(builder.Name, options =>
8484
{
@@ -119,7 +119,7 @@ public static IHttpClientBuilder AddInterceptor(this IHttpClientBuilder builder,
119119
throw new ArgumentNullException(nameof(configureInvoker));
120120
}
121121

122-
ValidateGrpcClient(builder);
122+
ValidateGrpcClient(builder, nameof(AddInterceptor));
123123

124124
builder.Services.Configure<GrpcClientFactoryOptions>(builder.Name, options =>
125125
{
@@ -147,7 +147,7 @@ public static IHttpClientBuilder AddCallCredentials(this IHttpClientBuilder buil
147147
throw new ArgumentNullException(nameof(authInterceptor));
148148
}
149149

150-
ValidateGrpcClient(builder);
150+
ValidateGrpcClient(builder, nameof(AddCallCredentials));
151151

152152
builder.Services.Configure<GrpcClientFactoryOptions>(builder.Name, options =>
153153
{
@@ -181,7 +181,7 @@ public static IHttpClientBuilder AddCallCredentials(this IHttpClientBuilder buil
181181
throw new ArgumentNullException(nameof(authInterceptor));
182182
}
183183

184-
ValidateGrpcClient(builder);
184+
ValidateGrpcClient(builder, nameof(AddCallCredentials));
185185

186186
builder.Services.Configure<GrpcClientFactoryOptions>(builder.Name, options =>
187187
{
@@ -215,7 +215,7 @@ public static IHttpClientBuilder AddCallCredentials(this IHttpClientBuilder buil
215215
throw new ArgumentNullException(nameof(credentials));
216216
}
217217

218-
ValidateGrpcClient(builder);
218+
ValidateGrpcClient(builder, nameof(AddCallCredentials));
219219

220220
builder.Services.Configure<GrpcClientFactoryOptions>(builder.Name, options =>
221221
{
@@ -270,7 +270,7 @@ public static IHttpClientBuilder AddInterceptor(this IHttpClientBuilder builder,
270270
throw new ArgumentNullException(nameof(configureInvoker));
271271
}
272272

273-
ValidateGrpcClient(builder);
273+
ValidateGrpcClient(builder, nameof(AddInterceptor));
274274

275275
builder.Services.Configure<GrpcClientFactoryOptions>(builder.Name, options =>
276276
{
@@ -308,7 +308,7 @@ public static IHttpClientBuilder AddInterceptor<TInterceptor>(this IHttpClientBu
308308
throw new ArgumentNullException(nameof(builder));
309309
}
310310

311-
ValidateGrpcClient(builder);
311+
ValidateGrpcClient(builder, nameof(AddInterceptor));
312312

313313
builder.AddInterceptor(scope, serviceProvider =>
314314
{
@@ -337,7 +337,7 @@ public static IHttpClientBuilder ConfigureGrpcClientCreator(this IHttpClientBuil
337337
throw new ArgumentNullException(nameof(configureCreator));
338338
}
339339

340-
ValidateGrpcClient(builder);
340+
ValidateGrpcClient(builder, nameof(ConfigureGrpcClientCreator));
341341

342342
builder.Services.AddTransient<IConfigureOptions<GrpcClientFactoryOptions>>(services =>
343343
{
@@ -369,7 +369,7 @@ public static IHttpClientBuilder ConfigureGrpcClientCreator(this IHttpClientBuil
369369
throw new ArgumentNullException(nameof(configureCreator));
370370
}
371371

372-
ValidateGrpcClient(builder);
372+
ValidateGrpcClient(builder, nameof(ConfigureGrpcClientCreator));
373373

374374
builder.Services.Configure<GrpcClientFactoryOptions>(builder.Name, options =>
375375
{
@@ -379,7 +379,7 @@ public static IHttpClientBuilder ConfigureGrpcClientCreator(this IHttpClientBuil
379379
return builder;
380380
}
381381

382-
private static void ValidateGrpcClient(IHttpClientBuilder builder)
382+
private static void ValidateGrpcClient(IHttpClientBuilder builder, string caller)
383383
{
384384
// Validate the builder is for a gRPC client
385385
foreach (var service in builder.Services)
@@ -395,6 +395,6 @@ private static void ValidateGrpcClient(IHttpClientBuilder builder)
395395
}
396396
}
397397

398-
throw new InvalidOperationException($"{nameof(AddInterceptor)} must be used with a gRPC client.");
398+
throw new InvalidOperationException($"{caller} must be used with a gRPC client.");
399399
}
400400
}

test/Grpc.Net.ClientFactory.Tests/GrpcHttpClientBuilderExtensionsTests.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#region Copyright notice and license
1+
#region Copyright notice and license
22

33
// Copyright 2019 The gRPC Authors
44
//
@@ -137,7 +137,6 @@ public void AddInterceptor_NotFromGrpcClientFactoryAndExistingGrpcClient_ThrowEr
137137
{
138138
// Arrange
139139
var services = new ServiceCollection();
140-
services.AddGrpcClient<Greeter.GreeterClient>();
141140
var client = services.AddHttpClient("TestClient");
142141

143142
var ex = Assert.Throws<InvalidOperationException>(() => client.AddInterceptor(() => new CallbackInterceptor(o => { })))!;
@@ -147,6 +146,28 @@ public void AddInterceptor_NotFromGrpcClientFactoryAndExistingGrpcClient_ThrowEr
147146
Assert.AreEqual("AddInterceptor must be used with a gRPC client.", ex.Message);
148147
}
149148

149+
[Test]
150+
public void AddInterceptor_AddGrpcClientWithoutConfig_NoError()
151+
{
152+
// Arrange
153+
var services = new ServiceCollection();
154+
var client = services.AddGrpcClient<Greeter.GreeterClient>();
155+
156+
// Act
157+
client.AddInterceptor(() => new CallbackInterceptor(o => { }));
158+
}
159+
160+
[Test]
161+
public void AddInterceptor_AddGrpcClientWithNameAndWithoutConfig_NoError()
162+
{
163+
// Arrange
164+
var services = new ServiceCollection();
165+
var client = services.AddGrpcClient<Greeter.GreeterClient>(nameof(Greeter.GreeterClient));
166+
167+
// Act
168+
client.AddInterceptor(() => new CallbackInterceptor(o => { }));
169+
}
170+
150171
[Test]
151172
public void AddInterceptor_NotFromGrpcClientFactory_ThrowError()
152173
{

0 commit comments

Comments
 (0)