Skip to content

Commit d77e559

Browse files
authored
GH-45451: [C#] Integration with Grpc.Net.ClientFactory (#45458)
### 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 <[email protected]> Signed-off-by: Curt Hagenlocher <[email protected]>
1 parent ed2a417 commit d77e559

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

csharp/src/Apache.Arrow.Flight/Client/FlightClient.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public FlightClient(ChannelBase grpcChannel)
3333
_client = new FlightService.FlightServiceClient(grpcChannel);
3434
}
3535

36+
public FlightClient(CallInvoker callInvoker)
37+
{
38+
_client = new FlightService.FlightServiceClient(callInvoker);
39+
}
40+
3641
public AsyncServerStreamingCall<FlightInfo> ListFlights(FlightCriteria criteria = null, Metadata headers = null)
3742
{
3843
return ListFlights(criteria, headers, null, CancellationToken.None);

csharp/test/Apache.Arrow.Flight.Tests/Apache.Arrow.Flight.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9+
<PackageReference Include="Grpc.Net.ClientFactory" Version="2.67.0" />
910
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
1011
<PackageReference Include="xunit" Version="2.9.3" />
1112
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1" />

csharp/test/Apache.Arrow.Flight.Tests/FlightTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using Google.Protobuf;
2525
using Grpc.Core;
2626
using Grpc.Core.Utils;
27+
using Microsoft.Extensions.DependencyInjection;
2728
using Xunit;
2829

2930
namespace Apache.Arrow.Flight.Tests
@@ -546,7 +547,30 @@ public async Task EnsureCallRaisesRequestCancelled()
546547
var handshakeStreamingCall = _flightClient.Handshake(null, null, cts.Token);
547548
exception = await Assert.ThrowsAsync<RpcException>(async () => await handshakeStreamingCall.RequestStream.WriteAsync(new FlightHandshakeRequest(ByteString.Empty)));
548549
Assert.Equal(StatusCode.Cancelled, exception.StatusCode);
550+
}
551+
552+
[Fact]
553+
public async Task TestIntegrationWithGrpcNetClientFactory()
554+
{
555+
IServiceCollection services = new ServiceCollection();
549556

557+
services.AddGrpcClient<FlightClient>(grpc => grpc.Address = new Uri(_testWebFactory.GetAddress()));
558+
559+
IServiceProvider provider = services.BuildServiceProvider();
560+
561+
// Test that an instance of the FlightClient can be resolved whilst using the Grpc.Net.ClientFactory library.
562+
FlightClient flightClient = provider.GetRequiredService<FlightClient>();
563+
564+
// Test that the resolved client is functional.
565+
var flightDescriptor = FlightDescriptor.CreatePathDescriptor("test");
566+
var expectedBatch = CreateTestBatch(0, 100);
567+
var expectedSchema = expectedBatch.Schema;
568+
569+
GivenStoreBatches(flightDescriptor, new RecordBatchWithMetadata(expectedBatch));
570+
571+
var actualSchema = await flightClient.GetSchema(flightDescriptor);
572+
573+
SchemaComparer.Compare(expectedSchema, actualSchema);
550574
}
551575
}
552576
}

0 commit comments

Comments
 (0)