Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Create an access an API from blazor server in blazor client using .net 8 auto project

$
0
0

I'm trying to access a function in my c# code that resides on the server from the client. I have a controller on the server that looks like this:

using LottoPicker.Models;using Microsoft.AspNetCore.Http;using Microsoft.AspNetCore.Mvc;namespace LottoPicker.Controllers.Lotto;[Route("api/Lotto")][ApiController]public class LottoGeneratorController : ControllerBase{    [HttpGet]    public ActionResult<List<List<int>>> GetCombinations()    {        LottoGenerator lottoGenerator = new LottoGenerator();        return lottoGenerator.GetCombinations();    }}

My razor page on the client side:

@page "/lotto"@using System.Net.Http@inject HttpClient Http@rendermode InteractiveAuto<h3>Lotto</h3>@code {    List<List<int>>? combinations;    protected override async Task OnInitializedAsync()    {        combinations = await Http.GetFromJsonAsync<List<List<int>>>("api/Lotto");    }}

And then my program.cs for the client:

using LottoPicker.Client.Pages;using Microsoft.AspNetCore.Components.WebAssembly.Hosting;var builder = WebAssemblyHostBuilder.CreateDefault(args);builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });await builder.Build().RunAsync();

But when I go to the lotto page I get the error:

InvalidOperationException: Cannot provide a value for property 'Http'on type 'LottoPicker.Client.Pages.Lotto'. There is no registeredservice of type 'System.Net.Http.HttpClient'.Microsoft.AspNetCore.Components.ComponentFactory+<>c__DisplayClass9_0.g__Initialize|1(IServiceProviderserviceProvider, IComponent component)

And some extra error info:

System.InvalidOperationException: Cannot provide a value for property'Http' on type 'LottoPicker.Client.Pages.Lotto'. There is noregistered service of type 'System.Net.Http.HttpClient'. atMicrosoft.AspNetCore.Components.ComponentFactory.<>c__DisplayClass9_0.g__Initialize|1(IServiceProviderserviceProvider, IComponent component) atMicrosoft.AspNetCore.Components.ComponentFactory.InstantiateComponent(IServiceProviderserviceProvider, Type componentType, IComponentRenderModecallerSpecifiedRenderMode, Nullable1 parentComponentId) at Microsoft.AspNetCore.Components.RenderTree.Renderer.InstantiateChildComponentOnFrame(RenderTreeFrame[] frames, Int32 frameIndex, Int32 parentComponentId) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InitializeNewComponentFrame(DiffContext& diffContext, Int32 frameIndex) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InitializeNewSubtree(DiffContext& diffContext, Int32 frameIndex) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InsertNewFrame(DiffContext& diffContext, Int32 newFrameIndex) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForRange(DiffContext& diffContext, Int32 oldStartIndex, Int32 oldEndIndexExcl, Int32 newStartIndex, Int32 newEndIndexExcl) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.ComputeDiff(Renderer renderer, RenderBatchBuilder batchBuilder, Int32 componentId, ArrayRange1 oldTree, ArrayRange`1 newTree) atMicrosoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilderbatchBuilder, RenderFragment renderFragment, Exception&renderFragmentException) atMicrosoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()--- End of stack trace from previous location --- at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()atMicrosoft.AspNetCore.Components.RenderTree.Renderer.AddToRenderQueue(Int32componentId, RenderFragment renderFragment) atMicrosoft.AspNetCore.Components.ComponentBase.StateHasChanged() atMicrosoft.AspNetCore.Components.ComponentBase.CallOnParametersSetAsync()atMicrosoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()atMicrosoft.AspNetCore.Components.Rendering.ComponentState.SetDirectParameters(ParameterViewparameters) atMicrosoft.AspNetCore.Components.RenderTree.Renderer.RenderRootComponentAsync(Int32componentId, ParameterView initialParameters) atMicrosoft.AspNetCore.Components.HtmlRendering.Infrastructure.StaticHtmlRenderer.BeginRenderingComponent(IComponentcomponent, ParameterView initialParameters) atMicrosoft.AspNetCore.Components.Endpoints.EndpointHtmlRenderer.RenderEndpointComponent(HttpContexthttpContext, Type rootComponentType, ParameterView parameters, BooleanwaitForQuiescence) atMicrosoft.AspNetCore.Components.Endpoints.RazorComponentEndpointInvoker.RenderComponentCore(HttpContextcontext) atMicrosoft.AspNetCore.Components.Endpoints.RazorComponentEndpointInvoker.RenderComponentCore(HttpContextcontext) atMicrosoft.AspNetCore.Components.Rendering.RendererSynchronizationContext.<>c.<b__10_0>d.MoveNext()--- End of stack trace from previous location --- at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContextcontext) atMicrosoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContextcontext)

I'm not quite sure where to go from here, any help would be appreciated.


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>