[英]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.