如何使用更好的CPU和内存在Xamarin.Forms应用程序中实现HttpClient?

时间:2018-11-19 23:52:16

标签: xamarin xamarin.forms httpclient asp.net-core-webapi

我对xamarin.forms中所有这些问题和httpclient使用的变通方法感到困惑。我想总结一下我的理解和犹豫/问题

  1. 与Singleton HttpClient和Static Client进行每次处置:据说不要{@ 3}}文章中所述在httpClient周围使用using和使用singleton实现

错误;

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

正确:

public class HttpService
{
    private readonly HttpClient _httpClient;

    public HttpService()
    {
        _httpClient = CreateHttpClient();
    }

这是第一个问题,如果我们不使用static,这有什么好处?  因为HttpService将为每个线程启动一个新客户端,不是吗?如果我们使用静态客户端,这会导致任何内存周期吗?   Xamarin表单对于静态依赖项非常脆弱,如果您在ViewModel中持有静态值,并且ViewModel使用Freshmvvm,Prism等与View耦合,通常不会处理View,并且实例会保留在内存中,即使弹出后也会导致内存泄漏页面。

  1. DNS更改问题:似乎每当DN如this所述更改时,使用单例HttpClient都会出现问题。如何在xamarin.forms应用程序中克服此问题?我看不到.net标准2.0中定义的任何ServicePointManager。我真的要为此担心吗?

  2. ReadAsStreamAsync与ReadAsStringAsync 。使用ReadAsStreamAsync是否有很大的不同?并有任何副作用可以用作流吗?

  3. 我们应该使用如下用法来处理HttpResponseMessage吗?

    使用(HttpResponseMessage response =等待client.GetAsync(uri))                 {
                        如果(!response.IsSuccessStatusCode)                     {                        //做点什么                     }                     其他                     {                       //做点什么                     }                 }

最后,我的班级如下所示;您看到这个有什么问题吗? here文章

中描述了代理设置
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 个答案:

没有答案