命名空间 'System.Net' 中不存在类型或命名空间名称 'Http'(是否缺少程序集引用?)

The type or namespace name 'Http' does not exist in the namespace 'System.Net' (are you missing an assembly reference?)

我正在尝试按照 pluralsight 视频中的示例进行操作

https://app.pluralsight.com/player?course=aspdotnet-5-ef7-bootstrap-angular-web-app&author=shawn-wildermuth&name=aspdotnet-5-ef7-bootstrap-angular-web-app-m7&clip=8&mode=live&start=2

当我尝试完成 Api 添加坐标时出现错误:

命名空间 'System.Net' 中不存在类型或命名空间名称 'Http'(是否缺少程序集引用?)

这发生在以下 class:

using Microsoft.Extensions.Logging;
using System.Net;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using System.Net.Http;

namespace Moran.Services
{
    public class CoordService
    {
        private ILogger<CoordService> _logger;

        public CoordService(ILogger<CoordService> logger)
        {
            _logger = logger;
        }

        public async Task<CoordServiceResult> Lookup(string location)
        {
            var result = new CoordServiceResult()
            {
                Success = false,
                Message = "Undetermined failures while looking up coordinates"
            };
            //Lookup Coordinates
            var bingKey = Startup.Configuration["AppSettings:BingKey"];
            var encodedName = WebUtility.UrlEncode(location);
            var url = $"http://dev.virtualearth.net/REST/v1/Locations?q={encodedName}&key={bingKey}";

            var client = new HttpClient();

            var json = await client.GetStringAsync(url);

            // Read out the results
            // Fragile, might need to change if the Bing API changes
            var results = JObject.Parse(json);
            var resources = results["resourceSets"][0]["resources"];
            if (!resources.HasValues)
            {
                result.Message = $"Could not find '{location}' as a location";
            }
            else
            {
                var confidence = (string)resources[0]["confidence"];
                if (confidence != "High")
                {
                    result.Message = $"Could not find a confident match for '{location}' as a location";
                }
                else
                {
                    var coords = resources[0]["geocodePoints"][0]["coordinates"];
                    result.Latitude = (double)coords[0];
                    result.Longitude = (double)coords[1];
                    result.Success = true;
                    result.Message = "Success";
                }
            }
            return result;
        }
    }
}

当我尝试添加时出现这种情况

var client = new HttpClient();

知道为什么会这样吗?

我找不到任何无法编译的原因....

HttpClient class 驻留在 System.Net.Http 命名空间中,它位于 System.Net.Http.dll 中。要在您的 ASP.NET 5 项目中使用此 class,您需要将 System.Net.Http 添加到 project.json 中 json 数据的 "dependencies" 部分文件

  "dependencies": {    
    "Microsoft.AspNet.Diagnostics": "1.0.0-beta5",
    "Microsoft.AspNet.Mvc": "6.0.0-beta5",
    "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-beta5",
    "Microsoft.AspNet.Server.IIS": "1.0.0-beta5",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-beta5",
    "Microsoft.AspNet.StaticFiles": "1.0.0-beta5",
    "Microsoft.AspNet.Tooling.Razor": "1.0.0-beta5",
    "Microsoft.Framework.Configuration.Json": "1.0.0-beta5",
    "Microsoft.Framework.Logging": "1.0.0-beta5",
    "Microsoft.Framework.Logging.Console": "1.0.0-beta5",

    "System.Net.Http": "4.0.1-beta-23409"

  },

4.0.1-beta-23409 是撰写本文时的最新版本。 Visual studio intellisense 将为您提供多个可用版本,您可以选择 latest/stable.

进行此更改后,当您保存文件时,它将执行 dnu 恢复通常会根据需要下载必要的包) 并添加对 System.Net.Http 程序集的引用。您可以在 class 中添加 using 语句并开始使用 HttpClient class.

using System;
using System.Net.Http;
public class SomeClass
{
  public void SomeMethod()
  {
     var client = new HttpClient();
  }
}