如何修复 Blazor WASM 内存访问越界错误?

How to fix Blazor WASM memory access out of bounds error?

启动了一个新的 Blazor WebAssembly 项目(Windows 10,Visual Studio 2019), 尝试从服务器端通过 HttpClient 获取一个简单的列表。

错误:

00755c3a:0xa3a0 Uncaught (in promise) RuntimeError: memory access out of bounds
    at malloc (<anonymous>:wasm-function[359]:0xa3a0)
    at monoeg_malloc (<anonymous>:wasm-function[161]:0x3c71)
    at stack_frag_new (<anonymous>:wasm-function[2019]:0x59758)
    at add_frag (<anonymous>:wasm-function[1430]:0x3ccf2)
    at interp_exec_method (<anonymous>:wasm-function[1120]:0x2f609)
    at interp_runtime_invoke (<anonymous>:wasm-function[5655]:0xf7391)
    at mono_jit_runtime_invoke (<anonymous>:wasm-function[5109]:0xddb3d)
    at do_runtime_invoke (<anonymous>:wasm-function[1410]:0x3ba85)
    at mono_runtime_try_invoke (<anonymous>:wasm-function[418]:0xcfdb)
at mono_runtime_invoke (<anonymous>:wasm-function[1610]:0x44b39)

服务器端(这个 returns 正确):

    [HttpGet]
    public async Task<IEnumerable<UserDetailsRM>> Get()
    {
        var users = await UserService.GetUsers();
        return users;
    }

客户端:尽管返回了结果,但不呈现任何内容。

<div class="userListWrapper">
        @if (Users != null)
        {
            <table class="table table-borderless" style="font-size:12px; font-family:arial; height: 
                500px; display:block;   overflow-y:scroll">
                <thead>
                    <tr>
                        <th scope="col">Profile</th>
                        <th scope="col">Full Name</th>
                        <th scope="col">Gender</th>
                        <th scope="col">Age</th>

                    </tr>
                </thead>

                @foreach (var user in Users)
                {
                    <tbody class="scrollableTable" style="border-radius:15px;">
                        <tr class="userRow" >
                            <td><img src="@user.ProfileImageUrl" style="width:30px;border- 
                             radius:30px" /></td>
                            <td>@user.FullName</td>
                            <td>@user.Gender</td>
                            <td>@user.Age</td>
                        </tr>

                    </tbody>
                }
            </table>
        }
  </div>

@code{

private IEnumerable<UserDetailsRM> Users;
string LoggedEmail;
string UserId;
string UserActionMessage = "No Logged User";
string Wanted = "";
protected async override Task OnInitializedAsync()
{
    Users = await Http.GetFromJsonAsync<IEnumerable<UserDetailsRM>>("user");
}
}

客户端项目文件:

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

  <PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
    <RazorLangVersion>3.0</RazorLangVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.1" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="5.0.0-preview.6.20312.15" PrivateAssets="all" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.0-rc.2.20475.17" PrivateAssets="all" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
    <PackageReference Include="System.Net.Http.Json" Version="3.2.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\..\Domain\Domain.csproj" />
  </ItemGroup>
  
</Project>

服务器项目文件:

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="3.2.1" />
    <PackageReference Include="NetTopologySuite" Version="2.1.0" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\..\DataAccess\DataAccess.csproj" />
    <ProjectReference Include="..\Client\TeamAppManager.Client.csproj" />
  </ItemGroup>


</Project>

非常感谢任何帮助。谢谢。

编辑:模型:

public class UserDetailsRM :  IIntEntity
{
  
    public int Id { get; set; }
    public int UserDetailsId { get; set; }
    public string UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime Birthdate { get; set; }
    public int UserGenderOptionId { get; set; }
    private string gender;
    public string Gender
    {
        get { return gender; }
        set
        {
            if (value == "1")
                gender = "Male";

            else if (value == "2")
                gender = "Female";

            else
                gender = "Other";
        }
    }
    public string FullName
    {
        get 
        {
            return $"{FirstName} {LastName}";
        }
        set
        {
            FullName = value;
        }
    }

    public int Age
    {
        get
        {
            var today = DateTime.Today;
            var age = today.Year - Birthdate.Year;
            if (Birthdate.Date > today.AddYears(-age)) age--;
            return age;
        }
    }

    public string ProfileImageUrl { get; set; }

    
}

您的 WebAssembly.

混音版本
  • 3.2.1
  • 5.0.0-preview.6.20312.15
  • 5.0.0-rc.2.20475.17
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="5.0.0-preview.6.20312.15" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.0-rc.2.20475.17" PrivateAssets="all" />

对于 3.2.1

<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="3.2.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.1" PrivateAssets="all" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.Net.Http.Json" Version="3.2.0" />

对于 5.0 rc2

<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.0-rc.2.20475.17" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.0-rc.2.20475.17" PrivateAssets="all" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.Net.Http.Json" Version="5.0.0-rc.2.20475.5" />

当 WASM 在您的浏览器中运行时,您还应该在 dll 更改之间清除应用程序缓存。

我想问题出在这个 属性 上,它导致了错误

public string FullName
    {
        get 
        {
            return $"{FirstName} {LastName}";
        }
        set
        {
            FullName = value;
        }
    }

删除设置访问器和运行您的代码。

添加到以后类似的情况,它需要这样定义局部变量,否则它是一个循环引用,最终导致内存不足的问题:

    private string _Name
    public string Name
     {
      get 
       {
         //...
         return this._Name;
         
       }
      set
       {
         this._Name = value;
         //...
       }
     }