2

I want to get an object with a list. A people and db result for this.

To describe the problem I share the proto file, and then the code c # where when copy and pasting visual studio marks the errors.

A profo file is:

syntax = "proto3";
option java_multiple_files = true;
option java_package = "com.example.migrpc.javierpersonas";
option java_outer_classname = "JavierPersonas";
option objc_class_prefix = "HLW";
package javierpersonas;

service ServicioProbarPersonas {
  rpc ListaPersonas(PersonaCompleto) returns (stream PersonaCompletoCollection) {}
}

message PersonaCompleto {
  int32 idpersona = 1;
  string nombre = 2;  
  string apellido = 3;
  int32 edad = 4;
  bool active = 5;
  string status =6;
}

message PersonaCompletoCollection {
    repeated PersonaCompleto personasCompleto = 1;
    bool title = 2;
    string titlename =3;
} 

The file c# is

public override async Task<PersonaCompletoCollection>  L    istaPersonas(PersonaCompleto request, IServerStreamWriter<PersonaCompletoCollection> responseStream, ServerCallContext context)
    {           
        List<PersonaCompleto> listaPersonas = new List<PersonaCompleto>();
        var itemsDb = await _context.Persona.ToListAsync();
        foreach (var item in itemsDb)
        {
            var people = new PersonaCompleto();
            people.Idpersona = item.idpersona;
            people.Apellido = item.apellido;
            people.Nombre = item.nombre;
            people.Edad = item.edad;              

            listaPersonas.Add(people);
        }

        PersonaCompletoCollection res = new PersonaCompletoCollection();
        res.Title = true;
        res.Titlename = "doctor";
        res.PersonasCompleto = listaPersonas;
        await responseStream.WriteAsync(res);
        return await Task.FromResult(res);
    }

Res.Personacompleto is read only. What do you recommend? You can share a code?

Javier Marcuzzi
  • 113
  • 2
  • 7

2 Answers2

2

I have the same problem and the only solution I found is save data to file with protobuf-net and then send to the server.

Edit: Ok I have another solution for you. All you need to do is

var message = new PersonaCompletoCollection { 
    PersonasCompleto = {listaPersonas},
    Title = true,
    TitleName = "doctor"
};

Then

var reply = client.WriteAsync(message);

Edit2 Another solution and I think The best: Automapper IEnumerable within class is not being mapped to RepeatedField

Let me know if that works.

Silver Origami
  • 133
  • 1
  • 8
1

A repeated field can not be set directly, since it does no have a set.

You can use linq to improve the code readability and you can use addRange to add your collection:

public override async Task<PersonaCompletoCollection>  ListaPersonas(PersonaCompleto request, IServerStreamWriter<PersonaCompletoCollection> responseStream, ServerCallContext context)
{
    var itemsDb = await _context.Persona.ToListAsync();
    var itemsTransformed = itemsDb
        .Select(itemDb => new PersonaCompleto
        {
            Idpersona = item.idpersona,
            Apellido = item.apellido,
            Nombre = item.nombre,
            Edad = item.edad
        })
        .ToList();

    PersonaCompletoCollection res = new PersonaCompletoCollection
    {
        Title = true,
        Titlename = "doctor"
    };
    res.PersonasCompleto.AddRange(itemsTransformed);
    await responseStream.WriteAsync(res);
    return await Task.FromResult(res);
}
rekiem87
  • 1,565
  • 18
  • 33