Reputation: 1604
I've created a ServiceStack.JsonServiceClient
to consume 3rd party API.
I'm using the Get(IReturn<MyType>)
method.
My Request object looks like this:
public class MyRequest : Base, IReturn<MyType>
{
public MyRequest(DateTime dateTime)
{
Date = dateTime.Date;
}
[DataMember(Name = "date")]
public DateTime Date { get; init; }
}
And normally everything is fine but sometimes it fails to get results.
I set ServiceStack.JsonServiceClient.CaptureHttp()
and reviewed the logs and what I found was that it's failing when Date
is converted to a long
rather than a string
in this format yyyy-MM-dd
.
I'm thinking it's probably my fault. I probably set a static setting that toggles this behavior somewhere in my solution but I don't remember and I'm not finding it.
My question is really just why is this happening.
I already have a solution and that is to modify MyRequest
slightly as follows:
public class MyRequest : Base, IReturn<MyType>
{
public MyRequest(DateTime dateTime)
{
Date = $"{dateTime.Date:yyyy-MM-dd}";
}
[DataMember(Name = "date")]
public string Date { get; init; }
}
but again, why was this required and what could possibly be causing it to work for some time and then change behavior.
Upvotes: 2
Views: 79
Reputation: 143319
As per Date Serialization the ServiceStack's Serializers return Date's in WCF's Date serialization format, here's some parsing examples. You can also configure it to use a different Date format, e.g:
JsConfig.Init(new Config {
DateHandler = DateHandler.ISO8601,
});
Please note you should never put any implementation logic inside DTOs, they should be impl-free classes used as inert Data Transfer Objects (DTO) only.
All DTOs should also have a parameterless constructor which are used by serializers to rehydrate DTOs during deserialization.
If you want to add impl logic in a class put it in a separate domain model that you map to a plain DTO that is returned from your API instead.
Upvotes: 1