ASP.NET 5,Microsoft.AspNet.WebApi.Client 包,在 ASP.NET 5 beta3 中未找到错误

发布时间: 2016/7/10 18:29:06
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有 ASP.NET MVC 6 应用程序,它调用一些外部 web 服务。 我使用本指南︰ http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client这里是代码︰

using System;
using System.Net.Http;
using System.Net.Http.Headers;

namespace TestService.Web.Code
{
    internal class ServiceProxy
    {
        internal string Get(string predicate)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:8001/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage response = client.GetAsync(string.Format("TestService/LookupService" +
                                                                         "/GetCountries?term={0}", predicate)).Result;

                if (response.IsSuccessStatusCode)
                {
                     var answer = response.Content.ReadAsStringAsync().Result;
                     return answer;
                }

                return string.Empty;
            }
        }
    }
}

但这里是意想不到的问题︰ 我有 3 错误后生成项目︰

  • 错误 CS0246 无法找到类型或命名空间名称 'HttpClient'

  • 错误 CS0246 无法找到类型或命名空间名称 MediaTypeWithQualityHeaderValue

  • 错误 CS0246 无法找到类型或命名空间名称 HttpResponseMessage

看起来像 Microsoft.AspNet.WebApi.Client 包版本与我 KRE 版本不兼容。

我是正确的吗?和如何可以解决此问题?

这里是 project.json 的内容︰

{

    "webroot": "wwwroot",
    "version": "1.0.0-*",
    "dependencies": {
    "Microsoft.AspNet.Server.IIS": "1.0.0-beta3",
    "Microsoft.AspNet.Mvc": "6.0.0-beta3",
    "Microsoft.AspNet.StaticFiles": "1.0.0-beta3",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-beta3",
    "Microsoft.AspNet.Security.Cookies": "1.0.0-beta3",
    "System.Net.Http": "4.0.0-beta-22605",
    "Microsoft.AspNet.WebApi.Client": "5.2.3",
    "Microsoft.AspNet.Mvc.WebApiCompatShim": "6.0.0-beta3"
},
"frameworks": {
    "aspnet50": {
        "frameworkAssemblies": {
            "System.Net.Http": "4.0.0.0",
            "System.Net": "4.0.0.0"
        }
    },
    "aspnetcore50": {
        "dependencies": {
            "System.Net.Http": "4.0.0-beta-22605"
        }
    }
},
"exclude": [
    "wwwroot",
    "node_modules",
    "bower_components"
],
"bundleExclude": [
    "node_modules",
    "bower_components",
    "**.kproj",
    "**.user",
    "**.vspscc"
]
}

解决方法 1:

你大多是正确。Microsoft.AspNet.WebApi.Client' 包是正在使用的 clrcore 和 clr 运行时都以你为榜样。此程序包仅可用于 clr 运行时。

与最新的更改 (重命名和所有的),这里有一块工作︰

using System;
using System.Net.Http;
using System.Net.Http.Headers;

public class Program
{
    public void Main(string[] args)
    {
    }

    internal class ServiceProxy
    {
        internal string Get(string predicate)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:8001/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage response = client.GetAsync(string.Format("TestService/LookupService" +
                                                                         "/GetCountries?term={0}", predicate)).Result;

                if (response.IsSuccessStatusCode)
                {
                     var answer = response.Content.ReadAsStringAsync().Result;
                     return answer;
                }

                return string.Empty;
            }
        }
    }
}

project.json:

{
    "dependencies": {
    },
    "frameworks": {
        "dnx451": { 
            "dependencies": {
                                "Microsoft.AspNet.WebApi.Client": "5.2.3"
            }
        }
    }
}
赞助商