zoukankan      html  css  js  c++  java
  • IHttpClientFactory 踩过的坑

    public class BasicUsageModel : PageModel
    {
        private readonly IHttpClientFactory _clientFactory;
    
        public IEnumerable<GitHubBranch> Branches { get; private set; }
    
        public bool GetBranchesError { get; private set; }
    
        public BasicUsageModel(IHttpClientFactory clientFactory)
        {
            _clientFactory = clientFactory;
        }
    
        public async Task OnGet()
        {
            var request = new HttpRequestMessage(HttpMethod.Get,
                "https://api.github.com/repos/aspnet/AspNetCore.Docs/branches");
            request.Headers.Add("Accept", "application/vnd.github.v3+json");
            request.Headers.Add("User-Agent", "HttpClientFactory-Sample");
    
            var client = _clientFactory.CreateClient();
    
            var response = await client.SendAsync(request);
    
            if (response.IsSuccessStatusCode)
            {
                var responseStream = await response.Content.ReadAsStreamAsync();
                Branches = await JsonSerializer.DeserializeAsync
                    <IEnumerable<GitHubBranch>>(responseStream);
            }
            else
            {
                GetBranchesError = true;
                Branches = Array.Empty<GitHubBranch>();
            }
        }
    仔细看没什么问题吧?
    using var responseStream = await response.Content.ReadAsStreamAsync();
    
    

    使用的时候没加using,导致并发连接数高一直没有释放,致使应用奔溃

     
  • 相关阅读:
    mysql参数优化
    看见的一个mysql面试题
    面向对象的继承
    面向对象的权限修饰符
    php实现无限极分类
    php的冒泡排序
    frame框架的跳转
    thinkphp中open路径问题
    mysql触发器
    mysql事务
  • 原文地址:https://www.cnblogs.com/panjuan/p/14345245.html
Copyright © 2011-2022 走看看