'Protobuf TimeStamp UTC
I try to map EF model with Protobuff model:
EF model
[Table("entries")]
public class Entry
{
[Key]
public Guid Uid { get; set; }
public string Ip { get; set; }
public DateTime Entered { get; set; }
public string Code { get; set; }
public int Retries { get; set; } = 0;
public Guid? TokenUid { get; set; }
[ForeignKey("TokenUid")]
public Token Token { get; set; }
}
Protobuff model
message ProtoEntry {
string ip = 1;
google.protobuf.Timestamp Entered = 2;
string code = 3;
int32 retries = 4;
string token = 5;
}
Mapping
var entries = _db.Entries
.Include(x => x.Token)
.Where(x => x.Ip == requestWithToken.Ip || x.Token.Text == token)
.Select(x => new ProtoEntry
{
Code = x.Code,
Entered = DateTime.SpecifyKind(x.Entered, DateTimeKind.Utc).ToTimestamp(),
Ip = x.Ip,
Retries = x.Retries,
Token = x.Token.Text
}).ToList();
But It throws an exception:
System.ArgumentException: Conversion from DateTime to Timestamp requires the DateTime kind to be Utc (Parameter 'dateTime')
I also tried:DateTime.SpecifyKind(x.Entered.ToUniversalTime(), DateTimeKind.Utc).ToTimestamp()
But seems it doesn't have an effect.
Can somebody help me?
Updates:
Solution 1:[1]
The problem was solved by changing Timestamp.FromDateTime() to Timestamp.FromDateTimeOffset().
Official Google .NET library for protobuff contains built-in extension for datetime types:
public static Timestamp ToTimestamp(this DateTime dateTime)
{
return Timestamp.FromDateTime(dateTime);
}
which is broken for me.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Endurance |

