HttpClient失败 [英] HttpClient failing

查看:190
本文介绍了HttpClient失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在iOS模拟器上运行我的应用程序时,它运行良好.当我在设备上运行它时,对HttpClient的首次调用失败.我的包含HttpClient调用的类位于PCL中.看起来像是此错误的实例.

When I run my app on the emulator for iOS it works fine. When I run it on a device the first call to HttpClient fails. My class containing the HttpClient calls is in a PCL. It looks like an instance of this bug.

但是,已记录的将以下内容添加到iOS项目app.config中的变通办法对我不起作用(system.net.http添加);

However, the documented workarounds of adding the following to your iOS project app.config does not work for me (the system.net.http addition);

<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(..)的底行失败并引发异常.例外是"null",并且也没有任何输出到终端.它可以在仿真器上正常运行,但不能在设备上运行.

With this in my app.config, the following method successfully retrieves the data from the server, but fails to read it. The bottom line containing ReadAsAsync(..) fails and throws an exception. The exception is 'null' and nothing is output to the terminal either. It works fine on the emulator, but not the device.

    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>>();
        }
    }

推荐答案

此问题通过从PCL和iOS项目中卸载"Microsoft HTTP Client Libraries" nuget程序包解决.然后在两者上重新安装. iOS项目现在已引用"System.Net.Http.Extensions"和"System.Net.Http.Primatives",但其中不包含"System.Net.Http". iOS项目的app.config文件中也使用了以下绑定重定向;

This was resolved by uninstalling the "Microsoft HTTP Client Libraries" nuget package from both the PCL and the iOS project. Then reinstalling on both. The iOS project has references now to 'System.Net.Http.Extensions' and to 'System.Net.Http.Primatives' but does not contain one to 'System.Net.Http'. The following binding redirect is used in the app.config file in the iOS project as well;

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

这篇关于HttpClient失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆