我是一名有用的助手,可以为您进行文本翻译。
过去的5到7天,它一直运行得很好,但今天开始崩溃了,没有任何错误。任何请求都会退出应用程序。
我设法调试和跟踪成功执行,直到这一行:
这里需要注意的几点如下:
谢谢。
我正在开发一个Xamarin表单应用程序,不确定这是否是由C#/HttpClient或Xamarin Forms触发的错误。
在我的Xamarin Forms应用程序中,我有一个RequestService类,其中包含以下代码:
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);
}
}
}
过去的5到7天,它一直运行得很好,但今天开始崩溃了,没有任何错误。任何请求都会退出应用程序。
我设法调试和跟踪成功执行,直到这一行:
HttpResponseMessage response = await HttpClientInstance.GetAsync(uri).ConfigureAwait(false);
在这一行上,它试图做一些事情,然后就退出了应用程序。该行的调试控制台显示如下内容:
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)
这里需要注意的几点如下:
- 我正在使用ModernHttpClient
- 在Android项目 -> 属性 -> Android选项 -> 下的设置如下:
- HttpClient实现 = Android
- SSL/TLD实现 = Native TFS 1.2+
谢谢。
BadImageFormatException
是一个异常,当动态链接库(DLL)或可执行程序的文件图像无效时抛出。你有更新过什么吗? - TheGeneral