I’m doing a Blazor course and find myself stuck witha bug i can’t fix.
I just developed a controller that should be callable through API but I can’t seem to call that API. I used Postman to test it and am receiving an error message: "An unhandled error has occurred."
instead of a list of units (it’s a game). I believe it depends from a different bug that may make the list un-callable.It is my first experience with this language and I am finding it quite difficult to figure the issue or at least triage the issue.Especially because i have about 0 previous experience with .net or Java. The code create a web application (opens my browser). In the Console of the Browser I received the Error Messages:
crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Exception of type 'System.Exception' was thrown.
System.Exception: Exception of type 'System.Exception' was thrown.
at BlazorBattles.Client.Services.UnitService.LoadUnitsAsync() in C:UsersXXXsourcereposBlazorBattlesBlazorBattlesClientServicesUnitService.cs:line 37
at BlazorBattles.Client.Pages.Build.OnInitializedAsync() in C:UsersXXXsourcereposBlazorBattlesBlazorBattlesClientPagesBuild.razor:line 33
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
window.Module.s.printErr @ blazor.webassembly.js:1
Fe._internal.dotNetCriticalError @ blazor.webassembly.js:1
St @ blazor.webassembly.js:1
_mono_wasm_invoke_js_blazor @ dotnet.6.0.10.95x5mwwwq1.js:1
$func219 @ 00971d2a:0x1a492
$func167 @ 00971d2a:0xce60
$func166 @ 00971d2a:0xbd73
$func2815 @ 00971d2a:0xabebf
$func1619 @ 00971d2a:0x6fc80
$func1617 @ 00971d2a:0x6fbf2
$func970 @ 00971d2a:0x50643
$func219 @ 00971d2a:0x1a44b
$func167 @ 00971d2a:0xce60
$func166 @ 00971d2a:0xbd73
$func2815 @ 00971d2a:0xabebf
$func1619 @ 00971d2a:0x6fc80
$func1623 @ 00971d2a:0x702ed
$mono_wasm_invoke_method @ 00971d2a:0x969f
Module._mono_wasm_invoke_method @ dotnet.6.0.10.95x5mwwwq1.js:1
managed__Microsoft_AspNetCore_Components_WebAssembly__Microsoft_AspNetCore_Components_WebAssembly_Services_DefaultWebAssemblyJSRuntime_BeginInvokeDotNet @ managed__Microsoft_A…eginInvokeDotNet:19
beginInvokeDotNetFromJS @ blazor.webassembly.js:1
b @ blazor.webassembly.js:1
e.invokeMethodAsync @ blazor.webassembly.js:1
(anonym) @ blazor.webassembly.js:1
ve @ blazor.webassembly.js:1
we @ blazor.webassembly.js:1
(anonym) @ blazor.webassembly.js:1
(anonym) @ blazor.webassembly.js:1
onGlobalEvent
You can find all of my code in [Github] under https://github.com/maxbiocca/BlazorBattles/tree/master/BlazorBattles
It seams to be not happy with the expression "Task LoadUnitsAsync();" as part of the public interface IUnitService. I am not understanding where the issue is or where i am missing a reference since I do not get any error messages in my Visual Studio.
The issue appears also in UnitService. I copied the code of those 3 elements down below (UnitControler, IUnitService, Unitservice) The issue occurs also in two other "pages" that I didn’t attach.
I am stuck in this since 3 days and can’t find a way to move forward. I would love your help, thanks!
IUnitService
```
using BlazorBattles.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BlazorBattles.Client.Services
{
public interface IUnitService
{
IList<Unit> Units { get; }
IList<UserUnit> MyUnits { get; set; }
void AddUnit(int unitId);
Task LoadUnitsAsync();
}
}
```
UnitService
using BlazorBattles.Shared;
using System.Net.Http.Json;
using System.Linq;
namespace BlazorBattles.Client.Services
{
public class UnitService : IUnitService
{
// private readonly HttpClient _http;
//public UnitService(HttpClient http){
// _http = http;
//}
public IList<Unit> Units => new List<Unit>
{
new Unit() {Id=1, Title = "Knight", Attack=10, Defense=10, BananaCost=100},
new Unit() {Id=2, Title = "Archer", Attack=15, Defense=5, BananaCost=150},
new Unit() {Id=3, Title = "Mage", Attack=20, Defense=1, BananaCost=200}
};
public IList<UserUnit> MyUnits { get; set; } = new List<UserUnit>();
public void AddUnit(int unitId)
{
var unit = Units.First(unit => unit.Id == unitId);
MyUnits.Add(new UserUnit { UnitId = unit.Id, HitPoints = unit.HitPoints });
}
public Task LoadUnitsAsync()
{
throw new Exception();
}
//public async Task LoadUnitsAsync(){
//if (Units.Count == 0)
//{
// Units = await _http.GetFromJsonAsync<IList<Unit>>("api/Unit");
//}
//}
}
}
UnitControler
using BlazorBattles.Shared;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace BlazorBattles.Server.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class UnitController : ControllerBase
{
public IList<Unit> Units => new List<Unit>
{
new Unit() {Id=1, Title = "Knight", Attack=10, Defense=10, BananaCost=100},
new Unit() {Id=2, Title = "Archer", Attack=15, Defense=5, BananaCost=150},
new Unit() {Id=3, Title = "Mage", Attack=20, Defense=1, BananaCost=200}
};
// [HttpGet]
//public IActionResult GetUnits()
// {
// return Ok(Units);
// }
}
}
2
Answers
I’ve just looked at your code. Within
BlazorBattles/Client/Pages/Build.razor
you are callingLoadUnitsAsync();
within line 33.However this functions is implemented like this in your service:
Not directly related to this question, but Google led me to this post while searching for the error message:
For anybody who sees this post in the future and has a "Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]" error message:
In my case the problem was a missing pair of parantheses.
Not working:
Working:
Function:
Compiler didn´t notice a thing.