How should I reference HttpClient for dnx451 and dnxcore50?

3.2k views Asked by At

How should I reference HttpClient using a project.json file?
I want both frameworks to work: dnx451 and dnxcore50.

Here is my current attempt at the project.json file. (I've removed the irrelevant parts.)

{
  "dependencies": {
    "Microsoft.Net.Http": "2.2.29",
    "Microsoft.Net.Http.Headers": "1.0.0-beta4",
    "System.Net.Http": "4.0.0-beta-22816"
  },
  "frameworks": {
    "dnx451": {
      "frameworkAssemblies": {
        "System.Net.Http": "4.0.0.0"
      }
    },
    "dnxcore50": { }
  }
}

Discovering the dependencies I do have listed was a trial-and-error procedure.

With this project.json file, the dnxcore50 context properly resolves all the classes in this example block of code, but it fails to resolve HttpRequestMessage, HttpMethod, and MediaTypeWithQualityHeaderValue with the dnx451 context:

var request = new HttpRequestMessage(HttpMethod.Get, "...");
request.Headers.Accept.Clear();
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/..."));
var response = await client.SendAsync(request);
var model = await response.EnsureSuccessStatusCode().Content.ReadAsAsync<SomeModel>();
2

There are 2 answers

4
Timothy Shields On BEST ANSWER

As of the time of posting (June 11, 2015) this is the combination that worked for me for both dnx451 and dnxcore50.

{
  "dependencies": {
    "Microsoft.AspNet.WebApi.Client": "5.2.3"
  },
  "frameworks": {
    "dnx451": {
      "frameworkAssemblies": {
        "System.Net.Http": "4.0.0.0"
      }
    },
    "dnxcore50": {
      "dependencies": {
        "System.Net.Http": "4.0.0-beta-22816"
      }
    }
  }
}
0
jgo On

I'm marking Timothy's answer as helpful as it lead me down the right path, but this question/answer is also a few months old. Since then, ASP.NET5 has RCed. Here's what works for me now:

{
  "version": "1.0.0-*",

  ...

  "dependencies": {
    "System.Runtime": "4.0.21-beta-23516",
    "Newtonsoft.Json": "6.0.6",
    "Microsoft.CSharp": "4.0.1-beta-23516",
    "System.Net.Http": "4.0.1-beta-23516"
  },
  "frameworks": {
    "dnx451": {
      "dependencies": {
      }
    },
    "dnxcore50": {
      "dependencies": {
      }
    }
  }
}

which seems to be similar with this more verbose way of specifying the dependencies:

{
  "version": "1.0.0-*",

  ...

  "dependencies": {
    "System.Runtime": "4.0.21-beta-23516",
    "Newtonsoft.Json": "6.0.6",
    "Microsoft.CSharp": "4.0.1-beta-23516"
  },
  "frameworks": {
    "dnx451": {
      "dependencies": {
        "System.Net.Http": "4.0.1-beta-23516"
      }
    },
    "dnxcore50": {
      "dependencies": {
        "System.Net.Http": "4.0.1-beta-23516"
      }
    }
  }
}

Both versions do work as I was able to debug my HttpClient calls successfully.

You may ignore the other dependencies except for System.Net.Http.