Repository navigation
Closed
Closed
Copy link
Description
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:
- Create Blazor project
- Add Test class to it
public class Test
{
public int X { get; set; }
public int Y { get; set; }
}- In
Counter.razoradd two lines toIncrementCount():
string json = JsonSerializer.Serialize(new Test { Y = 1, X = 2 });
Console.WriteLine(json);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
layomia commentedon 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 commentedon Jul 13, 2021
cc @pranavkm is this sort of issue familiar to you? Do you know why this method call could be failing with
MethodAccessExceptionin a blazorwasm app?romfir commentedon Jul 13, 2021
Same error with System.Text.Json 6.0.0-preview.7.21362.11:

layomia commentedon Jul 13, 2021
@romfir does it repro when you disable trimming in the csproj?
romfir commentedon Jul 13, 2021
It does repro, but I don't think it matter, because I'm launching it via F5 with debug, without publishing
layomia commentedon Jul 13, 2021
Thanks, I'll take a closer look here.
stephtr commentedon 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 commentedon Aug 4, 2021
@stepht does the newer SDK fix the issue on Blazor? If so, this issue should be closed. Thanks
stephtr commentedon Aug 4, 2021
My issue is fixed, I don't know what's with @romfir.
romfir commentedon Aug 4, 2021
using rc.1 builds it no longer throws.