【发布时间】:2014-12-17 09:04:00
【问题描述】:
当我在 iOS 模拟器上运行我的应用程序时,它运行良好。当我在设备上运行它时,对 HttpClient 的第一次调用失败。我的包含 HttpClient 调用的类位于 PCL 中。它看起来像 this bug 的一个实例。
但是,将以下内容添加到您的 iOS 项目 app.config 的记录解决方法对我不起作用(system.net.http 添加);
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
......
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="2.0.5.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
编辑:
在我的 app.config 中使用这个,以下方法成功地从服务器检索数据,但无法读取它。包含 ReadAsAsync(..) 的底线失败并引发异常。例外是“空”,也没有任何内容输出到终端。它在模拟器上运行良好,但在设备上运行良好。
public async Task<List<ExternalLoginViewModel>> GetExternalLoginsAsync()
{
using (var client = GetNewHttpClient(false))
{
client.DefaultRequestHeaders.Remove("Authorization");
var response = await client.GetAsync("api/account/externalLogins?returnUrl=/&generateState=true");
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsAsync<List<ExternalLoginViewModel>>();
}
}
【问题讨论】:
-
"...第一次调用 HttpClient 失败" - 它是如何失败的?
-
它以一种特殊的方式失败 :-) 我认为是因为它的 Xamarin.. 没有输出并且异常是“null”。
-
移动配置文件不支持配置文件,这可以解释为什么该解决方法会失败。
标签: xamarin.ios xamarin httpclient portable-class-library