System.Net.Http.Formatting库的PCL重新包装中的ReadAsAsync <>

Ron*_*ira 4 c# rest json.net portable-class-library dotnet-httpclient

尝试在System.Net.Http.Formatting的Bitrium.Http.Extensions重新包装中调用ReadAsAsync <>扩展方法时,我收到以下错误.

程序集'System.Net.Http.ObjectContent'中的方法'SerializeToStreamAsync'来自程序集'Bitrium.Http.Extensions,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = 43390f7aed073600'没有实现.

我在PCL项目中有类似于以下代码的东西,我通过简单的单元测试进行测试.我没有实现await模式,但我不相信它与此相关.

public class RestApi()
{
    public void Get()
    {
        HttpResponseMessage response = new HttpResponseMessage();
        HttpClient client = new HttpClient();
        response = client.GetAsync("http://someUrl.com").Result;
        var modelList = response.Content.ReadAsAsync<List<Model>>().Result; // I get the exception here
     }
}
Run Code Online (Sandbox Code Playgroud)

对方法的调用:

var target = new RestApi();
target.Get();
Run Code Online (Sandbox Code Playgroud)

在这一点上我能做些什么吗?我不一定需要Async功能,所以另一个仍然将我的响应转换为我的模型的实现也可以.

Ron*_*ira 6

我没有使用Bitrium重新打包来尝试重用ReadAsAsync<T>System.Net.Http.Formatting中的扩展方法,而是选择使用Json.NET.因此不需要依赖重新包装,而只需要依赖包含HttpClient(NuGet <package id="Microsoft.Net.Http" version="2.2.18" targetFramework="portable-net45+sl50+MonoAndroid10+MonoTouch10" />)的lib的PCL构建的当前标准实现.

var resultString = response.Content.ReadAsStringAsync().Result;
Contacts = JsonConvert.DeserializeObject<List<T>>(resultString);
Run Code Online (Sandbox Code Playgroud)

我发现这篇文章对新的Json.NET作品很有帮助.