简体   繁体   English

HttpClient和ReadAsAsync <T> ()扩展方法

[英]HttpClient and ReadAsAsync<T>() extension method

So I'm starting up a new .Net 4.0 project and will be doing some work with a public API. 所以我正在启动一个新的.Net 4.0项目,并将使用公共API进行一些工作。 I'm planning on using the Microsoft HttpClient class so I installed the latest stable version of the Microsoft.Net.Http NuGet package (version 2.2.13). 我打算使用Microsoft HttpClient类,所以我安装了最新的稳定版Microsoft.Net.Http NuGet包(版本2.2.13)。 I'm looking at some POC code that a coworker put together, also using a NuGet package for HttpClient and notice that there's code like this: 我正在看一些同事放在一起的POC代码,也使用HttpClient的NuGet包,并注意到这样的代码:

HttpClient client = new HttpClient();
HttpResponseMessage response = client.GetAync("/uri").Result;

DomainType result = response.Content.ReadAsAsync<DomainType>().Result;

In my project, after adding the reference to the Microsoft.Net.Http package, when I try to write similar code, I noticed that HttpResponseMessage doesn't have a ReadAsAsync<T>() method. 在我的项目中,在添加对Microsoft.Net.Http包的引用之后,当我尝试编写类似的代码时,我注意到HttpResponseMessage没有ReadAsAsync<T>()方法。 After doing some digging in my coworker's POC solution, it looks like ReadAsAsync<T>() is actually an extension method in the System.Net.Http.Formatting assembly. 在我的同事的POC解决方案中进行一些挖掘之后,看起来ReadAsAsync<T>()实际上是System.Net.Http.Formatting程序集中的扩展方法。 In this POC solution, there's a reference to System.Net.Http.Formatting, however it's pulling this file from the path C:\\Program Files (x86)\\Microsoft ASP.NET\\ASP.NET MVC 4\\Assemblies\\System.Net.Http.Formatting.dll . 在这个POC解决方案中,有一个对System.Net.Http.Formatting的引用,但它从路径C:\\Program Files (x86)\\Microsoft ASP.NET\\ASP.NET MVC 4\\Assemblies\\System.Net.Http.Formatting.dll提取此文件C:\\Program Files (x86)\\Microsoft ASP.NET\\ASP.NET MVC 4\\Assemblies\\System.Net.Http.Formatting.dll

My question is, is there a better solution to getting access to this extension method than referencing this assembly from my local ASP.Net installation? 我的问题是,是否有更好的解决方案来访问此扩展方法,而不是从我的本地ASP.Net安装引用此程序集? My concern is that this is going to cause a problem on our CI server since it's not likely to have ASP.Net installed. 我担心这会导致我们的CI服务器出现问题,因为它不太可能安装ASP.Net。 I guess I could copy System.Net.Http.Formatting to a Lib directory in my solution but I'm hoping there's a better option such as another NuGet package I'm missing that would give me this assembly. 我想我可以将System.Net.Http.Formatting复制到我的解决方案中的Lib目录,但是我希望有一个更好的选择,比如我错过的另一个NuGet包会给我这个程序集。

Thanks! 谢谢!

That's part of the WebAPI client libraries package. 这是WebAPI客户端库包的一部分。 Install the Microsoft.AspNet.WebApi.Client nuget package. 安装Microsoft.AspNet.WebApi.Client nuget包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有Content.ReadAsAsync方法的HttpClient多线程 - HttpClient multithreading with Content.ReadAsAsync method HttpClient ReadAsAsync <IQueryable<T> &gt;永不归来 - HttpClient ReadAsAsync<IQueryable<T>> never returns HttpClient ReadAsAsync不反序列化 - HttpClient ReadAsAsync does not deserialize 有 HttpContent.ReadAsAsync<t> .NET Core 中的方法已被取代?</t> - Has HttpContent.ReadAsAsync<T> method been superceded in .NET Core? 将HTTPClient.ReadAsAsync结果反序列化为对象列表 - Deserialize HTTPClient.ReadAsAsync results into a List of Objects 我可以伪造HttpContent.ReadAsAsync <T> ? - Can I fake HttpContent.ReadAsAsync<T>? 在运行时未找到方法HttpContentExtensions.ReadAsAsync - Method HttpContentExtensions.ReadAsAsync not found runtime 使用 .NET 4.0 任务模式使用 HTTPClient .ReadAsAsync 将 JSON 反序列化为数组或列表 - Deserialize JSON to Array or List with HTTPClient .ReadAsAsync using .NET 4.0 Task pattern Web Api Content.ReadAsAsync方法未找到,但代码编译 - Web Api Content.ReadAsAsync method not found, but code compiles 无法使用ReadAsAsync反序列化XML响应中的列表<T> - Unable to deserialize list in a XML Response using ReadAsAsync<T>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM