dotnet.rpc.client\Rpc.Client\Program.cs
using Grpc.Core;
using Listen.Proto;
try
{
var channel = new Channel("localhost:9901", ChannelCredentials.Insecure);
var client = new Listen.Proto.Listen.ListenClient(channel); // 确保名称正确
var request = new Request { Name = "Hello from C# Client!" };
var response = await client.ListenDataAsync(request);
Console.WriteLine($"Server Response: {response.Message}");
await channel.ShutdownAsync();
}
catch (RpcException ex)
{
Console.WriteLine($"gRPC Error: {ex.Status.Detail}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
dotnet.rpc.client\Rpc.Client\Rpc.Client.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Protobuf Include="Protos\listen.proto" />
</ItemGroup>
👇👇👇👇👇👇👇👇👇👇三个核心依赖包👇👇👇👇👇👇👇👇👇👇
<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.30.2" />
<PackageReference Include="Grpc" Version="2.46.6" />
<PackageReference Include="Grpc.Tools" Version="2.71.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>