This is an automated email from the ASF dual-hosted git repository.

curth pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/main by this push:
     new d77e5591d3 GH-45451: [C#] Integration with Grpc.Net.ClientFactory 
(#45458)
d77e5591d3 is described below

commit d77e5591d3c8abf6f82e00012b643c65d3a32ffb
Author: Robert Cao <[email protected]>
AuthorDate: Sat Feb 8 10:50:47 2025 -0600

    GH-45451: [C#] Integration with Grpc.Net.ClientFactory (#45458)
    
    
    
    ### Rationale for this change
    
    See https://github.com/apache/arrow/issues/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]>
---
 .../src/Apache.Arrow.Flight/Client/FlightClient.cs |  5 +++++
 .../Apache.Arrow.Flight.Tests.csproj               |  1 +
 .../test/Apache.Arrow.Flight.Tests/FlightTests.cs  | 24 ++++++++++++++++++++++
 3 files changed, 30 insertions(+)

diff --git a/csharp/src/Apache.Arrow.Flight/Client/FlightClient.cs 
b/csharp/src/Apache.Arrow.Flight/Client/FlightClient.cs
index 10660f40b4..c11b7e532d 100644
--- a/csharp/src/Apache.Arrow.Flight/Client/FlightClient.cs
+++ b/csharp/src/Apache.Arrow.Flight/Client/FlightClient.cs
@@ -33,6 +33,11 @@ namespace Apache.Arrow.Flight.Client
             _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);
diff --git 
a/csharp/test/Apache.Arrow.Flight.Tests/Apache.Arrow.Flight.Tests.csproj 
b/csharp/test/Apache.Arrow.Flight.Tests/Apache.Arrow.Flight.Tests.csproj
index eca1f70760..296da10463 100644
--- a/csharp/test/Apache.Arrow.Flight.Tests/Apache.Arrow.Flight.Tests.csproj
+++ b/csharp/test/Apache.Arrow.Flight.Tests/Apache.Arrow.Flight.Tests.csproj
@@ -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" />
diff --git a/csharp/test/Apache.Arrow.Flight.Tests/FlightTests.cs 
b/csharp/test/Apache.Arrow.Flight.Tests/FlightTests.cs
index 241b3c006a..acdd824590 100644
--- a/csharp/test/Apache.Arrow.Flight.Tests/FlightTests.cs
+++ b/csharp/test/Apache.Arrow.Flight.Tests/FlightTests.cs
@@ -24,6 +24,7 @@ using Apache.Arrow.Tests;
 using Google.Protobuf;
 using Grpc.Core;
 using Grpc.Core.Utils;
+using Microsoft.Extensions.DependencyInjection;
 using Xunit;
 
 namespace Apache.Arrow.Flight.Tests
@@ -546,7 +547,30 @@ namespace Apache.Arrow.Flight.Tests
             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);
         }
     }
 }

Reply via email to