question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ServiceSerializer.GetUpdatedAt throws System.FormatException on client

See original GitHub issue

Describe 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: image

Content of item JObject: image

UpdatedAt value cast directly from JObject: image

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:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
filemancommented, Jun 27, 2022

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

1reaction
cbs-cbtcommented, Jun 28, 2022

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 from return DateTimeOffset.Parse(updatedAt); to return DateTimeOffset.Parse(updatedAt, CultureInfo.InvariantCulture);

This change solved the problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Exception Where it shouldn't be (System.FormatException). ...
I am a little new to C# multithreading. Can errors just be thrown from a different thread and show up on what is...
Read more >
FormatException Class (System)
The exception that is thrown when the format of an argument is invalid, or when a composite format string is not well formed....
Read more >
why would this date throw System.FormatException: #705
It's throwing System.FormatException: 'String was not recognized as a valid DateTime.' - should it?
Read more >
Type: System.FormatException
FormatException is thrown when the format of an argument in a method invocation does not match the format of the corresponding formal parameter...
Read more >
System.FormatException: 'Input string was not in a correct ...
Hi, I am posting following query to server, where it throws error like- System.FormatException: 'Input string was not in a correct format.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found