简体   繁体   English

从scriptcs脚本中使用HttpClient

[英]Using HttpClient from scriptcs script

I am trying to use HttpClient from a scriptcs script but experiencing some issues. 我试图从Scriptcs脚本使用HttpClient但遇到一些问题。 First, I wasn't able to even install a Microsoft.Net.Http NuGet package because this package requires NuGet 2.8, and scriptcs was installed with NuGet.Core.dll version 2.7. 首先,我甚至无法安装Microsoft.Net.Http NuGet包,因为这个包需要NuGet 2.8,并且脚本与NuGet.Core.dll版本2.7一起安装。 But after fetching a prerelease version of scriptcs from MyGet (version 0.10 alpha) Microsoft.Net.Http was successfully installed. 但是从MyGet(版本0.10 alpha)获取scriptcs的预发布版本后,Microsoft.Net.Http已成功安装。 However, an attempt to instantiate HttpClient causes an error: 但是,尝试实例化HttpClient会导致错误:

error CS0234: The type or namespace name 'HttpClient' does not exist in the namespace 'System.Net.Http' (are you missing an assembly reference?) 错误CS0234:名称空间'System.Net.Http'中不存在类型或命名空间名称'HttpClient'(您是否缺少程序集引用?)

I tried with both "using" directive or specifying the qualified type name. 我尝试使用“using”指令或指定限定类型名称。 No luck. 没运气。 I can trace Microsoft.Net.Http is loaded. 我可以跟踪Microsoft.Net.Http已加载。

Here's the code that fails: 这是失败的代码:

using System.Net.Http;
var client = new HttpClient();

UPDATE. UPDATE。 Found the problem. 发现了问题。 I thought that referencing Microsoft.Net.Http would be sufficient. 我认为引用Microsoft.Net.Http就足够了。 But in .NET 4.5 I also had to reference System.Net.Http: 但在.NET 4.5中我还必须引用System.Net.Http:

#r "System.Net.Http"

Now it works! 现在它有效!

I thought that referencing Microsoft.Net.Http would be sufficient. 我认为引用Microsoft.Net.Http就足够了。 But in .NET 4.5 I also had to reference System.Net.Http: 但在.NET 4.5中我还必须引用System.Net.Http:

#r "System.Net.Http"

This solved the issue. 这解决了这个问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM