I am using gRPC protobuf-net to facilitate communication between a client application and a service.
The client can send a command to retrieve an entity by its ID. On the server side, the service method calls a method in the repository which may return a null value if the entity does not exist.
The issue I’m encountering is that I receive an exception when the entity is null, but everything works fine when the entity is not null.
Therefore, I suspect that the problem lies in protobuf-net’s inability to serialize a null value.
One solution I’ve considered is to use a ‘Response’ class that has a property of the type of value to be sent, which can be null. This approach works, but I would prefer to avoid this solution if possible.
Does protobuf-net support the transmission of null values, or should I encapsulate the result in a ‘Response’ class?
Thank you very much.
EDIT:
This is the code of the Iterface, the service implementation and the client implementation.
//Used as request because if I am not wrong, protobuf-net can't serialize a .NET primitve, I need a class.
[DataContract]
public class LongDTO
{
public LongDTO() { }
public LongDTO(long paramLong)
{
Long = paramLong;
}
[DataMember(Order = 1)]
public readonly long Long;
}
[ServiceContract]
public interface IMyService
{
public Task<MyType?> GetEntityAsync(LongDTO paramLongRequest);
}
[Authorize]
public class MyServiceProtobufNet : IMyService
{
public async Task<MyType?> GetEntityAsync(LongDTO paramLongRequest)
{
//if the entity doesn't exists in the database, the repository returns null.
return await _repository.GetEntityById(paramLongRequest.Long);
}
}
public class MyCLientProtobufNet
{
public async Task<MyType?> GetEntityAsync(long paramId)
{
LongDTO miRequest = new LongDTO(paramId);
return await _cliente.GetEntityAsync(miRequest);
}
}