Windowstemplatestudio: Input String exception in Json Helper

Created on 8 Jan 2018  Â·  25Comments  Â·  Source: microsoft/WindowsTemplateStudio

For Bugs:

Newtonsoft.Json.JsonReaderException: 'Input string '1.0.0.0' is not a valid number. Path '', line 1, position 7.'

Application was running just fine and I was normally working with my project, when suddenly on a debug session this exception occured. I can assure that there are only 2 references to this method, which were already in th generated code, to use in settings storage.

image

Repro steps

Just run the app. and this exception occurs on splash screen before the app is completely loaded. I tried cleaning and rebuilding the solution but that didnt help.

System

  • VS Version: VS 2017 , 15.5
  • WTS Wizard Version: dev nightly ( excluding the latest update )
  • WTS Template Version: dev nightly ( excluding the latest update )
  • Windows Build: fall creators update.
bug external-issue

Most helpful comment

@ralarcon @crutkas There's a potential for others to hit this issue if:

  1. They include the What's New Prompt feature in projects generated before v1.7
    and
  2. They add a reference to Toolkit v2.1 (or higher) and use the SystemInformation class.

If a lot of people hit this issue we may need to look to do something to address this. I'm not sure what though as the code has already shipped. It may just be that we need to point them to the above workaround.

All 25 comments

I'm trying to found the origin of the exception using a cache reading/saving PoC. Could you please paste on this Issue the call stack trace of the exception?

CALL STACK

Fluent_Video_Player.exe!Fluent_Video_Player.Helpers.Json.ToObjectAsync.AnonymousMethod__0() Line 14 C# Symbols loaded.

@mvegaca

I dont know why call stack isnt giving the full trace, but let me help you by telling you what I've found out so far. This method is referenced at 2 places in "SettingsStorageExtensions.cs" and this exception occurs on the 2nd referenced method in that file on line 59 as shown in the image below .
image

I confirmed it by using breakpoints on both calls of this method, that it happens on the line 59 as I mentioned above, also note that exception occurs on the third call of this line. Which means that the first 2 calls of this method on line 59 are executed perfectly, the 3rd time the app version number is passed to this method as "1.0.0.0" which is obviously the right version number, but its not an integer technically, hence causing the exception.

Hope that helps :)
@mvegaca

looking at your initial claim, what is happening is your setting file is being read and then deserialized into an object and failing on that.

Uninstalling your app via start menu would remove that setting file. As just cleaning the solution won't be enough.

Why your setting file got in a weird state all depends on what you're putting in it. That to me is the more interesting question here and that feels heavy implementation details.

@crutkas I havent changed anything in the setting file or havent put anything in settings by myself, everything was generated by the wts. i used normal settings file and included feedback link in it ( which was also included by the wizard )

@crutkas error is resolved by cleaning the solution and uninstalling the app but i still dont knw the reason of the error cause

There's a short callstack as it only goes up to the start of the Task that was awaited to run the code. You may see more from expanding the exception details.


The only thing that writes a string like "1.0.0.0" to settings is the WhatsNewDisplayService. (As this stores the last used version number in the settings to detect changes.)
Are you using this features?
However, this was recently (will be in v1.7 - #1304) updated to use the Community Toolkit for the underlying functionality. If this change is causing something to break then we need to know. If the issue was before the change then it may or may not be an issue. To investigate further it will be important to know which version of the templates was used to create the project.

dev nightly ( excluding the latest update )

Isn't enough to indicate what version you're running. Do you mean it was built from a dev-nightly build before 1.6 was released? Either way, knowing the exact version number would aid in trying to identify this (or any similar) issue. The version used to generate the project can be found in Package.appxmanifest. Just open it as a text (or XML) file to view it.

Just run the app. and this exception occurs on splash screen before the app is completely loaded.

This isn't enough to reproduce an issue unless we have access to your app.
We need to know how we can reproduce the error, not how you do.


app version number is passed to this method as "1.0.0.0" which is obviously the right version number, but its not an integer

This value isn't a number and shouldn't be. My best guess of what's happened is a corruption of the settings file. The value is being deserialized for a different property. It's a shame you uninstalled the app without first taking a copy of the file.

Without more details, a repro, or best of all, a copy of the settings file, I'm not sure what else we can do apart from monitor the issue and see if anything happens again.

@mrlacey here are the project wts details.






@mrlacey If I had known the settings file can help I surely wouldve saved it for investigation. But unfortunately I didn't. :(

0.10.17356.1 is Pre-Release build. This is different from a dev/nightly build.
0.10.17356.1 is also the current pre-release build.

Knowing this makes it easier to confirm that there isn't a simple repro that affects every use.

so the solution u mentioned tht uwp community toolkit will be used in 1.7, when can we have tht in pre-release?

@mrlacey @crutkas @mvegaca I was able to reproduce the issue. by doing the exact same thing I did the last time and now I know what change caused it.

StackTrace

StackTrace " at Newtonsoft.Json.JsonTextReader.ParseReadNumber(ReadType readType, Char firstChar, Int32 initialPosition)\rn

at Newtonsoft.Json.JsonTextReader.ParseNumber(ReadType readType)\rn

at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType)\rn

at Newtonsoft.Json.JsonTextReader.ReadAsString()\rn

at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)\rn

at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)\rn

at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)\rn

at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)\rn at Newtonsoft.Json.JsonConvert.DeserializeObjectT\rn at Newtonsoft.Json.JsonConvert.DeserializeObjectT\rn

at Fluent_Video_Player.Helpers.Json.<>c__DisplayClass0_0`1.b__0()\rn

at System.Threading.Tasks.Task`1.InnerInvoke()\rn at System.Threading.Tasks.Task.Execute()" string

Steps to Reproduce

if (SystemInformation.DeviceFamily == "Desktop")

Stated Above is an If condition I am using in my app to find out device and write some code accordingly, with breakpoints, I found out that DeviceFamily is actually returning "Windows.Desktop" . So in order to correct it I used the following condition.

if (SystemInformation.DeviceFamily == "Windows.Desktop")

As I made this change and tried to debug the app again I got exact same exception as before. And now I remember that last time I made the same change when I got the exception the first time.

Also note that the SystemInformation helper class I am using for this purpose is part of UWP community toolkit 2.1.1 nuget package.

I also retrieved the "suspensionState.json" file from package folder and it has the following data in it.

{"SuspensionState":{"Data":null,"SuspensionDate":"2018-01-09T00:59:30.2911397+05:00"},"Target":null}

The prerelease version of 1.7 will be made available when v1.7 is ready for RC level testing.


If you can reproduce the issue, can you share enough information (or a full project) that allows us to reproduce the issue?
Just saying that you know what code you wrote and a few pieces of code are insufficient.

There are two ways to provide a good repro:

  1. Provide a minimal project and explain what to do when running it.
  2. Provide a complete set of steps (including any code to add) to create a project that reproduces the issue.

For reference, a good bug report should include three things.

  1. The absolute minimum required to reproduce the issue.
  2. What happens when the bug occurs.
  3. What was expected to happen.

Ok. So, both the WTS code and the toolkit code are saving a setting called "currentVersion" if you use WTS v1.6 or lower and toolkit 2.1 or higher.

One saves the value as a plain string. The other first serializes it to JSON.
The problem then comes when one attempts to deserialize the value which was written in the format of the other.

What we end up with is code like this
Newtonsoft.Json.JsonConvert.DeserializeObject<string>("1.0.0.0")

When the calling code is expecting something like this
Newtonsoft.Json.JsonConvert.DeserializeObject<string>("\"1.0.0.0\"")

The Json.Net error message is misleading in this instance as it talks about an issue with a number. This makes sense with what it's trying to do internally, but not from a consumer perspective.

_If only there was a better way to get helpful error messages when working with JSON. #whistles_


Anyway, you can work around this issue by changing the WhatsNewDisplayService to be like this:

    public static class WhatsNewDisplayService
    {
        internal static async Task ShowIfAppropriateAsync()
        {
            if (Microsoft.Toolkit.Uwp.Helpers.SystemInformation.IsAppUpdated)
            {
                var dialog = new WhatsNewDialog();
                await dialog.ShowAsync();
            }
        }
    }

The above workaround is what WTS will produce from v1.7.

@mrlacey actually our project contains some libraries built on this machine and referenced in the uwp project, so providing full project wont be possible, because building steps on another machine will be quite a lot. I am trying to get a minimal project, so far I am not able to reproduce this issue in a minimal project but if I do, I will surely provide the repo for that.
As far as my project is concerned it remains error free if I don't use SystemInformation class by the toolkit, specifically with "Windows.Desktop" parameter, and yeah it does have something to do with What's new prompt because my app is also using that feature. and it never occurs on the first run, because my app also has the first run dialog feature. Hope this helps.

@ralarcon @crutkas There's a potential for others to hit this issue if:

  1. They include the What's New Prompt feature in projects generated before v1.7
    and
  2. They add a reference to Toolkit v2.1 (or higher) and use the SystemInformation class.

If a lot of people hit this issue we may need to look to do something to address this. I'm not sure what though as the code has already shipped. It may just be that we need to point them to the above workaround.

internal static async Task ShowIfAppropriateAsync()
    {
        var currentVersion = PackageVersionToReadableString(Package.Current.Id.Version);

        var lastVersion = await Windows.Storage.ApplicationData.Current.LocalSettings.ReadAsync<string>(nameof(currentVersion));

        if (lastVersion == null)
        {
            await Windows.Storage.ApplicationData.Current.LocalSettings.SaveAsync(nameof(currentVersion), currentVersion);
        }
        else
        {
            if (currentVersion != lastVersion)
            {
                await Windows.Storage.ApplicationData.Current.LocalSettings.SaveAsync(nameof(currentVersion), currentVersion);

                var dialog = new WhatsNewDialog();
                await dialog.ShowAsync();
            }
        }
    }

    private static string PackageVersionToReadableString(PackageVersion packageVersion)
    {
        return $"{packageVersion.Major}.{packageVersion.Minor}.{packageVersion.Build}.{packageVersion.Revision}";
    }

@mrlacey Above is whatsnewDisplayService in my project, Should I replace it completely with the class you provided for workaround?

@touseefbsb Yes replace the class fully. This is what it'll look like from v1.7. (see) Almost identical logic now exists in the toolkit.

That it doesn't occur on your apps first run probably comes down to when you're calling the SystemInformation class. I have managed to create a really simple repro that blows up on the first run.

That I wrote the code for both projects has been a massive help in solving this. Not a fun issue to debug but hopefully, the above workaround unblocks you.

@mrlacey Yes thanks a lot for the workaround its working just fine now :) it really was a complicated one but you did a great job with such limited information. Cheers!

I want go on an project but don't know how to on it

Sent from my Windows Phone


From: Touseef Maliknotifications@github.com
Sent: ‎1/‎8/‎2018 10:53 PM
To: Microsoft/WindowsTemplateStudioWindowsTemplateStudio@noreply.github.com
Cc: Subscribedsubscribed@noreply.github.com
Subject: Re: [Microsoft/WindowsTemplateStudio] Input String exception in Json Helper (#1659)

@mrlaceyhttps://github.com/mrlacey Yes thanks a lot for the workaround its working just fine now :) it really was a complicated one but you did a great job with such limited information. Cheers!

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHubhttps://github.com/Microsoft/WindowsTemplateStudio/issues/1659#issuecomment-356108338, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AdoJKRHmHQ2h3KVT3-7JvVW4Q21eAXP2ks5tIo5AgaJpZM4RWeYt.

@mrlacey and @ralarcon thoughts on prepending our settings keys to prevent this in the future?

@Chibest27 go to https://aka.ms/wtsinstall to get the extension :)

thoughts on prepending our settings keys to prevent this in the future?

@crutkas good plan > #1660

Hi, guys. Thanks for your workaround.

I just add the SystemInformationPage today, copied from UWPCommunityToolkit.

And my project was created using WTS in Sept, 2017, including the What's New Prompt feature. The version should below 1.7.

So the same problem happened. I search all the internet, but found nothing. Thanks for your discussion here. 💯

Was this page helpful?
0 / 5 - 0 ratings