Noel
Noel

Reputation: 373

HttpClient using Xamarin Forms

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>
05-14 10:20:52.354 F/libc    (20217): Fatal signal 6 (SIGABRT), code -6 in tid 20217 (com.companyname.appname), pid 20217 (com.companyname.appname)

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+

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

Thanks

Upvotes: 4

Views: 7853

Answers (1)

Noel
Noel

Reputation: 373

Ok! So this was solved by doing the following:

  1. Upgrade to latest version of Xamarin forms.
  2. Get rid of ModernHttpClient as it is not required any more due to point # 3.
  3. The functionality of ModernHttpClient (i.e Native SSL/Tls) is now built into Xamarin and can now be configured at the platform level as per below links:

iOS - https://learn.microsoft.com/en-us/xamarin/cross-platform/macios/http-stack

Android - https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/http-stack?tabs=windows

Upvotes: 3

Related Questions