简体   繁体   中英

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? #(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

[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 . Finally, by using Postman, you can debug the proto service and solve the service problem.

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. [

https://learning.postman.com/docs/sending-requests/grpc/first-grpc-request/

]1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM