简体   繁体   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 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。 [ [

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

]1 ]1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio 从 gRPC.proto 文件自动生成代码 - Visual Studio auto generated code from gRPC .proto file 使用 csharp GRPC gen 导入的 proto 文件未生成 - using csharp GRPC gen imported proto file not generated Grpc.Core.RpcException:'状态(状态代码=“不可用”,详细信息=“空更新”, - Grpc.Core.RpcException: 'Status(StatusCode=“Unavailable”, Detail=“Empty update”, Grpc.Core.RpcException: 'Status (StatusCode = "Unavailable", Detail = "Error starting gRPC call - Grpc.Core.RpcException: 'Status (StatusCode = "Unavailable", Detail = "Error starting gRPC call 为什么要通过Google Cloud Firestore 1.0.0-beta05 C#SetAsync获取Grpc.Core.RpcException StatusCode =不可用,Detail =“连接失败”? - Why getting Grpc.Core.RpcException StatusCode=Unavailable, Detail=“Connect Failed”, with Google Cloud Firestore 1.0.0-beta05 C# SetAsync? C# gRPC 服务没有 proto 文件 - C# gRPC service without proto file 为所有解决方案共享一个 gRPC proto 文件 - Sharing one gRPC proto file for all solutions Grpc.Core.RpcException StatusCode Unavailable Channel 处于状态 TRANSIENT_FAILURE - Grpc.Core.RpcException StatusCode Unavailable Channel is in state TRANSIENT_FAILURE C# 程序将数据保存到 Google FirestoreDB 在某些计算机上失败(状态代码=不可用;grpc_status:14) - C# Program saving data to Google FirestoreDB fails on some computers (StatusCode=Unavailable; grpc_status: 14) 为什么发生异常时未从Mvc设置StatusCode - Why is StatusCode not set from Mvc when exception occurs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM