簡體   English   中英

為什么 grpc unhandled (StatusCode="Unavailable"), 在調用從 proto 文件生成的方法時未知?

[英]why grpc unhandled (StatusCode="Unavailable") ,unknown when invoking a method generated from a proto file?

# Description of the problem當我想在客戶端調用grpc服務時,拋出這個異常,請問我的問題在哪里? #(Exception =\> StatusCode="Unavailable", Error connecting to subchannel"))

# 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; }
    }

服務原型:

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);
            }
        }

原型:原型文檔:

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;
}

夠了! 安裝GRpc.AspNetCore.Server.Reflection package 並將服務添加到程序class 並添加其中間件 最后通過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();

使用此鏈接,您可以在我的帖子中了解如何測試 grpc。 [

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

]1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM