代码之家  ›  专栏  ›  技术社区  ›  Emil

如何在中实现HttpClientXamarin.表格使用更好的CPU和内存的应用程序?

  •  0
  • Emil  · 技术社区  · 8 年前

    1. 每次处理vs单例HttpClient和静态客户端: 据说不要在httpClient周围使用using,而是使用中所述的singleton实现 this 文章

    错误的;

    var httpClient = new HttpClient(new HttpClientHandler
    {
        //Some settings
    });
    

    对的:

    public class HttpService
    {
        private readonly HttpClient _httpClient;
    
        public HttpService()
        {
            _httpClient = CreateHttpClient();
        }
    

    第一个问题,如果我们不使用 static 因为HttpService将为每个线程启动一个新的客户端,而不是?如果我们使用静态客户机,这会导致任何内存周期吗?

    1. 无论何时DNs发生如上所述的更改,使用单例HttpClient似乎都有问题 here . 如何在短时间内克服这个问题xamarin.forms公司应用程序?我没有看到.net标准2.0中定义任何ServicePointManager。我真的要担心这个吗?

    2. 当得到回应时。使用ReadAsStreamAsync有很大区别吗?有没有什么副作用可以作为流使用?

    3. 我们是否应该使用下面的方法来处理HttpResponseMessage?

      {
      如果(!response.issueccessStatusCode) { //做点什么 } 其他的 { //做点什么 }

    最后,我的课看起来是这样的,你觉得这有什么问题吗? 代理设置如中所述 this 文章

    namespace myApp
    {
    
        public class HttpService
        {
            private readonly HttpClient client;
            private JsonSerializer _serializer = new JsonSerializer();        
    
            public HttpService()
            {
    
                if (client == null)
                {
                    try
                        {
                            HttpClientHandler handler = new HttpClientHandler
                            {
                                Proxy = Xamarin.Forms.DependencyService.Get<IProxyInfoProvider>().GetProxySettings()
                            };
                            client = new HttpClient(handler);
                        }
                        catch (Exception ex)
                        {
    
                        }              
                } 
            }
    
    
            public async Task<T> GetItemAsync<T>(string url, CancellationToken cancellationToken=default(CancellationToken))
            {
                T returnObject = default(T);
                Uri uri = new Uri(url);
    
                try
                {
                   using (HttpResponseMessage response = await client.GetAsync(uri,cancellationToken))
                    {   
                    if (!response.IsSuccessStatusCode)
                    {
                       //Handle error
                    }
                    else
                    {
                        returnObject = await getReturnObject<T>(response);
                    }
                    }
                }
                catch (OperationCanceledException)
                {
    
                }
                catch (System.Net.Http.HttpRequestException)
                {
    
                }
                catch (Exception ex)
                {
    
                }
    
                return returnObject;
            }
    
    
    
    
            public async Task<T> getReturnObject<T>(HttpResponseMessage response)
            {
                T returnObject = default(T);
    
                if (response.IsSuccessStatusCode)
                {
                    using (System.IO.Stream stream = await response.Content.ReadAsStreamAsync())
                    using (System.IO.StreamReader reader = new System.IO.StreamReader(stream))
                    using (JsonTextReader json = new JsonTextReader(reader))
                    {
                        returnObject = _serializer.Deserialize<T>(json);
                    }
                    //string content = await response.Content.ReadAsStringAsync();
                    //returnObject = JsonConvert.DeserializeObject<T>(content);
                }
    
                return returnObject;
            }
    
    
        }
    }
    
    0 回复  |  直到 8 年前