Blazor WebAssembly code throwing exception only after being published

64 Views Asked by At

I have following converter:

public class ExpandoObjectConverter : JsonConverter<dynamic>
{
    private static readonly ExpandoObjectConverter self = new();

    public override dynamic Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        // By default, it is not using itself for deeper levels in the object.
        if (!options.Converters.OfType<ExpandoObjectConverter>().Any())
        {
            options = new JsonSerializerOptions(options);
            options.Converters.Add(self);
        }

        if (reader.TokenType == JsonTokenType.StartObject)
        {
            return JsonSerializer.Deserialize<ExpandoObject>(ref reader, options);
        }

        else if (reader.TokenType == JsonTokenType.StartArray)
        {
            var list = new List<object>();
            while (reader.Read())
            {
                if (reader.TokenType == JsonTokenType.EndArray)
                {
                    break;
                }
                if (reader.TokenType == JsonTokenType.StartObject)
                {
                    list.Add(JsonSerializer.Deserialize<ExpandoObject>(ref reader, options));
                }
                else
                {
                    list.Add(GetValue(ref reader));
                }
            }
            return list.ToArray();
        }
        else
        {
            return GetValue(ref reader);
        }
    }

    public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
    {
        JsonSerializer.Serialize(writer, value, options);
    }

    private object GetValue(ref Utf8JsonReader reader) =>
        reader.TokenType switch
        {
            JsonTokenType.False or JsonTokenType.True => reader.GetBoolean(),
            JsonTokenType.Null => null,
            JsonTokenType.Number => reader.GetDecimal(),
            _ => reader.GetString()
        };
}

And a helper class that uses it:

public class StringContainer
{
    private readonly JsonSerializerOptions opts = new() { Converters = { new ExpandoObjectConverter() } };

    public StringContainer(            
        ILanguageFileProvider languageFileProvider)
    {
        this.logger = logger;

        var json = languageFileProvider.GetLanguageFile();

        ExpandoObject? inter = JsonSerializer.Deserialize<dynamic>(json, opts);

        ...
    }
....

It works with the same input on all platforms, except, when used in WebAssembly, and even there fails only after being published (not when run directly, indifferent if debug or relesae). There, and only there, I get this:

blazor.webassembly.js:1 System.AggregateException: AggregateException_ctor_DefaultMessage (DeserializeNoConstructor, JsonConstructorAttribute, System.Dynamic.ExpandoObject Path: $ | LineNumber: 0 | BytePositionInLine: 1.)
 ---> System.NotSupportedException: DeserializeNoConstructor, JsonConstructorAttribute, System.Dynamic.ExpandoObject Path: $ | LineNumber: 0 | BytePositionInLine: 1.
 ---> System.NotSupportedException: DeserializeNoConstructor, JsonConstructorAttribute, System.Dynamic.ExpandoObject
   Exception_EndOfInnerExceptionStack
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException(ReadStack& , Utf8JsonReader& , NotSupportedException )
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException_DeserializeNoConstructor(Type , Utf8JsonReader& , ReadStack& )
   at System.Text.Json.Serialization.JsonDictionaryConverter`3[[System.Dynamic.ExpandoObject, System.Linq.Expressions, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a],[System.String, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Object, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].CreateCollection(Utf8JsonReader& , ReadStack& )
   at System.Text.Json.Serialization.Converters.IDictionaryOfTKeyTValueConverter`3[[System.Dynamic.ExpandoObject, System.Linq.Expressions, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a],[System.String, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Object, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].CreateCollection(Utf8JsonReader& , ReadStack& )
   at System.Text.Json.Serialization.JsonDictionaryConverter`3[[System.Dynamic.ExpandoObject, System.Linq.Expressions, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a],[System.String, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Object, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].OnTryRead(Utf8JsonReader& , Type , JsonSerializerOptions , ReadStack& , ExpandoObject& )
   at System.Text.Json.Serialization.JsonConverter`1[[System.Dynamic.ExpandoObject, System.Linq.Expressions, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]].TryRead(Utf8JsonReader& , Type , JsonSerializerOptions , ReadStack& , ExpandoObject& , Boolean& )
   at System.Text.Json.Serialization.JsonConverter`1[[System.Dynamic.ExpandoObject, System.Linq.Expressions, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]].ReadCore(Utf8JsonReader& , JsonSerializerOptions , ReadStack& )
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1[[System.Dynamic.ExpandoObject, System.Linq.Expressions, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]].Deserialize(Utf8JsonReader& , ReadStack& )
   at System.Text.Json.JsonSerializer.Read[ExpandoObject](Utf8JsonReader& , JsonTypeInfo`1 )
   at System.Text.Json.JsonSerializer.Deserialize[ExpandoObject](Utf8JsonReader& , JsonSerializerOptions )
   at Shared.Helpers.ExpandoObjectConverter.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options)
   ...

I thought, I get a BOM, but no. My second guess is, that something is trimmed away when published. But I haven't set any trimming. Neither for the host project nor for the wasm project. The blazor wasm project is built together with the host.

How can I fine-tune its publishing process? What could be the missing piece? What parameter should I change and where? I could most likely replace this code with something else, but if this caused issues, something else, more critical could cause as well...

0

There are 0 best solutions below