Class relationships in dependacy injection.

Jonathan Williams 1 Reputation point
2021-11-04T18:46:05.987+00:00

@Having issues regarding class relationships, So @Sch.Name works fine, but Sch.Level.Rank i get an error@
@Anonymous "/School/{id:int}"
@inject IValues Values

<h3>GetSchool</h3>
@if(Sch!=null)
{

<table class="table">  
    <thead>  
        <tr>  
            <td>School</td>  
            <td>Address</td>  
            <td>Telephone#</td>  
            <td>Level</td>  
        </tr>  
    </thead>  
    <tbody>  
        <tr>  
            <td>@Sch.Name</td>  
            <td>@Sch.Address</td>  
            <td>@Sch.TelephoneNumber</td>  
            <td>@Sch.Levels.Rank  
                @*<InputRadioGroup @bind-Value="SchoolId">  
                    @foreach (var Rank in Values.Levels)  
                        {  
                        <label>@Rank.Rank</label><br/><InputRadio Value="@Rank.Id"></InputRadio><br />  
                        }  
                </InputRadioGroup>*@  
            </td>  
        </tr>  
    </tbody>  
</table>  

}
else
{

<h3>Loading</h3>
}

@Aidan Wick
{
[Parameter]
public int Id { get; set; }

public WebApplication.Shared.School Sch { get; set; } = new WebApplication.Shared.School();  
public List<WebApplication.Shared.Level> Lvl= new List<WebApplication.Shared.Level>();  
  

protected async override Task OnInitializedAsync()  
{  

    Sch = await Values.GetSchool(Id);  
    await Values.GetLevel();  
      


}  
public async void HandleSubmit()  
{  
    //Sch.Levels = Values.Levels.FirstOrDefault(x=> x.Id==Id);  


}  

}

Developer technologies | ASP.NET | ASP.NET Core
{count} votes

3 answers

Sort by: Most helpful
  1. Jonathan Williams 1 Reputation point
    2021-11-04T21:09:54.803+00:00

    //This is the error
    GET https://localhost:44300/api/Values/levels/ net::ERR_ABORTED 400
    _mono_wasm_invoke_js_with_args @ dotnet.5.0.11.js:1
    $do_icall @ 00aba242:0x194ddb
    $do_icall_wrapper @ 00aba242:0x79df9
    $interp_exec_method @ 00aba242:0x44ad3
    $interp_runtime_invoke @ 00aba242:0x12efff
    $mono_jit_runtime_invoke @ 00aba242:0x118e5f
    $do_runtime_invoke @ 00aba242:0x79d42
    $mono_runtime_try_invoke @ 00aba242:0x12982
    $mono_runtime_invoke @ 00aba242:0x10ec2b
    $mono_wasm_invoke_method @ 00aba242:0x108e48
    Module._mono_wasm_invoke_method @ dotnet.5.0.11.js:1
    call_method @ dotnet.5.0.11.js:1
    set_task_result @ dotnet.5.0.11.js:1
    (anonymous) @ dotnet.5.0.11.js:1
    Promise.then (async)
    js_to_mono_obj @ dotnet.5.0.11.js:1
    mono_wasm_convert_return_value @ dotnet.5.0.11.js:1
    _mono_wasm_invoke_js_with_args @ dotnet.5.0.11.js:1
    $do_icall @ 00aba242:0x194ddb
    $do_icall_wrapper @ 00aba242:0x79df9
    $interp_exec_method @ 00aba242:0x44ad3
    $interp_runtime_invoke @ 00aba242:0x12efff
    $mono_jit_runtime_invoke @ 00aba242:0x118e5f
    $do_runtime_invoke @ 00aba242:0x79d42
    $mono_runtime_try_invoke @ 00aba242:0x12982
    $mono_runtime_invoke @ 00aba242:0x10ec2b
    $mono_wasm_invoke_method @ 00aba242:0x108e48
    Module._mono_wasm_invoke_method @ dotnet.5.0.11.js:1
    call_method @ dotnet.5.0.11.js:1
    set_task_result @ dotnet.5.0.11.js:1
    (anonymous) @ dotnet.5.0.11.js:1
    Promise.then (async)
    js_to_mono_obj @ dotnet.5.0.11.js:1
    mono_wasm_convert_return_value @ dotnet.5.0.11.js:1
    _mono_wasm_invoke_js_with_args @ dotnet.5.0.11.js:1
    $do_icall @ 00aba242:0x194ddb
    $do_icall_wrapper @ 00aba242:0x79df9
    $interp_exec_method @ 00aba242:0x44ad3
    $interp_runtime_invoke @ 00aba242:0x12efff
    $mono_jit_runtime_invoke @ 00aba242:0x118e5f
    $do_runtime_invoke @ 00aba242:0x79d42
    $mono_runtime_try_invoke @ 00aba242:0x12982
    $mono_runtime_invoke @ 00aba242:0x10ec2b
    $mono_wasm_invoke_method @ 00aba242:0x108e48
    Module._mono_wasm_invoke_method @ dotnet.5.0.11.js:1
    call_method @ dotnet.5.0.11.js:1
    set_task_result @ dotnet.5.0.11.js:1
    (anonymous) @ dotnet.5.0.11.js:1
    Promise.then (async)
    js_to_mono_obj @ dotnet.5.0.11.js:1
    d.preRun.push.window.Blazor._internal.getSatelliteAssemblies @ blazor.webassembly.js:1
    w @ blazor.webassembly.js:1
    _mono_wasm_invoke_js_blazor @ dotnet.5.0.11.js:1
    $do_icall @ 00aba242:0x194e4e
    $do_icall_wrapper @ 00aba242:0x79df9
    $interp_exec_method @ 00aba242:0x44ad3
    $interp_runtime_invoke @ 00aba242:0x12efff
    $mono_jit_runtime_invoke @ 00aba242:0x118e5f
    $do_runtime_invoke @ 00aba242:0x79d42
    $mono_runtime_invoke_checked @ 00aba242:0xf65d
    $mono_runtime_try_invoke_array @ 00aba242:0x10e831
    $ves_icall_InternalInvoke @ 00aba242:0xed492
    $ves_icall_InternalInvoke_raw @ 00aba242:0xecf57
    $do_icall @ 00aba242:0x194ddb
    $do_icall_wrapper @ 00aba242:0x79df9
    $interp_exec_method @ 00aba242:0x44ad3
    $interp_runtime_invoke @ 00aba242:0x12efff
    $mono_jit_runtime_invoke @ 00aba242:0x118e5f
    $do_runtime_invoke @ 00aba242:0x79d42
    $mono_runtime_try_invoke @ 00aba242:0x12982
    $mono_runtime_invoke @ 00aba242:0x10ec2b
    $mono_wasm_invoke_method @ 00aba242:0x108e48
    Module._mono_wasm_invoke_method @ dotnet.5.0.11.js:1
    call_method @ dotnet.5.0.11.js:1
    (anonymous) @ dotnet.5.0.11.js:1
    callEntryPoint @ blazor.webassembly.js:1
    (anonymous) @ blazor.webassembly.js:1
    (anonymous) @ blazor.webassembly.js:1
    (anonymous) @ blazor.webassembly.js:1
    a @ blazor.webassembly.js:1
    Promise.then (async)
    u @ blazor.webassembly.js:1
    a @ blazor.webassembly.js:1
    Promise.then (async)
    u @ blazor.webassembly.js:1
    a @ blazor.webassembly.js:1
    Promise.then (async)
    u @ blazor.webassembly.js:1
    (anonymous) @ blazor.webassembly.js:1
    r @ blazor.webassembly.js:1
    g @ blazor.webassembly.js:1
    (anonymous) @ blazor.webassembly.js:1
    n @ blazor.webassembly.js:1
    (anonymous) @ blazor.webassembly.js:1
    (anonymous) @ blazor.webassembly.js:1
    blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
    Unhandled exception rendering component: Response status code does not indicate success: 400 (Bad Request).
    System.Net.Http.HttpRequestException: Response status code does not indicate success: 400 (Bad Request).
    at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
    at System.Net.Http.Json.HttpClientJsonExtensions.<GetFromJsonAsyncCore>d__91[[System.Collections.Generic.List1[[WebApplication.Shared.Level, WebApplication.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
    at WebApplication.Client.Services.Values.GetLevel() in C:\Users\jxcwi\source\repos\WebApplication\WebApplication\Client\Services\Values.cs:line 45
    at WebApplication.Client.Pages.School.OnInitializedAsync() in C:\Users\jxcwi\source\repos\WebApplication\WebApplication\Client\Pages\School.razor:line 57
    at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
    at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)
    d.printErr @ blazor.webassembly.js:1
    d.preRun.push.window.Blazor._internal.dotNetCriticalError @ blazor.webassembly.js:1
    w @ blazor.webassembly.js:1
    _mono_wasm_invoke_js_blazor @ dotnet.5.0.11.js:1
    $do_icall @ 00aba242:0x194e4e
    $do_icall_wrapper @ 00aba242:0x79df9
    $interp_exec_method @ 00aba242:0x44ad3
    $interp_runtime_invoke @ 00aba242:0x12efff
    $mono_jit_runtime_invoke @ 00aba242:0x118e5f
    $do_runtime_invoke @ 00aba242:0x79d42
    $mono_runtime_try_invoke @ 00aba242:0x12982
    $mono_runtime_invoke @ 00aba242:0x10ec2b
    $mono_wasm_invoke_method @ 00aba242:0x108e48
    Module._mono_wasm_invoke_method @ dotnet.5.0.11.js:1
    call_method @ dotnet.5.0.11.js:1
    set_task_result @ dotnet.5.0.11.js:1
    (anonymous) @ dotnet.5.0.11.js:1
    Promise.then (async)
    js_to_mono_obj @ dotnet.5.0.11.js:1
    mono_wasm_convert_return_value @ dotnet.5.0.11.js:1
    _mono_wasm_invoke_js_with_args @ dotnet.5.0.11.js:1
    $do_icall @ 00aba242:0x194ddb
    $do_icall_wrapper @ 00aba242:0x79df9
    $interp_exec_method @ 00aba242:0x44ad3
    $interp_runtime_invoke @ 00aba242:0x12efff
    $mono_jit_runtime_invoke @ 00aba242:0x118e5f
    $do_runtime_invoke @ 00aba242:0x79d42
    $mono_runtime_try_invoke @ 00aba242:0x12982
    $mono_runtime_invoke @ 00aba242:0x10ec2b
    $mono_wasm_invoke_method @ 00aba242:0x108e48
    Module._mono_wasm_invoke_method @ dotnet.5.0.11.js:1
    call_method @ dotnet.5.0.11.js:1
    set_task_result @ dotnet.5.0.11.js:1
    (anonymous) @ dotnet.5.0.11.js:1
    Promise.then (async)
    js_to_mono_obj @ dotnet.5.0.11.js:1
    mono_wasm_convert_return_value @ dotnet.5.0.11.js:1
    _mono_wasm_invoke_js_with_args @ dotnet.5.0.11.js:1
    $do_icall @ 00aba242:0x194ddb
    $do_icall_wrapper @ 00aba242:0x79df9
    $interp_exec_method @ 00aba242:0x44ad3
    $interp_runtime_invoke @ 00aba242:0x12efff
    $mono_jit_runtime_invoke @ 00aba242:0x118e5f
    $do_runtime_invoke @ 00aba242:0x79d42
    $mono_runtime_try_invoke @ 00aba242:0x12982
    $mono_runtime_invoke @ 00aba242:0x10ec2b
    $mono_wasm_invoke_method @ 00aba242:0x108e48
    Module._mono_wasm_invoke_method @ dotnet.5.0.11.js:1
    call_method @ dotnet.5.0.11.js:1
    set_task_result @ dotnet.5.0.11.js:1
    (anonymous) @ dotnet.5.0.11.js:1
    Promise.then (async)
    js_to_mono_obj @ dotnet.5.0.11.js:1
    mono_wasm_convert_return_value @ dotnet.5.0.11.js:1
    _mono_wasm_invoke_js_with_args @ dotnet.5.0.11.js:1
    $do_icall @ 00aba242:0x194ddb
    $do_icall_wrapper @ 00aba242:0x79df9
    $interp_exec_method @ 00aba242:0x44ad3
    $interp_runtime_invoke @ 00aba242:0x12efff
    $mono_jit_runtime_invoke @ 00aba242:0x118e5f
    $do_runtime_invoke @ 00aba242:0x79d42
    $mono_runtime_try_invoke @ 00aba242:0x12982
    $mono_runtime_invoke @ 00aba242:0x10ec2b
    $mono_wasm_invoke_method @ 00aba242:0x108e48
    Module._mono_wasm_invoke_method @ dotnet.5.0.11.js:1
    call_method @ dotnet.5.0.11.js:1
    set_task_result @ dotnet.5.0.11.js:1
    (anonymous) @ dotnet.5.0.11.js:1
    Promise.then (async)
    js_to_mono_obj @ dotnet.5.0.11.js:1
    d.preRun.push.window.Blazor._internal.getSatelliteAssemblies @ blazor.webassembly.js:1
    w @ blazor.webassembly.js:1
    _mono_wasm_invoke_js_blazor @ dotnet.5.0.11.js:1
    $do_icall @ 00aba242:0x194e4e
    $do_icall_wrapper @ 00aba242:0x79df9
    $interp_exec_method @ 00aba242:0x44ad3
    $interp_runtime_invoke @ 00aba242:0x12efff
    $mono_jit_runtime_invoke @ 00aba242:0x118e5f
    $do_runtime_invoke @ 00aba242:0x79d42
    $mono_runtime_invoke_checked @ 00aba242:0xf65d
    $mono_runtime_try_invoke_array @ 00aba242:0x10e831
    $ves_icall_InternalInvoke @ 00aba242:0xed492
    $ves_icall_InternalInvoke_raw @ 00aba242:0xecf57
    $do_icall @ 00aba242:0x194ddb
    $do_icall_wrapper @ 00aba242:0x79df9
    $interp_exec_method @ 00aba242:0x44ad3
    $interp_runtime_invoke @ 00aba242:0x12efff
    $mono_jit_runtime_invoke @ 00aba242:0x118e5f
    $do_runtime_invoke @ 00aba242:0x79d42
    $mono_runtime_try_invoke @ 00aba242:0x12982
    $mono_runtime_invoke @ 00aba242:0x10ec2b
    $mono_wasm_invoke_method @ 00aba242:0x108e48
    Module._mono_wasm_invoke_method @ dotnet.5.0.11.js:1
    call_method @ dotnet.5.0.11.js:1
    (anonymous) @ dotnet.5.0.11.js:1
    callEntryPoint @ blazor.webassembly.js:1
    (anonymous) @ blazor.webassembly.js:1
    (anonymous) @ blazor.webassembly.js:1
    (anonymous) @ blazor.webassembly.js:1
    a @ blazor.webassembly.js:1
    Promise.then (async)
    u @ blazor.webassembly.js:1
    a @ blazor.webassembly.js:1
    Promise.then (async)
    u @ blazor.webassembly.js:1
    a @ blazor.webassembly.js:1
    Promise.then (async)
    u @ blazor.webassembly.js:1
    (anonymous) @ blazor.webassembly.js:1
    r @ blazor.webassembly.js:1
    g @ blazor.webassembly.js:1
    (anonymous) @ blazor.webassembly.js:1
    n @ blazor.webassembly.js:1
    (anonymous) @ blazor.webassembly.js:1
    (anonymous) @ blazor.webassembly.js:1


  2. AgaveJoe 30,126 Reputation points
    2021-11-04T23:15:33.357+00:00

    You did not share all the relevant code which makes a code review difficult. A 400 error mean there is a problem with client's HTTP request.

    Below is a working Blazor WASM example that calls web API. Compare the programming pattern to your code to figure out what's wrong.

    Register a named HttpClient service and the Values service in Program.cs.

    public class Program  
    {  
        public static async Task Main(string[] args)  
        {  
            var builder = WebAssemblyHostBuilder.CreateDefault(args);  
            builder.RootComponents.Add<App>("#app");  
      
            var RemoteApi = builder.Configuration.GetValue<string>("ApiUrl:RemoteApi");  
      
            builder.Services.AddHttpClient("RemoteApi",   
                client => client.BaseAddress = new Uri(RemoteApi));  
      
            builder.Services.AddScoped<IValues, Values>();  
      
            await builder.Build().RunAsync();  
        }  
    }  
    

    appsetting.json in the wwwroot

    {  
      "ApiUrl": {  
        "RemoteApi": "https://localhost:44352"  
      }  
    }  
    

    Values service

    public interface IValues  
    {  
        Task<DateTime[]> GetDatesAsync();  
    }  
      
    public class Values : IValues  
    {  
        private readonly IHttpClientFactory _clientFactory;  
        public Values(IHttpClientFactory ClientFactory)  
        {  
            _clientFactory = ClientFactory;  
        }  
      
        public async Task<DateTime[]> GetDatesAsync()  
        {  
            var client = _clientFactory.CreateClient("RemoteApi");  
            return await client.GetFromJsonAsync<DateTime[]>("api/Time/AllDate/?startdate=7/1/2021&enddate=7/31/2021");  
        }  
    }  
    

    Implementation

    @page "/DateEx"  
    @inject BlazorWasm.Services.IValues valuesService   
    <h3>DateEx</h3>  
      
    @if (dates == null)  
    {  
        <p><em>Loading...</em></p>  
    }  
    else  
    {  
        <div>  
            @dates[0]  
        </div>  
        <div>  
            @dates[1]  
        </div>  
    }  
      
    @code {  
        private DateTime[] dates;  
      
        protected override async Task OnInitializedAsync()  
        {  
            dates = await valuesService.GetDatesAsync();  
        }  
    }  
    

    JSON result

    [  
      "2021-07-01T00:00:00",  
      "2021-07-31T00:00:00"  
    ]  
    

    Reference documentation.

    Call a web API from ASP.NET Core Blazor
    ASP.NET Core Blazor dependency injection


  3. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2021-11-09T18:29:30.487+00:00

    when blazor client calls HttpClient:

         public async Task GetLevel()
         {
             Levels= await httpClient.GetFromJsonAsync<List<Level>>("api/Values/levels/");
    
         }
    

    it is really a javascript invoke of the fetch api. in your case the url (which is a get):

    https://localhost:44300/api/Values/levels/

    is returning a 400 error. this is a bad request, which generally means the url, method and parameters don't match the routing. we would need to see the server code for this url to see why the error is generated.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.