Skip to content

Commit

Permalink
feat: Select the first GrpcAdapter that supports the API
Browse files Browse the repository at this point in the history
  • Loading branch information
jskeet committed Apr 6, 2022
1 parent 4f9f337 commit cf2001e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public partial class ClientBuilderBaseTest
public class DependencyInjection
{
[Fact]
public void GrpcAdapter_NotSetBefore()
public void GrpcAdapter_NotSetBefore_SingleMatching()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddGrpcNetClientAdapter();
Expand All @@ -33,6 +33,27 @@ public void GrpcAdapter_NotSetBefore()
Assert.IsType<GrpcNetClientAdapter>(builder.GrpcAdapter);
}

[Fact]
public void GrpcAdapter_NotSetBefore_SingleNonMatching()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddRestGrpcAdapter();
var builder = new FakeBuilder();
builder.Configure(serviceCollection);
Assert.Null(builder.GrpcAdapter);
}

[Fact]
public void GrpcAdapter_NotSetBefore_MultipleFirstMatchUsed()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddRestGrpcAdapter();
serviceCollection.AddGrpcNetClientAdapter();
var builder = new FakeBuilder();
builder.Configure(serviceCollection);
Assert.IsType<GrpcNetClientAdapter>(builder.GrpcAdapter);
}

[Fact]
public void GrpcAdapter_SetBefore()
{
Expand Down
7 changes: 5 additions & 2 deletions Google.Api.Gax.Grpc/ClientBuilderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,10 @@ protected virtual GrpcChannelOptions GetChannelOptions()
/// </summary>
/// <remarks>
/// <para>
/// If gRPC adapters are configured in <paramref name="provider"/>, the first one that supports
/// the <see cref="ServiceMetadata"/> will be used.
/// </para>
/// <para>
/// Credentials are only requested from dependency injection if they are not already set
/// via any of <see cref="ChannelCredentials"/>, <see cref="CredentialsPath"/>,
/// <see cref="JsonCredentials"/>, <see cref="Credential"/>, <see cref="GoogleCredential"/>
Expand All @@ -539,8 +543,7 @@ protected virtual void Configure(IServiceProvider provider)
return;
}

// TODO: Check for the concrete adapters as well, based on what is supported?
GrpcAdapter ??= provider.GetService<GrpcAdapter>();
GrpcAdapter ??= provider.GetServices<GrpcAdapter>().FirstOrDefault(adapter => adapter.SupportsApi(ServiceMetadata));
GrpcChannelOptions ??= provider.GetService<GrpcChannelOptions>();

#pragma warning disable CS0618 // Type or member is obsolete
Expand Down

0 comments on commit cf2001e

Please sign in to comment.