0

When running the below example, I get the error

System.InvalidOperationException: JsonPropertyInfo 'test' defined in type 'Test.Config' is marked required but does not specify a setter.

If I don't include the JsonRequired attribute on the parameter, then it does not produce the error but then I get a default instantiation of my config object instead of one deserialized from my file

This happens only when running on Linux. On Windows it deserializes fine.

public class Config
{
    [JsonRequired]
    public int test { get;  set; } = 1;
}


JsonSerializerOptions options = new JsonSerializerOptions();
using (StreamReader reader = new StreamReader(filePath)){
    config = JsonSerializer.Deserialize<Config>(reader.BaseStream, options);
}

JSON file:

{
  "test": 2
}
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
John Moffitt
  • 5,730
  • 7
  • 30
  • 39
  • Double check the filepath for upper vs lower case letters. Linux filenames are case sensitive and is often the cause of linux only isues. – Ben Jul 05 '23 at 14:23
  • 1
    @Ben then prolly there would be FileNotFoundException .... – Selvin Jul 05 '23 at 14:27
  • 3
    Where does 'NumberOfSensorInfoFetchThreads' come from? If you have sample code then show the error that is thrown by that sample code or show the real code. – Ralf Jul 05 '23 at 14:30
  • 3
    How do you deploy the app? Is it trimmed? – Guru Stron Jul 05 '23 at 14:30
  • Does `[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(Config))]` help at all? – Marc Gravell Jul 05 '23 at 14:56
  • 2
    @GuruStron Guru, that was it. I was debugging on windows and deploying to linux. The deploy was trimming unused assemblies. After turning that off it is working. If you create an answer I will accept it. – John Moffitt Jul 05 '23 at 15:11

1 Answers1

2

Check if you are trying to run a trimmed application version.

Read more:

Guru Stron
  • 102,774
  • 10
  • 95
  • 132