如何将HttpClientFactory与polly一起使用以在WPF中调用Get请求

时间:2018-11-14 02:20:43

标签: c# .net dotnet-httpclient polly

这是我用于消费get请求的代码。它运行良好,但是我想用polt封装HttpClientFactory,以便在异步过程中跟踪超时或网络故障。我该怎么办?

public static async Task<CustomerResponse> GetCustomersAsync(IQueryCritera critera)
    {
        if (critera == null)
        {
            throw new ArgumentNullException(nameof(critera));
        }
        var parmeters = new List<string>
            {
                "request.active=false"
            };

        if (!string.IsNullOrWhiteSpace(critera.SearchText))
        {
            parmeters.Add($"request.filter={critera.SearchString}");
        }

        var builder = new UriBuilder(GetApiUri());
        builder.Path += "Customers/GetCustomers";
        builder.Query = string.Join("&", parmeters);

   var client = new HttpClient()
        {
            BaseAddress = GetBaseAddr();
        };

        using (var response = await client.GetAsync(builder.Uri).ConfigureAwait(false))
        {

            var responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
            var result = JsonConvert.DeserializeObject<CustomerResponse>(responseText);
        }
    }

0 个答案:

没有答案