10

i want return list of Person model to client in grpc.project is asp.net core

person.proto code is :

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

PeopleService.cs code is :

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

    }
}

and client project call grpc server :

var channel = GrpcChannel.ForAddress("https://localhost:5001");
var client = new People.PeopleClient(channel);
var result= client.GetPeople(new RequestModel(), new Grpc.Core.Metadata());

this work for one model but when want return list of model i can't.how i can send list to client project? thanks for read my problem

noob
  • 126
  • 1
  • 7

1 Answers1

15

change error line (replyModel.Person = people) to this code

replyModel.Person.AddRange(people);
mehdi farhadi
  • 1,415
  • 1
  • 10
  • 16