安装 GRPC.Tools NuGet 包并从 Greeter.proto 构建 Greeter.cs 文件后,我收到很多缺少依赖项的错误
Greeter.cs 文件需要一些 google API,显然这不是由 Grpc.Tools 安装的
我还需要安装什么,我还缺少什么?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GreeterService;
using Grpc.Core;
namespace gRPCtest
{
public class GreeterService : Greeter.GreeterBase
{
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
{
return Task.FromResult(new HelloReply
{
Message = "Hello, " + request.Name
});
}
}
public class Server : Form
{
public Server()
{
Text = "Server Window";
Size = new System.Drawing.Size(300, 200);
const int port = 50051;
Server server = new Server
{
Services = { Greeter.BindService(new GreeterServiceImpl()) },
Ports = { new ServerPort("localhost", port, ServerCredentials.Insecure) }
};
server.Start();
Console.WriteLine($"Greeter service is listening on port {port}");
Console.WriteLine("Press any key to stop the server...");
Console.ReadKey();
server.ShutdownAsync().Wait();
}
}
public class Client : Form
{
public Client()
{
Text = "Client Window";
Size = new System.Drawing.Size(300, 200);
const string serverAddress = "localhost";
const int port = 50051;
Channel channel = new Channel($"{serverAddress}:{port}", ChannelCredentials.Insecure);
var client = new Greeter.GreeterClient(channel);
string name = "John";
var request = new HelloRequest { Name = name };
var reply = client.SayHello(request);
Console.WriteLine($"Received: {reply.Message}");
channel.ShutdownAsync().Wait();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Create a new instance of the child window
Server serverForm = new Server();
serverForm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
// Create a new instance of the child window
Client clientForm = new Client();
clientForm.Show();
}
}
}
您还需要添加 Google.Protobuf NuGet 包 - Protocol Buffers 的 C# 运行时库。