2

I try to write simple code:

 public async Task<string> GetData(String labelName)
    {           
        using (var client = new HttpClient())
        {
            var uri = new Uri(@"https://example.com/over/search_field?=search_label=" + labelName);

            var response = await client.GetAsync(uri).ConfigureAwait(false);
            var textResult = await response.Content.ReadAsStringAsync();
            return textResult;
        }
    }

At project.json:

 {
"webroot": "wwwroot",
"version": "1.0.0-*",
"dependencies": {
  "Microsoft.AspNet.IISPlatformHandler": "1.0.0-beta8",
 "Microsoft.AspNet.Mvc": "6.0.0-beta8",
 "Microsoft.AspNet.Server.Kestrel": "1.0.0-beta8",
 "Microsoft.AspNet.StaticFiles": "1.0.0-beta8",

 "Microsoft.Framework.Logging": "1.0.0-beta8",
 "Microsoft.Framework.Logging.Console": "1.0.0-beta8",
 "Microsoft.Framework.Logging.Debug": "1.0.0-beta8",
 "Microsoft.Net.Http": "2.2.22"
  },      
 "frameworks": {
  "dnx451": {
    "dependencies": {
       "Microsoft.AspNet.WebApi.Client": "5.2.3",
      "Microsoft.AspNet.WebApi.Owin": "5.2.3",
      "System.Net.Http": "4.0.1-beta-23409"


   },
   "frameworkAssemblies": {
     "System.Net": "4.0.0.0",
     "System.Net.Http": "4.0.0.0"
   }
 },
 "dnxcore50": { }
},

But, when i try to build code i get error:

Error CS0246 The type or namespace name 'HttpClient' could not be found (are you missing a using directive or an assembly reference?)

Can you help me? How to fix this error? I use VS 2015 and ASP MVC 6 Web API. Thank you!

1
  • 2
    Try moving "dependencies" outside of the "frameworks". I assume your application could not be compiled when targeting dnxcore50? Commented Dec 13, 2015 at 15:52

1 Answer 1

5

This worked for me. I added System.Net.Http to the dependencies section and built the project (thus restored this new dependency in both DNX 4.5.1 and DNX Core 5.0)

"dependencies": {
    "Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final",
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
    "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
    // Removed some dependencies for saving space:)
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc1-final",
    "System.Net.Http": "4.0.1-beta-23409"
},
"frameworks": {
    "dnx451": { },
    "dnxcore50": {}
},

Verified in the below runtimes and it works fine.

  • coreclr - X86 - 1.0.0-rc1-update1
  • coreclr - X64 - 1.0.0-rc1-update1
  • clr - X86 - 1.0.0-rc1-update1
  • clr - X64 - 1.0.0-rc1-update1

Also, you need to make sure that you have the using directive which imports the System.Net.Http namespace in the class file where you have your GetData method.

using System.Net.Http;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you - using rc1-update1 x64 and solved my issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.