Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Change gRPC client factory to log ConfigureHttpClient is unsupported …
…instead of an error
  • Loading branch information
JamesNK committed Jun 1, 2024
commit bc5f7968b09d04fef090ac0b07310cb5fe1b5046
15 changes: 14 additions & 1 deletion src/Grpc.Net.ClientFactory/Internal/GrpcCallInvokerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ internal class GrpcCallInvokerFactory
private readonly IServiceScopeFactory _scopeFactory;
private readonly ConcurrentDictionary<EntryKey, CallInvoker> _activeChannels;
private readonly Func<EntryKey, CallInvoker> _invokerFactory;
private readonly ILogger<GrpcCallInvokerFactory> _logger;

public GrpcCallInvokerFactory(
IServiceScopeFactory scopeFactory,
Expand All @@ -57,6 +58,7 @@ public GrpcCallInvokerFactory(
_scopeFactory = scopeFactory;
_activeChannels = new ConcurrentDictionary<EntryKey, CallInvoker>();
_invokerFactory = CreateInvoker;
_logger = _loggerFactory.CreateLogger<GrpcCallInvokerFactory>();
}

public CallInvoker CreateInvoker(string name, Type type)
Expand All @@ -75,7 +77,7 @@ private CallInvoker CreateInvoker(EntryKey key)
var httpClientFactoryOptions = _httpClientFactoryOptionsMonitor.Get(name);
if (httpClientFactoryOptions.HttpClientActions.Count > 0)
{
throw new InvalidOperationException($"The ConfigureHttpClient method is not supported when creating gRPC clients. Unable to create client with name '{name}'.");
Log.UnsupportedHttpClientActions(_logger, name);
}

var clientFactoryOptions = _grpcClientFactoryOptionsMonitor.Get(name);
Expand Down Expand Up @@ -190,4 +192,15 @@ public override void SetCompositeCredentials(object state, ChannelCredentials ch
{
}
}

private static class Log
{
private static readonly Action<ILogger, string, Exception?> _unsupportedHttpClientActions =
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(1, "UnsupportedHttpClientActions"), "The ConfigureHttpClient method is not supported when creating gRPC clients. Unable to apply configuration to client with the name '{ClientName}'.");

public static void UnsupportedHttpClientActions(ILogger<GrpcCallInvokerFactory> logger, string clientName)
{
_unsupportedHttpClientActions(logger, clientName, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,14 @@ public void CreateClient_NoAddress_ThrowError()
}

[Test]
public void CreateClient_ConfigureHttpClient_ThrowError()
public void CreateClient_ConfigureHttpClient_LogMessage()
{
// Arrange
var testSink = new TestSink();

var services = new ServiceCollection();
services.AddLogging(configure => configure.SetMinimumLevel(LogLevel.Trace));
services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, TestLoggerProvider>(s => new TestLoggerProvider(testSink, true)));
services
.AddGrpcClient<TestGreeterClient>()
.ConfigureHttpClient(options => options.BaseAddress = new Uri("http://contoso"))
Expand All @@ -164,7 +168,9 @@ public void CreateClient_ConfigureHttpClient_ThrowError()
var ex = Assert.Throws<InvalidOperationException>(() => clientFactory.CreateClient<TestGreeterClient>(nameof(TestGreeterClient)))!;

// Assert
Assert.AreEqual("The ConfigureHttpClient method is not supported when creating gRPC clients. Unable to create client with name 'TestGreeterClient'.", ex.Message);
Assert.AreEqual(@"Could not resolve the address for gRPC client 'TestGreeterClient'. Set an address when registering the client: services.AddGrpcClient<TestGreeterClient>(o => o.Address = new Uri(""https://localhost:5001""))", ex.Message);

Assert.IsTrue(testSink.Writes.Any(w => w.EventId.Name == "UnsupportedHttpClientActions"));
}

#if NET462
Expand Down