4

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

7
  • 1
    I would start by removing the ConfigureAwait from the request. There's no point in awaiting a Synchronous Task. Then what I find best is to stick a try catch around your method and debug the exception from there. At the very least it will help you gain a better understanding of the problem. Commented May 14, 2018 at 10:37
  • BadImageFormatException The exception that is thrown when the file image of a dynamic link library (DLL) or an executable program is invalid. have you updated anything ? Commented May 14, 2018 at 10:38
  • @whiskeycoder how is that synchronous? You can only await an async task and you can only configureawait an awaited task. Removing that will cause the continuation to resume on the captured context but will not cause an observable difference. Commented May 14, 2018 at 11:55
  • Thanks for the inputs. I have removed the ConfigureAwait and have tried wrapping a try...catch around that line. The debugger gets to that line and then just exits debugging. It does not get to the exception section. I have repro'd the error in another simple solution here. Commented May 15, 2018 at 5:23
  • @TheGeneral: It took me sometime to find out but yes updated XF to latest stable version and Modernhttpclient-updated to v2.7.0 which was released around the same time as XF v3+. BTW!! What does the BadImageException line translate to? Commented May 15, 2018 at 9:05

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.