自定义 API 客户端组织



我有一个自定义 API 客户端调用具有许多控制器的自定义 API 服务。我实际上有这个类:

public abstract class APIClientBase
{
protected HttpClient _httpClient;
public APIClientBase()
{
_httpClient = new HttpClient();
_httpClient.BaseAddress = new Uri("https://localhost:44369/");
}
}

它由许多类实现,如下所示:

public class APIClientEmployee : APIClientBase
{
public async Task<ObservableCollection<Employee>> GetAllEmployeesRequestAsync()
{
ObservableCollection<Employee> employees = null;
HttpResponseMessage response = await _httpClient.GetAsync($"api/employees");
if (response.IsSuccessStatusCode)
{
employees = response.Content.ReadAsAsync<ObservableCollection<Employee>>().Result;
}
return employees;
}
public ObservableCollection<Employee> GetAllVeterinaryEmployeesRequest()
{
ObservableCollection<Employee> employees = null;
HttpResponseMessage response = _httpClient.GetAsync($"api/employees/veterinaryemployees").Result;
if (response.IsSuccessStatusCode)
{
employees = response.Content.ReadAsAsync<ObservableCollection<Employee>>().Result;
}
return employees;
}
public async Task<Employee> GetSingleEmployeeRequestAsync(int id)
{
Employee employee = null;
HttpResponseMessage response = await _httpClient.GetAsync($"api/employees/{id}");
if (response.IsSuccessStatusCode)
{
employee = response.Content.ReadAsAsync<Employee>().Result;
}
return employee;
}
public Employee GetSingleEmployeeRequest(int id)
{
Employee employee = null;
HttpResponseMessage response = _httpClient.GetAsync($"api/employees/{id}").Result;
if (response.IsSuccessStatusCode)
{
employee = response.Content.ReadAsAsync<Employee>().Result;
}
return employee;
}
public async Task PutSingleEmployeeRequestAsync(Employee employee)
{
HttpResponseMessage response = await _httpClient.PutAsJsonAsync($"api/employees/{employee.EmployeeID}", employee);
response.EnsureSuccessStatusCode();
}
public async Task PostSingleEmployeeRequestAsync(Employee employee)
{
HttpResponseMessage response = await _httpClient.PostAsJsonAsync($"api/employees", employee);
response.EnsureSuccessStatusCode();
}
public async Task DeleteSingleEmployeeRequestAsync(int id)
{
HttpResponseMessage response = await _httpClient.DeleteAsync($"api/employees/{id}");
response.EnsureSuccessStatusCode();
}
}

我在我的视图模型中像这样使用它们:

public class EmployeeListViewModel
{
private APIClientEmployee _apiClientEmployee;
private ObservableCollection<Employee> _employees;
public ObservableCollection<Employee> Employees
{
get
{
return _employees;
}
set
{
_employees = value;
OnPropertyChanged("Employees");
}
}
public EmployeeListViewModel()
{
_apiClientEmployee = new APIClientEmployee();
FillEmployeeList();
}
public async void FillEmployeeList()
{
Employees = await _apiClientEmployee.GetAllEmployeesRequestAsync();
}
}

我在很多地方都做过这样的事情。此外,我在其他 ViewModel 中创建其他 APIClientEmployee,因为我在其他一些地方需要 Employee 数据。但是这种方法让我创建了许多APIClient 对象/Http 客户端,我觉得我这样做的方式有问题。我想我可以破坏我的插座极限矿石类似的东西,然后它会导致问题。我读过很多教程,但它们通常并不复杂。我应该只有一个类来包含每个 API 调用吗?我应该实现一个具有每个 APIClient 类的一个实例的单例吗?我不知道该怎么办,我想有建议...

你说得很对。查看 HttpClient 上的官方文档:

HttpClient 旨在实例化一次,并在应用程序的整个生命周期中重复使用。为每个请求实例化 HttpClient 类将耗尽重负载下可用的套接字数。这将导致套接字异常错误。下面是正确使用 HttpClient 的示例。

这就是它的声明方式

static readonly HttpClient client = new HttpClient();

您可以执行以下操作:

public abstract class APIClientBase
{
protected static readonly HttpClient client = new HttpClient();

然后像这样使用:

HttpResponseMessage response = await APIClientBase.client.GetAsync($"api/employees/{id}");

相关内容

  • 没有找到相关文章

最新更新