Search code examples
c#asp.net-coreprotocol-buffersgrpc

How to have a nullable DateTime property in gRPC .Net?


I'm using gRPC to write a method and give it to another microservice but I do not know how to generate a nullable DateTime property in protobuf and fill it with a data that I get from AppService! The final property that I need is like below:

public class MyClass{
   public DateTime? MyDateTime {get;set;}
}

And also there is no variable type as a DateTime in protobuf.

I'm expecting to set a nullable DateTime property in my proto file and after that fill it with a DataTime that I get from another service in gRPCService.


Solution

  • I found an answer by using:

    import "google/protobuf/timestamp.proto";
    

    In proto file and also in the current file the message property should be:

    message MyTestDto{
        google.protobuf.Timestamp MyTimestamp = 1;
    }
    

    After doing all of these steps in the GrpcService I used this C# code to convert timestamp to DateTime:

    Timestamp.FromDateTimeOffset(x.CvCardFa.MyTimestamp.Value.ToLocalTime())