Skip to content

Commit

Permalink
GH-45451: [C#] Integration with Grpc.Net.ClientFactory (#45458)
Browse files Browse the repository at this point in the history
### Rationale for this change

See #45451. This adds out of the box compatibility with the [Grpc.Net.ClientFactory](https://learn.microsoft.com/en-us/aspnet/core/grpc/clientfactory?view=aspnetcore-9.0), library, which is fairly standardized for users making gRPC requests in .NET web applications.

### What changes are included in this PR?

Added a new constructor to `FlightClient` that accepts a `CallInvoker` instance.

`public FlightClient(CallInvoker callInvoker)`

### Are these changes tested?

Yes, added a unit test to resolve an instance of the `FlightClient` using the `Grpc.Net.ClientFactory` integration and made a request with it.

### Are there any user-facing changes?

Yes, a new overload constructor of `FlightClient` was added.

* GitHub Issue: #45451

Authored-by: Robert Cao <robert.cao@relativity.com>
Signed-off-by: Curt Hagenlocher <curt@hagenlocher.org>
  • Loading branch information
robcao authored Feb 8, 2025
1 parent ed2a417 commit d77e559
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions csharp/src/Apache.Arrow.Flight/Client/FlightClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public FlightClient(ChannelBase grpcChannel)
_client = new FlightService.FlightServiceClient(grpcChannel);
}

public FlightClient(CallInvoker callInvoker)
{
_client = new FlightService.FlightServiceClient(callInvoker);
}

public AsyncServerStreamingCall<FlightInfo> ListFlights(FlightCriteria criteria = null, Metadata headers = null)
{
return ListFlights(criteria, headers, null, CancellationToken.None);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Grpc.Net.ClientFactory" Version="2.67.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1" />
Expand Down
24 changes: 24 additions & 0 deletions csharp/test/Apache.Arrow.Flight.Tests/FlightTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using Google.Protobuf;
using Grpc.Core;
using Grpc.Core.Utils;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace Apache.Arrow.Flight.Tests
Expand Down Expand Up @@ -546,7 +547,30 @@ public async Task EnsureCallRaisesRequestCancelled()
var handshakeStreamingCall = _flightClient.Handshake(null, null, cts.Token);
exception = await Assert.ThrowsAsync<RpcException>(async () => await handshakeStreamingCall.RequestStream.WriteAsync(new FlightHandshakeRequest(ByteString.Empty)));
Assert.Equal(StatusCode.Cancelled, exception.StatusCode);
}

[Fact]
public async Task TestIntegrationWithGrpcNetClientFactory()
{
IServiceCollection services = new ServiceCollection();

services.AddGrpcClient<FlightClient>(grpc => grpc.Address = new Uri(_testWebFactory.GetAddress()));

IServiceProvider provider = services.BuildServiceProvider();

// Test that an instance of the FlightClient can be resolved whilst using the Grpc.Net.ClientFactory library.
FlightClient flightClient = provider.GetRequiredService<FlightClient>();

// Test that the resolved client is functional.
var flightDescriptor = FlightDescriptor.CreatePathDescriptor("test");
var expectedBatch = CreateTestBatch(0, 100);
var expectedSchema = expectedBatch.Schema;

GivenStoreBatches(flightDescriptor, new RecordBatchWithMetadata(expectedBatch));

var actualSchema = await flightClient.GetSchema(flightDescriptor);

SchemaComparer.Compare(expectedSchema, actualSchema);
}
}
}

0 comments on commit d77e559

Please sign in to comment.