Skip to content

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

Closed

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>

Activity

  1. ghost added
    untriaged
    on Jul 13, 2021
  2. layomia commented on Jul 13, 2021

    @layomia
    Contributor

    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?

  3. layomia commented on Jul 13, 2021

    @layomia
    Contributor

    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?

  4. removed
    untriaged
    on Jul 13, 2021
  5. self-assigned this
    on Jul 13, 2021
  6. added this to the 6.0.0 milestone on Jul 13, 2021
  7. romfir commented on Jul 13, 2021

    @romfir
    Author

    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

  8. layomia commented on Jul 13, 2021

    @layomia
    Contributor

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

    <PropertyGroup>
        <PublishTrimmed>false</PublishTrimmed>
    </PropertyGroup>
  9. romfir commented on Jul 13, 2021

    @romfir
    Author
    <PublishTrimmed>false</PublishTrimmed>
    

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

  10. layomia commented on Jul 13, 2021

    @layomia
    Contributor

    Thanks, I'll take a closer look here.

  11. stephtr commented on Jul 17, 2021

    @stephtr

    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.

  12. steveharter commented on Aug 4, 2021

    @steveharter
    Contributor

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

  13. stephtr commented on Aug 4, 2021

    @stephtr

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

  14. romfir commented on Aug 4, 2021

    @romfir
    Author

    using rc.1 builds it no longer throws.

  15. ghost locked as resolved and limited conversation to collaborators on Sep 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions