Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
romfir opened this issue Jul 13, 2021 · 11 comments
Closed
Assignees
Milestone

Comments

@romfir
Copy link

romfir commented Jul 13, 2021

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>

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Jul 13, 2021
@ghost
Copy link

ghost commented Jul 13, 2021

Tagging subscribers to this area: @eiriktsarpalis, @layomia
See info in area-owners.md if you want to be subscribed.

Issue Details

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>

Author: romfir
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@layomia
Copy link
Contributor

layomia commented Jul 13, 2021

Thanks for opening this issue, @romfir. Did you happen to check if the issue still repros when using a slightly earlier build of System.Text.Json that doesn't contain the changes from #54526, e.g this build?

@layomia
Copy link
Contributor

layomia commented Jul 13, 2021

cc @pranavkm is this sort of issue familiar to you? Do you know why this method call could be failing with MethodAccessException in a blazorwasm app?

@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Jul 13, 2021
@layomia layomia self-assigned this Jul 13, 2021
@layomia layomia added this to the 6.0.0 milestone Jul 13, 2021
@layomia layomia added the bug label Jul 13, 2021
@romfir
Copy link
Author

romfir commented Jul 13, 2021

Thanks for opening this issue, @romfir. Did you happen to check if the issue still repros when using a slightly earlier build of System.Text.Json that doesn't contain the changes from #54526, e.g this build?

Same error with System.Text.Json 6.0.0-preview.7.21362.11:
image

@layomia
Copy link
Contributor

layomia commented Jul 13, 2021

@romfir does it repro when you disable trimming in the csproj?

<PropertyGroup>
    <PublishTrimmed>false</PublishTrimmed>
</PropertyGroup>

@romfir
Copy link
Author

romfir commented Jul 13, 2021

<PublishTrimmed>false</PublishTrimmed>

It does repro, but I don't think it matter, because I'm launching it via F5 with debug, without publishing

@layomia
Copy link
Contributor

layomia commented Jul 13, 2021

Thanks, I'll take a closer look here.

@stephtr
Copy link

stephtr commented Jul 17, 2021

It's actually not only failing in Blazor, I also get this issue for an ordinary MVC/WebAPI project.
Forget that, on my second development machine I accidentally still had the preview 4 SDK installed, despite referencing the preview 6 packages. With updating the SDK, I got rid of that exception.

@steveharter
Copy link
Member

@stepht does the newer SDK fix the issue on Blazor? If so, this issue should be closed. Thanks

@stephtr
Copy link

stephtr commented Aug 4, 2021

My issue is fixed, I don't know what's with @romfir.

@romfir
Copy link
Author

romfir commented Aug 4, 2021

using rc.1 builds it no longer throws.

@romfir romfir closed this as completed Aug 4, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Sep 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants