Der GetAsync-Aufruf in Blazor WebAssembly schlägt fehlC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Der GetAsync-Aufruf in Blazor WebAssembly schlägt fehl

Post by Anonymous »

Ich glaube, dass hier etwas passiert, das ich nicht verstehe, da Blazor aus der Windows Forms-Entwicklung stammt. Ich versuche, Daten von einer API abzurufen, von der ich weiß, dass sie funktioniert und Daten korrekt an eine andere Anwendung übermittelt. Mir scheint, dass der Code beendet wird, bevor der asynchrone Aufruf beendet werden kann.
Es ist in der Zeile httpClient.GetAsync abgestürzt. Zu Testzwecken wird dieser Aufruf ausgeführt, wenn die Seite mit der OnInitializedAsync-Funktion geladen wird.
Ich habe zwei verschiedene Tutorials befolgt, aber beide liefern das gleiche Ergebnis von .GetAsync Aufruf schlägt fehl: https://code-maze.com/blazor-webassembly-httpclient/ und
Bitte Hilfe.
Danke!
Serviceklasse:

Code: Select all

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 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;
}
}
Schnittstelle:

Code: Select all

public interface IRecipeService
{
//Task getRecipeByID(int id);
Task getRecipeByID(int id);
}
Funktion auf der Razor-Seite:

Code: Select all

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

Code: Select all

Program.cs
(fügte die folgende Zeile hinzu):

Code: Select all

builder.Services.AddScoped();
AKTUALISIERTER CODE
Schnittstelle

Code: Select all

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 TaskgetRecipeByID(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(result, jsonOptions);
return recipe;

}
}

public interface IRecipeService
{
Task getRecipeByID(int id);
}
Programm

Code: Select all

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

builder.Services.AddSingleton();
SEITE

Code: Select all

@inject IRecipeService RecipeService

private List recipe { get; set; }

public interface IRecipeService
{
Task getRecipeByID(int id);
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post