ServiceSerializer.GetUpdatedAt throws System.FormatException on client
See original GitHub issueDescribe the bug
Microsoft.Datasync.Client.Serialization.ServiceSerializer.GetUpdatedAt
throws
System.FormatException: 'String '06/22/2022 11:01:27' was not recognized as a valid DateTime.'
when PullItemsAsync on client
To Reproduce
Steps to reproduce the behavior:
Call await MyObjectsOfflineTable.PullItemsAsync().ConfigureAwait(false);
on client, this will throw System.FormatException
Expected behavior
System.FormatException
is not thrown
What platforms?
-
Server: ASP.NET Core, Net 6.0:
- Microsoft.AspNetCore.Datasync, Version=5.0.5
- Microsoft.AspNetCore.Datasync.EFCore, Version=5.0.5
- Azure SQL database
-
Client: UWP and Android (not yet tested on iOS, but the bug should also exist):
- Xamarin.Forms
- Version: 5.0.0.2478
- Platforms: UWP and Android
- This happen on any device
Screenshots
Location of the exception:
Content of item
JObject:
UpdatedAt
value cast directly from JObject:
Additional context
To solve the problem, it would be necessary to define the expected date format in DateTimeOffset.Parse
because DateTimeOffset
does not necessarily expect the ISO 8601 format
We may have missed something, maybe we need to configure the JSON serializer used on our client. Because I don’t understand why this code works on your clients.
After analysis, it seems that the updatedAt
value can be directly cast from item
JObject thanks to the JSON deserializer, without the need to go through a transformation of a string.
Here is the code we use to work around the problem and it works on the clients we use:
public static DateTimeOffset? GetUpdatedAt(JObject item)
{
var l_jvValue = item[SystemProperties.JsonUpdatedAtProperty];
try
{
var l_dtResult = ((DateTimeOffset?)l_jvValue);
if (l_dtResult.HasValue) return l_dtResult;
}
catch
{
}
string updatedAt = item.Value<string>(SystemProperties.JsonUpdatedAtProperty);
if (updatedAt == null)
{
return null;
}
// Throw an error if the service returned a bad date format.
return DateTimeOffset.Parse(updatedAt);
}
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Did you have the lastest commit? I see less test, I have 3954 tests with 1219 in Integration.Test while you have 2921 and 686
Hello Fileman, Thanks for the remark.
After pulling the latest version of the library (which forced me to install VS 2022 Preview), all tests pass regardless of the regional settings configured on the PC.
While inspecting the code, I noticed that on May 25 mcudri changed the last line of code of the
GetUpdatedAt
function which changed fromreturn DateTimeOffset.Parse(updatedAt);
toreturn DateTimeOffset.Parse(updatedAt, CultureInfo.InvariantCulture);
This change solved the problem.