Он аварийно завершился на строке httpClient.GetAsync. В целях тестирования этот вызов выполняется, когда страница загружается с помощью функции OnInitializedAsync.
Я следовал двум разным руководствам, но оба дают один и тот же результат, что и .GetAsync Ошибка вызова: https://code-maze.com/blazor-webassembly-httpclient/ и
Пожалуйста. Помогите.
Спасибо!
Класс обслуживания:
Код: Выделить всё
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;
}
}
Код: Выделить всё
public interface IRecipeService
{
//Task getRecipeByID(int id);
Task getRecipeByID(int id);
}
Код: Выделить всё
protected override async Task OnInitializedAsync()
{
recipe = await RecipeService.getRecipeByID(currentCount);
}
Код: Выделить всё
Program.csКод: Выделить всё
builder.Services.AddScoped();
Интерфейс
Код: Выделить всё
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);
}
Код: Выделить всё
builder.Services.AddHttpClient("MainApi", options =>
{
options.BaseAddress = new Uri(clConstants.SERVER_PATH);
});
builder.Services.AddSingleton();
Код: Выделить всё
@inject IRecipeService RecipeService
private List recipe { get; set; }
public interface IRecipeService
{
Task getRecipeByID(int id);
}
Код: Выделить всё
has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled
Подробнее здесь: https://stackoverflow.com/questions/793 ... mbly-fails
Мобильная версия