5

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>();
Timothy Shields
  • 75,459
  • 18
  • 120
  • 173
  • 1
    See [this](http://stackoverflow.com/questions/27946798/httpclient-in-asp-net-5-0-not-found/27949353#27949353) – Yuval Itzchakov Jun 12 '15 at 00:46
  • @YuvalItzchakov That got everything working in the dnx451 context except for the `ReadAsAsync` extension method on `HttpContent`. It is a member of the `HttpContentExtensions` class in the `System.Net.Http` namespace. Any idea what framework assembly or dependency I need for that? This is a big help, by the way! – Timothy Shields Jun 12 '15 at 01:13
  • @YuvalItzchakov With your help I figured it out. See my answer. – Timothy Shields Jun 12 '15 at 01:19
  • Awesome. Glad it worked. – Yuval Itzchakov Jun 12 '15 at 01:26

2 Answers2

9

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"
      }
    }
  }
}
Timothy Shields
  • 75,459
  • 18
  • 120
  • 173
  • Do you need the reference for each frameworks? Wouldn't the top dependences apply to both framworks? I only have "System.Net.Http":"4.0.0-beta-23008" in dependencies and that shows up as both dnx451 and dnxcore50 reference in the Project List. – jonas Jun 12 '15 at 07:52
  • @jonas I just confirmed that if you do what you are saying `HttpClient` **will not** be accessible in both dnx451 and dnxcore50. Try writing code that uses it in a project configured as you are saying. – Timothy Shields Jun 12 '15 at 14:25
  • @Timothy Shields - I have done just that except it's Microsoft.Net.HttpClient and it works fine. – Shane Courtrille Sep 17 '15 at 02:05
  • @ShaneCourtrille Note the date on the question and answer. – Timothy Shields Sep 17 '15 at 05:15
0

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.

jgo
  • 372
  • 2
  • 12