首先,检查gRPC服务端和客户端的proto是否完全一致,包括package 定义和调用参数及返回类型。
如果不是上述问题,我这边出现的问题在于服务端没有MapService,在Program.cs里面添加,最终像这样:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using xxx.Services; var builder = WebApplication.CreateBuilder(args); // Additional configuration is required to successfully run gRPC on macOS. // For instructions on how to configure Kestrel and gRPC clients on macOS, visit https://go.microsoft.com/fwlink/?linkid=2099682 // Add services to the container. builder.Services.AddGrpc(); var app = builder.Build(); app.MapGrpcService<MyRpcService>(); //划重点,我就是在添加新proto后忘记了加这句 app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909"); app.Run(); |
如果你的服务端是Asp.net Core项目,则需要在 Startup.cs里面添加,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapGrpcService<GreeterService>(); //Add your endpoint here like this endpoints.MapGrpcService<YourProtoService>(); }); |