Reputation: 102
I have ASP.NET MVC 6 application, which calls some external web service. I use this guide: http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client Here is the code:
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;
}
}
}
}
But here is unexpected problem: I have 3 errors after build project:
Error CS0246 The type or namespace name 'HttpClient' could not be found
Error CS0246 The type or namespace name 'MediaTypeWithQualityHeaderValue' could not be found
Error CS0246 The type or namespace name 'HttpResponseMessage' could not be found
Looks like Microsoft.AspNet.WebApi.Client package version not compatible with my KRE version.
Am I right? And how can I fix this?
Here is project.json content:
{
"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"
]
}
Upvotes: 2
Views: 6010
Reputation: 807
Note for others : I had the same error message but only because I had just
using System.Net.Http;
I also needed to add.
using System.Net.Http.Headers;
Upvotes: 1
Reputation: 58454
You are mostly right. Microsoft.AspNet.WebApi.Client` package is being used both for clrcore and clr runtimes in your example. This package can only be used for clr runtime.
With the latest changes (renames and all that), here is a working piece:
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"
}
}
}
}
Upvotes: 1