noo*_*oob 10 c# grpc asp.net-core
我想要将 Person 模型的列表返回给 grpc.project 中的客户端是 asp.net core
person.proto 代码是:
syntax = "proto3";
option csharp_namespace = "GrpcService1";
service People {
rpc GetPeople (RequestModel) returns (ReplyModel);
}
message RequestModel {
}
message ReplyModel {
repeated Person person= 1;
}
message Person {
int32 id = 1;
string firstName=2;
string lastName=3;
int32 age=4;
}
Run Code Online (Sandbox Code Playgroud)
PeopleService.cs 代码是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Grpc.Core;
using Microsoft.Extensions.Logging;
namespace GrpcService1
{
public class PeopleService:People.PeopleBase
{
private readonly ILogger<GreeterService> _logger;
public PeopleService(ILogger<GreeterService> logger)
{
_logger = logger;
}
public override async Task<ReplyModel> GetPeople(RequestModel request, ServerCallContext context)
{
List<Person> people = new List<Person>() {
new Person() { Id=1,FirstName="david",LastName="totti",Age=31},
new Person() { Id=2,FirstName="lebron",LastName="maldini",Age=32},
new Person() { Id=3,FirstName="leo",LastName="zidan",Age=33},
new Person() { Id=4,FirstName="bob",LastName="messi",Age=34},
new Person() { Id=5,FirstName="alex",LastName="ronaldo",Age=35},
new Person() { Id=6,FirstName="frank",LastName="fabregas",Age=36}
};
ReplyModel replyModel = new ReplyModel();
replyModel.Person = people; //this line is error : Property or indexer 'ReplyModel.Person' cannot be assigned to --it is read only
return replyModel;
}
}
}
Run Code Online (Sandbox Code Playgroud)
和客户端项目调用 grpc 服务器:
var channel = GrpcChannel.ForAddress("https://localhost:5001");
var client = new People.PeopleClient(channel);
var result= client.GetPeople(new RequestModel(), new Grpc.Core.Metadata());
Run Code Online (Sandbox Code Playgroud)
这适用于一个模型,但当我想要返回模型列表时我不能。我如何将列表发送到客户项目?感谢您阅读我的问题
小智 17
将错误行 ( replyModel.Person = people) 更改为此代码
replyModel.Person.AddRange(people);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7110 次 |
| 最近记录: |