Task<HttpClient>'不包含'GetFromJsonAsync'的定义

问题描述 投票:0回答:1

我为我的项目创建了一个 C# 类库,并创建了一个名为 GetHttpClient.cs 的类 当我使用 GetPublicHttpClient() 函数时没有错误,但是当使用 GetPrivateHttpClient() 时 显示以下错误:(任务'不包含'GetFromJsonAsync'的定义,并且最佳扩展方法重载'HttpClientJsonExtensions.GetFromJsonAsync(HttpClient,string?,CancellationToken)'需要类型为'System.Net.Http的接收器。 HttpClient')

我安装了所有必需的软件包,例如 Microsoft.Extensions.Http" Version="8.0.0"

我的代码是:

using BaseLibrary.DTOs;

namespace ClientLibrary.Helpers
{
public class GetHttpClient(IHttpClientFactory httpClientFactory, LocalStorageService localStorageService)
{
private const string HeaderKey = "Authorization";

        public async Task<HttpClient> GetPrivateHttpClient()
        {
            var client = httpClientFactory.CreateClient("SystemApiClient");
            var stringToken = await localStorageService.GetToken();
            if (string.IsNullOrEmpty(stringToken)) return client;
    
            var deserializeToken = Serializations.DeserializeJsonString<UserSession>(stringToken);
            if (deserializeToken == null) return client;
    
            client.DefaultRequestHeaders.Authorization =
                new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", deserializeToken.Token);
            return client;
        }
    
        public HttpClient GetPublicHttpClient()
        {
            var client = httpClientFactory.CreateClient("SystemApiClient");
            client.DefaultRequestHeaders.Remove(HeaderKey);
            return client;
        }
    
    }

}

UserAccountService.cs 代码是

using System.Net.Http.Json;
using BaseLibrary.DTOs;
using BaseLibrary.Responses;
using ClientLibrary.Helpers;
using ClientLibrary.Services.Contracts;

namespace ClientLibrary.Services.Implementations
{
public class UserAccountService(GetHttpClient getHttpClient) : IUserAccountService
{
public const string AuthUrl = "api/authentication";
public async Task<GeneralResponse> CreateAsync(Register user)
{
var httpClient = getHttpClient.GetPublicHttpClient();
var result = await httpClient.PostAsJsonAsync($"{AuthUrl}/register", user);
if (!result.IsSuccessStatusCode) return new GeneralResponse(false, "Error occurred");

            return await result.Content.ReadFromJsonAsync<GeneralResponse>()!;
        }
    
        public async Task<LoginResponse> SignInAsync(Login user)
        {
            var httpClient = getHttpClient.GetPublicHttpClient();
            var result = await httpClient.PostAsJsonAsync($"{AuthUrl}/login", user);
            if (!result.IsSuccessStatusCode) return new LoginResponse(false, "Error occurred");
    
            return await result.Content.ReadFromJsonAsync<LoginResponse>()!;
        }
    
        public Task<LoginResponse> RefreshTokenAsync(RefreshToken token)
        {
            throw new NotImplementedException();
        }
    
        public async Task<WeatherForecast[]> GetWeatherForecast()
        {
            var httpClient = getHttpClient.GetPrivateHttpClient();
            var result = await httpClient.GetFromJsonAsync<WeatherForecast[]>("api/weatherforecast");
          
            return result!;
        }
    }

}

我阅读了几个文档但无法解决问题

httpclient
1个回答
0
投票

你好我的朋友,你在 GetWeatherForecast 方法的第一行添加了一个等待: var httpClient = await getHttpClient.GetPrivateHttpClient();

© www.soinside.com 2019 - 2024. All rights reserved.