ASP.NET 5,Microsoft.AspNet.WebApi.Client 包,在 ASP.NET 5 beta3 中找不到错误
ASP.NET 5, Microsoft.AspNet.WebApi.Client package, not found error in ASP.NET 5 beta3
我有 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"
]
}
你基本上是对的。 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"
}
}
}
}
我有 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"
]
}
你基本上是对的。 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"
}
}
}
}