在Blazor客户端中使用从JsonConverter派生的自定义转换器对对象图进行反序列化时出错

时间:2019-05-17 10:03:39

标签: json.net blazor

当从blazor服务器端api方法返回时,我想在blazor客户端中反序列化对象图(类似于weatherforecasts示例)。我需要将自己的反序列化方法与基于JsonConverter的自定义转换器一起使用,因为我的对象图具有实现相同接口的复合对象。

一切都在我的blazor项目之外工作,如果我将对象图作为字符串返回给客户端,则它已经正确序列化了。反序列化无法正常工作。

blazor.webassembly.js:1 WASM: System.MissingMethodException: Constructor on type 'System.ComponentModel.ReferenceConverter' not found.
blazor.webassembly.js:1 WASM:   at System.RuntimeType.CreateInstanceImpl (System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes, System.Threading.StackCrawlMark& stackMark) <0x20812e8 + 0x0032c> in <f4072889680a4c139ae7f58d7e8832a7>:0 
blazor.webassembly.js:1 WASM:   at System.Activator.CreateInstance (System.Type type, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes) <0x2080d18 + 0x000c8> in <f4072889680a4c139ae7f58d7e8832a7>:0 
blazor.webassembly.js:1 WASM:   at System.Activator.CreateInstance (System.Type type, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture) <0x267adf8 + 0x00016> in <f4072889680a4c139ae7f58d7e8832a7>:0 
blazor.webassembly.js:1 WASM:   at System.SecurityUtils.SecureCreateInstance (System.Type type, System.Object[] args, System.Boolean allowNonPublic) <0x267ad28 + 0x00092> in <19bbec7b522743e889bff711c7b96bc3>:0 
blazor.webassembly.js:1 WASM:   at System.SecurityUtils.SecureCreateInstance (System.Type type) <0x2678c10 + 0x00008> in <19bbec7b522743e889bff711c7b96bc3>:0 
blazor.webassembly.js:1 WASM:   at System.ComponentModel.ReflectTypeDescriptionProvider.CreateInstance (System.Type objectType, System.Type callingType) <0x2677f58 + 0x00034> in <19bbec7b522743e889bff711c7b96bc3>:0 
blazor.webassembly.js:1 WASM:   at System.ComponentModel.ReflectTypeDescriptionProvider.SearchIntrinsicTable (System.Collections.Hashtable table, System.Type callingType) <0x26774e8 + 0x002e8> in <19bbec7b522743e889bff711c7b96bc3>:0 
blazor.webassembly.js:1 WASM:   at System.ComponentModel.ReflectTypeDescriptionProvider+ReflectedTypeData.GetConverter (System.Object instance) <0x266a3b8 + 0x00180> in <19bbec7b522743e889bff711c7b96bc3>:0 
blazor.webassembly.js:1 WASM:   at System.ComponentModel.ReflectTypeDescriptionProvider.GetConverter (System.Type type, System.Object instance) <0x2669d30 + 0x00016> in <19bbec7b522743e889bff711c7b96bc3>:0 
blazor.webassembly.js:1 WASM:   at System.ComponentModel.TypeDescriptor+TypeDescriptionNode+DefaultTypeDescriptor.System.ComponentModel.ICustomTypeDescriptor.GetConverter () <0x2669a88 + 0x0003e> in <19bbec7b522743e889bff711c7b96bc3>:0 
blazor.webassembly.js:1 WASM:   at System.ComponentModel.TypeDescriptor.GetConverter (System.Type type) <0x2665718 + 0x00010> in <19bbec7b522743e889bff711c7b96bc3>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.Serialization.JsonTypeReflector.CanTypeDescriptorConvertString (System.Type type, System.ComponentModel.TypeConverter& typeConverter) <0x265a220 + 0x0000a> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.Serialization.DefaultContractResolver.CanConvertToString (System.Type type) <0x265a010 + 0x0000a> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract (System.Type objectType) <0x260ec98 + 0x0013c> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver.ResolveContract (System.Type type) <0x260e2b8 + 0x000a2> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.GetContractSafe (System.Type type) <0x260dda0 + 0x00034> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList (System.Collections.IList list, Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonArrayContract contract, Newtonsoft.Json.Serialization.JsonProperty containerProperty, System.String id) <0x27f8df0 + 0x000c8> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, System.Object existingValue, System.String id) <0x27eac60 + 0x001ac> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) <0x27bce40 + 0x000d0> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ResolvePropertyAndCreatorValues (Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty containerProperty, Newtonsoft.Json.JsonReader reader, System.Type objectType) <0x27cd020 + 0x00270> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObjectUsingCreatorWithParameters (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty containerProperty, Newtonsoft.Json.Serialization.ObjectConstructor`1[T] creator, System.String id) <0x27d4980 + 0x00142> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonObjectContract objectContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, Newtonsoft.Json.Serialization.JsonProperty containerProperty, System.String id, System.Boolean& createdFromNonDefaultCreator) <0x27c0418 + 0x0006a> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) <0x27bdb80 + 0x002c2> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) <0x27bce40 + 0x000b0> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList (System.Collections.IList list, Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonArrayContract contract, Newtonsoft.Json.Serialization.JsonProperty containerProperty, System.String id) <0x27f8df0 + 0x0034e> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, System.Object existingValue, System.String id) <0x27eac60 + 0x001ac> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) <0x27bce40 + 0x000d0> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ResolvePropertyAndCreatorValues (Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty containerProperty, Newtonsoft.Json.JsonReader reader, System.Type objectType) <0x27cd020 + 0x00270> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObjectUsingCreatorWithParameters (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty containerProperty, Newtonsoft.Json.Serialization.ObjectConstructor`1[T] creator, System.String id) <0x27d4980 + 0x00142> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonObjectContract objectContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, Newtonsoft.Json.Serialization.JsonProperty containerProperty, System.String id, System.Boolean& createdFromNonDefaultCreator) <0x27c0418 + 0x0006a> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) <0x27bdb80 + 0x002c2> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) <0x27bce40 + 0x000b0> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) <0x260daa8 + 0x001b0> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) <0x25f1928 + 0x000ac> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) <0x25f1258 + 0x00010> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) <0x25f84e8 + 0x00076> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) <0x25f7c70 + 0x0000c> in <12891e825fce44a581e5bbbb579c1d49>:0 
blazor.webassembly.js:1 WASM:   at Localization.Jliff.Graph.Converter.Deserialize (System.String json) [0x0006f] in E:\Dev\DotNet\JliffEditor\JliffEditor.Jliff\Serialization\Converter.cs:76 
blazor.webassembly.js:1 WASM:   at JliffEditor.Client.Pages.Editor.OnInitAsync () [0x00096] in E:\Dev\DotNet\JliffEditor\JliffEditor.Client\Pages\Editor.razor:46 
blazor.webassembly.js:1 WASM:   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync () <0x206d928 + 0x00160> in <13ab8f8dacb6489b93c9655168c56037>:0 
blazor.webassembly.js:1 WASM:   at Microsoft.AspNetCore.Components.Rendering.Renderer.GetErrorHandledTask (System.Threading.Tasks.Task taskToHandle) <0x224efa0 + 0x000fe> in <13ab8f8dacb6489b93c9655168c56037>:0 

对于解决该问题的任何建议,我将不胜感激。

编辑

考虑到我可能会切换Json序列化程序,我通过以下链接访问了Announcing .NET Core 3.0 Preview 5System.Text.Json Docs,这表明不支持多态行为。为了寻找其他选择,也许TinyJSON ...

EDIT II

TinyJSON工作到一定程度。在传输过程中,数据包含额外的@type属性,这在我的纯粹观点上是可观的,但是,嘿。如果有一个简单的解决方案可以使用Newtonsoft,那么这个问题就没有答案了。

1 个答案:

答案 0 :(得分:1)

您可以通过在客户端代码中包含对转换器类的引用来解决问题。

我在Blazor客户端中对JSON反序列化有类似的问题。就我而言,“ {xxx}类型的构造函数未找到”错误是针对Int32ConverterStringConverter类的。在客户端代码中包含对每个类的引用,使我能够克服问题(我将它们放在ConfigureServices方法中)。

似乎代码优化器急于去除未使用的类/方法。

几个月前,我为此创建了一个issue,但迄今为止还没有动静。如果您有兴趣,请在问题的说明中提供更多详细信息。

顺便说一句,在此之前,我还遇到过其他类似的问题,这些问题很难破解。在写这篇文章时,我在System.Math.Min上遇到MissingMethodException问题。