[英]why grpc unhandled (StatusCode="Unavailable") ,unknown when invoking a method generated from a proto file?
# Description of the problem
When I want to call the Grpc service on the client side, it throws this exception, where is my problem? # Description of the problem
当我想在客户端调用grpc服务时,抛出这个异常,请问我的问题在哪里? #(Exception =\> StatusCode="Unavailable", Error connecting to subchannel"))
# Model
In this example, my model code is connected to the database, but it does not call any data for me # Model
在这个例子中,我的model代码连接了数据库,但是没有为我调用任何数据
[ProtoContract]
public class Person
{
[ProtoMember(1)]
public int PersonId { get; set; }
[ProtoMember(2)]
public string FirstName { get; set; }
[ProtoMember(3)]
public string LastName { get; set; }
}
Service Proto:服务原型:
public class PersonInfoService : PersonInformationService.PersonInformationServiceBase
{
private readonly IPersonService _service;
private readonly IMapper _mapper;
public PersonInfoService(IPersonService service, IMapper mapper)
{
_service = service;
_mapper = mapper;
}
public async override Task<Persons> GetAllPersonList(Empty request, ServerCallContext context)
{
var person = await _service.GetPersonListAsync();
Persons response = new Persons();
foreach (Person item in person)
{
response.Items.Add(_mapper.Map<PersonInfo>(item));
}
return await Task.FromResult(response);
}
}
Proto: proto document:原型:原型文档:
service PersonInformationService {
rpc GetAllPersonList (Empty) returns (Persons) {}
message Empty{}
message PersonInfo{
int32 personId = 1;
string firstName = 2;
string lastName = 3;
}
message Persons {
repeated PersonInfo items = 1;
}
it's enough !够了! Install the GRpc.AspNetCore.Server.Reflection package and add the service to the Program class and add its middleware .安装GRpc.AspNetCore.Server.Reflection package 并将服务添加到程序class 并添加其中间件。 Finally, by using Postman, you can debug the proto service and solve the service problem.最后通过Postman调试proto服务,解决服务问题。
using GrpcService_Test.AppDBContext;
using GrpcService_Test.Interface;
using GrpcService_Test.Services;
using Microsoft.EntityFrameworkCore;
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();
builder.Services.AddGrpcReflection();
builder.Services.AddTransient<IPersonService, PersonService>();
builder.Services.AddDbContext<AppDbContext>(opt =>
{
opt.UseSqlServer(builder.Configuration.GetConnectionString("SqlServer"));
});
builder.Services.AddCors(options => {
options.AddPolicy("cors", policy => {
policy.AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin().WithExposedHeaders("Grpc-Status", "Grpc-Message", "Grpc-Encoding", "Grpc-Accept-Encoding");
});
});
builder.Services.AddAutoMapper(typeof(Program).Assembly);
var app = builder.Build();
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors();
// Configure the HTTP request pipeline.
app.MapGrpcService<TestService>();
app.MapGrpcService<GetObjectValueService>();
app.MapGrpcService<PersonInfoService>();
app.MapGrpcReflectionService();
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();
Using this link, you can see how to test grpc in my post.使用此链接,您可以在我的帖子中了解如何测试 grpc。 [ [
]1 ]1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.