Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

GetAsync call in Blazor WebAssembly fails

$
0
0

I believe something which I don't understand about Blazor coming from Windows Forms development is happening here. I am trying to get data from an API which I know is working and delivering data correctly to another application. To me, it appears that the code is being terminated prior to allowing the async call to finish.

It crashed on the httpClient.GetAsync line. For testing purposes this call is running when the page loads with the OnInitializedAsync function.

I have followed 2 different tutorials but both yield the same result of the .GetAsync call failing: https://code-maze.com/blazor-webassembly-httpclient/ and https://www.youtube.com/watch?v=ffrGQwICEIY

Please help.

Thanks!

Service class:

public class RecipeService : IRecipeService{    private readonly HttpClient httpClient;    private string baseURL = clConstants.SERVER_PATH;    private string apiEndPoint = "getRecipeByID/";    public RecipeService(HttpClient httpClient)    {        this.httpClient = httpClient;    }    async Task<List<clRecipe>> IRecipeService.getRecipeByID(int id)    {        ConfigureHTTPClient();        string URL = clConstants.SERVER_PATH + apiEndPoint + id.ToString();        // Crashes on this line of code        var res = httpClient.GetAsync(URL).Result;     }}

Interface:

public interface IRecipeService{    //Task<IEnumerable<clRecipe>> getRecipeByID(int id);    Task<List<clRecipe>> getRecipeByID(int id);}

Function in the Razor page:

protected override async Task OnInitializedAsync(){    recipe = await RecipeService.getRecipeByID(currentCount);}

Program.cs (added the following line):

builder.Services.AddScoped<IRecipeService, RecipeService>();

UPDATED CODE

Interface

public class RecipeService : IRecipeService{    private readonly string apiEndPoint = "GetRecipeByID/";    private readonly IHttpClientFactory httpClientFactory;    private HttpClient CreateHttpClient() => httpClientFactory.CreateClient("MainApi");    private readonly JsonSerializerOptions jsonOptions = new()    {        PropertyNameCaseInsensitive = true    };    public RecipeService(IHttpClientFactory httpClientFactory)    {        this.httpClientFactory = httpClientFactory;    }    public async Task<List<clRecipe>>getRecipeByID(int id)    {        string URL = apiEndPoint + id.ToString();        var response = await CreateHttpClient().GetAsync(URL, HttpCompletionOption.ResponseHeadersRead);        var result = await response.Content.ReadAsStreamAsync();        var recipe = await JsonSerializer.DeserializeAsync<List<clRecipe>>(result, jsonOptions);        return recipe;    }}public interface IRecipeService{    Task<List<clRecipe>> getRecipeByID(int id);}

Program

builder.Services.AddHttpClient("MainApi", options =>{     options.BaseAddress = new Uri(clConstants.SERVER_PATH);});builder.Services.AddSingleton<IRecipeService, RecipeService>();

PAGE

@inject IRecipeService RecipeServiceprivate List<clRecipe> recipe { get; set; }public interface IRecipeService{    Task<List<clRecipe>> getRecipeByID(int id);}

Viewing all articles
Browse latest Browse all 4839

Trending Articles



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