Skip to content

JsonSerializer.Serialize stopped working with Blazor, throws MethodAccessException #55568

@romfir

Description

@romfir

Description

When calling JsonSerializer.Serialize code throws MethodAccessException:

Uncaught (in promise) Error: System.MethodAccessException: Method `System.String.CopyTo(System.Span`1<char>)' is inaccessible from method `System.Text.Json.JsonCamelCaseNamingPolicy+<>c.<ConvertName>b__0_0(System.Span`1<char>,string)'
   at System.Text.Json.JsonCamelCaseNamingPolicy.<>c.<ConvertName>b__0_0(Span`1 chars, String name) in System.Text.Json.dll:token 0x6000140+0x0
   at System.String.Create[String](Int32 length, String state, SpanAction`2 action) in System.Private.CoreLib.dll:token 0x60004fc+0x2d
   at System.Text.Json.JsonCamelCaseNamingPolicy.ConvertName(String name) in System.Text.Json.dll:token 0x600013b+0x18
   at System.Text.Json.Serialization.Metadata.JsonPropertyInfo.DeterminePropertyName() in System.Text.Json.dll:token 0x6000832+0x4c
   at System.Text.Json.Serialization.Metadata.JsonPropertyInfo.GetPolicies(Nullable`1 ignoreCondition, Nullable`1 declaringTypeNumberHandling) in System.Text.Json.dll:token 0x6000831+0x17
   at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Initialize(Type parentClassType, Type declaredPropertyType, Type runtimePropertyType, ConverterStrategy runtimeClassType, MemberInfo memberInfo, Boolean isVirtual, JsonConverter converter, Nullable`1 ignoreCondition, Nullable`1 parentTypeNumberHandling, JsonSerializerOptions options) in System.Text.Json.dll:token 0x600087b+0x15e
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo.CreateProperty(Type declaredPropertyType, Type runtimePropertyType, MemberInfo memberInfo, Type parentClassType, Boolean isVirtual, JsonConverter converter, JsonSerializerOptions options, Nullable`1 parentTypeNumberHandling, Nullable`1 ignoreCondition) in System.Text.Json.dll:token 0x60008b2+0x8
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo.AddProperty(MemberInfo memberInfo, Type memberType, Type parentClassType, Boolean isVirtual, Nullable`1 parentTypeNumberHandling, JsonSerializerOptions options) in System.Text.Json.dll:token 0x60008b1+0x52
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo.CacheMember(Type declaringType, Type memberType, MemberInfo memberInfo, Boolean isVirtual, Nullable`1 typeNumberHandling, Dictionary`2& ignoredMembers) in System.Text.Json.dll:token 0x60008a4+0x34
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo..ctor(Type type, JsonConverter converter, Type runtimeType, JsonSerializerOptions options) in System.Text.Json.dll:token 0x60008a3+0x116
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo..ctor(Type type, JsonSerializerOptions options) in System.Text.Json.dll:token 0x60008a2+0x0
   at System.Text.Json.JsonSerializerOptions.<RootBuiltInConvertersAndTypeInfoCreator>g__CreateJsonTypeInfo|107_0(Type type, JsonSerializerOptions options) in System.Text.Json.dll:token 0x6000436+0x0
   at System.Text.Json.JsonSerializerOptions.GetClassFromContextOrCreate(Type type) in System.Text.Json.dll:token 0x600042c+0x29
   at System.Text.Json.JsonSerializerOptions.GetOrAddClass(Type type) in System.Text.Json.dll:token 0x600042b+0x17
   at System.Text.Json.JsonSerializerOptions.GetOrAddClassForRootType(Type type) in System.Text.Json.dll:token 0x600042d+0x1b
   at System.Text.Json.JsonSerializer.GetTypeInfo(Type runtimeType, JsonSerializerOptions options) in System.Text.Json.dll:token 0x6000387+0x10
   at System.Text.Json.JsonSerializer.ReadUsingOptions[Object](Utf8JsonReader& reader, Type returnType, JsonSerializerOptions options) in System.Text.Json.dll:token 0x60003ba+0x0
   at System.Text.Json.JsonSerializer.Deserialize(Utf8JsonReader& reader, Type returnType, JsonSerializerOptions options) in System.Text.Json.dll:token 0x60003b7+0x14
   at Microsoft.JSInterop.Infrastructure.DotNetDispatcher.ParseArguments(JSRuntime jsRuntime, String methodIdentifier, String arguments, Type[] parameterTypes) in Microsoft.JSInterop.dll:token 0x600004e+0xaa
   at Microsoft.JSInterop.Infrastructure.DotNetDispatcher.InvokeSynchronously(JSRuntime jsRuntime, DotNetInvocationInfo& callInfo, IDotNetObjectReference objectReference, String argsJson) in Microsoft.JSInterop.dll:token 0x600004d+0x6f
   at Microsoft.JSInterop.Infrastructure.DotNetDispatcher.BeginInvokeDotNet(JSRuntime jsRuntime, DotNetInvocationInfo invocationInfo, String argsJson) in Microsoft.JSInterop.dll:token 0x600004b+0x4c
    at Object.endInvokeDotNetFromJS (blazor.webassembly.js:1)
    at Object.ut [as endInvokeDotNetFromJS] (blazor.webassembly.js:1)
    at Object.lt [as invokeJSFromDotNet] (blazor.webassembly.js:1)
    at _mono_wasm_invoke_js_blazor (dotnet.6.0.0-preview.5.21301.5.js:1)
    at <anonymous>:wasm-function[10629]:0x1930bb
    at <anonymous>:wasm-function[2450]:0x5972b
    at <anonymous>:wasm-function[2175]:0x4541e
    at <anonymous>:wasm-function[7878]:0x12d0c6
    at <anonymous>:wasm-function[7356]:0x116d09
    at <anonymous>:wasm-function[3341]:0x79023

steps to reproduce:

  1. Create Blazor project
  2. Add Test class to it
public class Test
    {
        public int X { get; set; }

        public int Y { get; set; }
    }
  1. In Counter.razor add two lines to IncrementCount():
string json = JsonSerializer.Serialize(new Test { Y = 1, X = 2 });

Console.WriteLine(json);
  1. Launch project, go to Counter page and click the button
    image

Configuration

.NET version: 6.0.100-preview.5.21302.13

.csproj:

<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.0-preview.5.21301.17" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.0-preview.5.21301.17" PrivateAssets="all" />
    <PackageReference Include="System.Text.Json" Version="6.0.0-preview.7.21363.1" />
  </ItemGroup>

</Project>

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions