【问题标题】:HttpClient failingHttpClient 失败
【发布时间】: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


【解决方案1】:

您的程序集绑定重定向错误。您的 iOS 项目应该有一个包含以下内容的 app.config 文件:

<?xml version="1.0" encoding="utf-8"?>
<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>

重定向的原因是 Microsoft.Net.HttpClient 包包含 System.Net.HttpClient 命名空间,但它已经存在于 iOS 和 Android 上(甚至功能略有不同)。在运行时,它将解析到错误的程序集,而不是使用(Xamarin-)本机 iOS 程序集。

您可以在我的Github repo 的 PCL 中找到基于 HttpClient 的 iOS、Android 和 WP8 演示项目。

【讨论】:

  • 当我使用它时,我在调用 postasjsonasync() 时遇到异常。异常显示“发生类型加载异常”。消息是:“”无法从程序集“System.Net.Http.Formatting”中加载类型“System.Net.Http.ObjectContent`1[T]”,...“”。如果我从 app.config 中删除它,它会再次正常工作。
  • 你使用了repo中的demo吗?
  • 我按照 iOS 项目下 repo 的说明进行操作。我将该部分添加到 app.config 并将一行添加到 packages.config。我已经编辑了我的主要帖子,以向您展示发生任何对“postasjsonasync()”的调用时引发的整个错误。
  • 模拟器中出现此错误。随着您的配置文件更改,这应该在模拟器和设备中都可以工作,对吗?每次部署到不同的目标时都不需要更改此设置? (设备与模拟器)?
  • 没错。回购中的演示是否适合您(无需更改)?
【解决方案2】:

通过从 PCL 和 iOS 项目中卸载“Microsoft HTTP 客户端库”nuget 包解决了这个问题。然后在两者上重新安装。 iOS 项目现在有对“System.Net.Http.Extensions”和“System.Net.Http.Primatives”的引用,但不包含对“System.Net.Http”的引用。 iOS 项目的 app.config 文件中也使用了以下绑定重定向;

<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />

【讨论】:

    猜你喜欢
    • 2016-04-20
    • 2015-08-04
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    相关资源
    最近更新 更多