Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

HttpClient using Xamarin Forms

$
0
0

I'm working on a Xamarin forms application and not sure if this is an error triggered by C#/HttpClient or by Xamarin Forms.

In my Xamarin Forms application, I have a RequestService class that contains the following code:

public class RequestService : IRequestService
{
    private static HttpClient instance;
    private static HttpClient HttpClientInstance => instance ?? (instance = new HttpClient(new NativeMessageHandler() { EnableUntrustedCertificates = true, DisableCaching = true }));
    public async Task<TResult> GetAsync<TResult>(string uri, string token = "")
    {
        setupHttpClient(token);
        HttpResponseMessage response = await HttpClientInstance.GetAsync(uri).ConfigureAwait(false);
        await HandleResponse(response);
        string responseData = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
        return await Task.Run(() => JsonConvert.DeserializeObject<TResult>(responseData));
    }

    private void setupHttpClient(string token = "")
    {
        HttpClientInstance.DefaultRequestHeaders.Accept.Clear();
        HttpClientInstance.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        if (!string.IsNullOrWhiteSpace(token))
        {
            HttpClientInstance.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", token);
        }
    }

    private async Task HandleResponse(HttpResponseMessage response)
    {
        if (!response.IsSuccessStatusCode)
        {
            var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
            if (response.StatusCode == HttpStatusCode.Forbidden || response.StatusCode == HttpStatusCode.Unauthorized)
            {
                throw new Exception(content);
            }
            throw new HttpRequestException(content);
        }
    }
}

It has been working quite well for the last 5 to 7 days but today it started crashing without any errors.Any request just exits the application.

I managed to debug and trace successful execution until the line:

 HttpResponseMessage response = await HttpClientInstance.GetAsync(uri).ConfigureAwait(false);

It is on this line that it tries to do something and then just exits the app. The Debug console for that line shows the following:

Thread started:  #3
05-14 10:20:51.974 D/Mono    (20217): Assembly Ref addref ModernHttpClient[0x7028fef180] -> System[0x701365c000]: 15
05-14 10:20:51.986 D/Mono    (20217): Assembly Ref addref ModernHttpClient[0x7028fef180] -> System.Core[0x701439c500]: 10
05-14 10:20:52.098 D/Mono    (20217): DllImport searching in: '__Internal' ('(null)').
05-14 10:20:52.098 D/Mono    (20217): Searching for 'java_interop_jnienv_new_object_array'.
05-14 10:20:52.098 D/Mono    (20217): Probing 'java_interop_jnienv_new_object_array'.
05-14 10:20:52.098 D/Mono    (20217): Found as 'java_interop_jnienv_new_object_array'.
05-14 10:20:52.101 D/Mono    (20217): DllImport searching in: '__Internal' ('(null)').
05-14 10:20:52.101 D/Mono    (20217): Searching for 'java_interop_jnienv_set_object_array_element'.
05-14 10:20:52.101 D/Mono    (20217): Probing 'java_interop_jnienv_set_object_array_element'.
05-14 10:20:52.101 D/Mono    (20217): Found as 'java_interop_jnienv_set_object_array_element'.
05-14 10:20:52.107 D/Mono    (20217): DllImport searching in: '__Internal' ('(null)').
05-14 10:20:52.107 D/Mono    (20217): Searching for 'java_interop_jnienv_get_object_array_element'.
05-14 10:20:52.107 D/Mono    (20217): Probing 'java_interop_jnienv_get_object_array_element'.
05-14 10:20:52.107 D/Mono    (20217): Found as 'java_interop_jnienv_get_object_array_element'.
05-14 10:20:52.213 D/Mono    (20217): DllImport searching in: '__Internal' ('(null)').
05-14 10:20:52.214 D/Mono    (20217): Searching for 'java_interop_jnienv_call_boolean_method'.
05-14 10:20:52.214 D/Mono    (20217): Probing 'java_interop_jnienv_call_boolean_method'.
05-14 10:20:52.214 D/Mono    (20217): Found as 'java_interop_jnienv_call_boolean_method'.
05-14 10:20:52.348 F/        (20217): /Users/builder/jenkins/workspace/xamarin-android-d15-6/xamarin-android/external/mono/mono/mini/debugger-agent.c:4846: (null) assembly:mscorlib.dll type:BadImageFormatException member:<none>

A few things to note here are as follows:

  1. I'm using ModernHttpClient
  2. The settings under Android Project -> Properties -> Android Options -> are:
    HttpClient Implementation = Android
    SSL/TLD Implementation = Native TFS 1.2+
  3. I have also tried wrapping that line in a try...catch block but the control never gets to the exception section ut rather just exits.

I have reproduced the issue in a simple solution hosted here.

Has anybody come across this issue? Any assistance would be greatly appreciated.

Thanks


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>