Reputation: 373
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:
Has anybody come across this issue? Any assistance would be greatly appreciated.
Thanks
Upvotes: 4
Views: 7853
Reputation: 373
Ok! So this was solved by doing the following:
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