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



启动了一个新的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)

服务器端(返回正确(:

[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="....DomainDomain.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="....DataAccessDataAccess.csproj" />
<ProjectReference Include="..ClientTeamAppManager.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; }

}

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

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

删除set访问器并运行代码。

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

private string _Name
public string Name
{
get 
{
//...
return this._Name;

}
set
{
this._Name = value;
//...
}
}

WebAssembly的混合版本。

  • 3.2.1
  • 5.0.0-审查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更改之间清除应用程序缓存。

最新更新